ConfigMain.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. using System;
  2. using System.Data;
  3. using System.Linq;
  4. using System.Net;
  5. using System.ServiceModel;
  6. using System.Text.RegularExpressions;
  7. using System.Threading;
  8. using System.Windows.Forms;
  9. using System.Xml.Linq;
  10. using Curtain.Log;
  11. using Dongke.IBOSS.PRD.Basics.BaseResources;
  12. using Dongke.IBOSS.PRD.Basics.DataAccess;
  13. using Dongke.IBOSS.PRD.Basics.Library;
  14. using Dongke.IBOSS.PRD.WCF.Services;
  15. namespace Dongke.IBOSS.PRD.WCF.Hosting
  16. {
  17. public partial class ConfigMain : Form
  18. {
  19. private bool _isClose = false;
  20. private SynchronizationContext _syncContext;
  21. private string _formTitle = "[{0}]" + Constant.M_SYSTEM_NAME + "服务器端配置及服务-{1}";
  22. public ServiceHostCollection ServiceHosts
  23. {
  24. get;
  25. private set;
  26. }
  27. public ConfigMain()
  28. {
  29. InitializeComponent();
  30. txtExePath.Text = System.AppDomain.CurrentDomain.BaseDirectory;
  31. _syncContext = SynchronizationContext.Current;
  32. //ServiceInvoker.ServiceDoingEvent += DKIMSSWCFService_ServiceDoingEvent;
  33. //ServiceInvoker.ServiceDoneEvent += DKIMSSWCFService_ServiceDoneEvent;
  34. //ServiceInvoker.ServiceExceptionEvent += DKIMSSWCFService_ServiceExceptionEvent;
  35. Logger.DefaultLogDirectory = "Log";
  36. // 启动内存回收(每小时)
  37. timer1.Interval = 1000 * 60 * 60;
  38. timer1.Start();
  39. }
  40. #region 事件
  41. private void ConfigMain_Load(object sender, EventArgs e)
  42. {
  43. try
  44. {
  45. GetPublishConfig();
  46. GetProgramSetting();
  47. // 获取数据库配置信息
  48. GetDBSetting();
  49. GetLocalIPAddress();
  50. GetInternetIPAddress();
  51. this.butSopt.Enabled = false;
  52. this.tsmiStop.Enabled = false;
  53. }
  54. catch (Exception ex)
  55. {
  56. MessageBox.Show(ex.ToString());
  57. }
  58. }
  59. /// <summary>
  60. /// 启动服务方法
  61. /// </summary>
  62. /// <param name="sender"></param>
  63. /// <param name="e"></param>
  64. private void butStart_Click(object sender, EventArgs e)
  65. {
  66. try
  67. {
  68. Application.DoEvents();
  69. this.txtLogInfo.Text = "服务开始准备启动!" + Environment.NewLine;
  70. IsCheckInfo();
  71. SetProgramSetting();
  72. WCFStart();
  73. GetLocalIPAddress();
  74. GetInternetIPAddress();
  75. MessageBox.Show("服务器WCF服务启动成功。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  76. this.butStart.Enabled = false;
  77. this.butSopt.Enabled = true;
  78. this.tsmiStart.Enabled = false;
  79. this.tsmiStop.Enabled = true;
  80. }
  81. catch (Exception ex)
  82. {
  83. MessageBox.Show(ex.Message.ToString());
  84. LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
  85. }
  86. }
  87. /// <summary>
  88. /// 服务重新启动方法
  89. /// </summary>
  90. /// <param name="sender"></param>
  91. /// <param name="e"></param>
  92. private void butReStart_Click(object sender, EventArgs e)
  93. {
  94. try
  95. {
  96. Application.DoEvents();
  97. this.txtLogInfo.Text = "服务开始准备重新启动。" + Environment.NewLine;
  98. IsCheckInfo();
  99. SetProgramSetting();
  100. WCFStart();
  101. GetLocalIPAddress();
  102. GetInternetIPAddress();
  103. }
  104. catch (Exception ex)
  105. {
  106. MessageBox.Show(ex.Message.ToString());
  107. LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
  108. }
  109. }
  110. /// <summary>
  111. /// 服务停止方法
  112. /// </summary>
  113. /// <param name="sender"></param>
  114. /// <param name="e"></param>
  115. private void butSopt_Click(object sender, EventArgs e)
  116. {
  117. try
  118. {
  119. Application.DoEvents();
  120. this.txtLogInfo.Text = "服务开始准备关闭。" + Environment.NewLine;
  121. WCFStop();
  122. MessageBox.Show("服务器WCF服务停止成功。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  123. this.butStart.Enabled = true;
  124. this.butSopt.Enabled = false;
  125. this.tsmiStart.Enabled = true;
  126. this.tsmiStop.Enabled = false;
  127. }
  128. catch (Exception ex)
  129. {
  130. MessageBox.Show(ex.Message.ToString());
  131. LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
  132. }
  133. }
  134. /// <summary>
  135. /// 显示日志方法
  136. /// </summary>
  137. /// <param name="sender"></param>
  138. /// <param name="e"></param>
  139. private void butShowLog_Click(object sender, EventArgs e)
  140. {
  141. try
  142. {
  143. var logBity = LogFileOperation.ReadFile(ConfigConst.LogFileName);
  144. this.txtLogInfo.Text = System.Text.Encoding.UTF8.GetString(logBity);
  145. }
  146. catch (Exception ex)
  147. {
  148. MessageBox.Show(ex.ToString());
  149. }
  150. }
  151. /// <summary>
  152. /// 清空日志方法
  153. /// </summary>
  154. /// <param name="sender"></param>
  155. /// <param name="e"></param>
  156. private void butClearLog_Click(object sender, EventArgs e)
  157. {
  158. try
  159. {
  160. this.txtLogInfo.Clear();
  161. LogFileOperation.DeleteLogFile(ConfigConst.LogFileName);
  162. }
  163. catch (Exception ex)
  164. {
  165. MessageBox.Show(ex.ToString());
  166. }
  167. }
  168. /// <summary>
  169. /// 关闭窗体
  170. /// </summary>
  171. /// <param name="sender"></param>
  172. /// <param name="e"></param>
  173. private void butClose_Click(object sender, EventArgs e)
  174. {
  175. //DialogResult exitDialogResult = MessageBox.Show("退出本服务程序,服务器的WCF服务将会停止。\r\n确定要退出本服务程序吗?",
  176. // this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
  177. //if (exitDialogResult == DialogResult.Yes)
  178. //{
  179. // if (this.ServiceHosts != null)
  180. // {
  181. // DialogResult dialogResult = MessageBox.Show("WCF服务正在运行中,是否强行停止?",
  182. // this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
  183. // if (dialogResult == DialogResult.Yes)
  184. // {
  185. // this.WCFStop();
  186. // }
  187. // else
  188. // {
  189. // return;
  190. // }
  191. // }
  192. // this._isClose = true;
  193. // this.Close();
  194. //}
  195. //else
  196. //{
  197. // return;
  198. //}
  199. if (this.ServiceHosts != null)
  200. {
  201. DialogResult dialogResult = MessageBox.Show("WCF服务正在运行中,退出后服务将会停止。\r\n确定要退出本服务程序?",
  202. this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
  203. if (dialogResult == DialogResult.OK)
  204. {
  205. this.WCFStop();
  206. this._isClose = true;
  207. this.Close();
  208. }
  209. else
  210. {
  211. return;
  212. }
  213. }
  214. else
  215. {
  216. DialogResult exitDialogResult = MessageBox.Show("确定要退出本服务程序?",
  217. this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
  218. if (exitDialogResult == DialogResult.OK)
  219. {
  220. this._isClose = true;
  221. this.Close();
  222. }
  223. else
  224. {
  225. return;
  226. }
  227. }
  228. }
  229. /// <summary>
  230. /// 窗体关闭验证
  231. /// </summary>
  232. /// <param name="sender"></param>
  233. /// <param name="e"></param>
  234. private void ConfigMain_FormClosing(object sender, FormClosingEventArgs e)
  235. {
  236. if (this._isClose)
  237. {
  238. if (this.ServiceHosts != null)
  239. {
  240. e.Cancel = true;
  241. this._isClose = false;
  242. MessageBox.Show("请先停止WCF服务,然后再退出本程序。");
  243. this.Visible = true;
  244. this.WindowState = FormWindowState.Normal;
  245. }
  246. else
  247. {
  248. e.Cancel = false;
  249. this.notifyIcon1.Visible = false;
  250. }
  251. }
  252. else
  253. {
  254. e.Cancel = true;
  255. this.WindowState = FormWindowState.Minimized;
  256. this.Visible = false;
  257. }
  258. }
  259. /// <summary>
  260. /// 保存数据库配置
  261. /// </summary>
  262. /// <param name="sender"></param>
  263. /// <param name="e"></param>
  264. private void btnDBSave_Click(object sender, EventArgs e)
  265. {
  266. try
  267. {
  268. INIUtility.Instance(INIUtility.IniFile.Config).WriteIniData("DBSetting", "DBIP", this.txtDBIP.Text.Trim());
  269. INIUtility.Instance(INIUtility.IniFile.Config).WriteIniData("DBSetting", "DBPort", this.txtDBPort.Text.Trim());
  270. INIUtility.Instance(INIUtility.IniFile.Config).WriteIniData("DBSetting", "SID", this.txtSID.Text.Trim());
  271. INIUtility.Instance(INIUtility.IniFile.Config).WriteIniData("DBSetting", "DBUser", this.txtUser.Text.Trim());
  272. //INIUtility.Instance(INIUtility.IniFile.Config).WriteIniData("DBSetting", "DBPassword", this.txtPassword.Text.Trim());
  273. try
  274. {
  275. string password = Encryption.EncryptDES(this.txtPassword.Text.Trim(), Constant.S_ENCRYPTION_KEY);
  276. INIUtility.Instance(INIUtility.IniFile.Config).WriteIniData("DBSetting", "DBPassword", password);
  277. }
  278. catch
  279. {
  280. }
  281. // 更改连接串
  282. DataManager.ConnectionString = string.Format(DataManager.ConnectionStringFormat,
  283. this.txtDBIP.Text.Trim(),
  284. this.txtDBPort.Text.Trim(),
  285. this.txtSID.Text.Trim(),
  286. this.txtUser.Text.Trim(),
  287. this.txtPassword.Text.Trim()
  288. );
  289. MessageBox.Show("数据库配置保存成功。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  290. }
  291. catch (Exception ex)
  292. {
  293. MessageBox.Show("数据库配置保存失败。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  294. LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
  295. }
  296. }
  297. /// <summary>
  298. /// 连接测试
  299. /// </summary>
  300. /// <param name="sender"></param>
  301. /// <param name="e"></param>
  302. private void btnDBTest_Click(object sender, EventArgs e)
  303. {
  304. #region 校验
  305. if (string.IsNullOrEmpty(this.txtDBIP.Text.Trim()))
  306. {
  307. MessageBox.Show("请输入数据库IP地址!");
  308. this.txtDBIP.Focus();
  309. return;
  310. }
  311. else if (string.IsNullOrEmpty(this.txtDBPort.Text.Trim()))
  312. {
  313. MessageBox.Show("请输入数据库端口号!");
  314. this.txtDBPort.Focus();
  315. return;
  316. }
  317. else if (string.IsNullOrEmpty(this.txtSID.Text.Trim()))
  318. {
  319. MessageBox.Show("请输入数据库SID!");
  320. this.txtSID.Focus();
  321. return;
  322. }
  323. else if (string.IsNullOrEmpty(this.txtUser.Text.Trim()))
  324. {
  325. MessageBox.Show("请输入用户名!");
  326. this.txtUser.Focus();
  327. return;
  328. }
  329. else if (string.IsNullOrEmpty(this.txtPassword.Text.Trim()))
  330. {
  331. MessageBox.Show("请输入密码!");
  332. this.txtPassword.Focus();
  333. return;
  334. }
  335. string strIpRegEdp = @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$";
  336. Regex regEdpIP = new Regex(strIpRegEdp, RegexOptions.None);
  337. if (!regEdpIP.IsMatch(this.txtDBIP.Text.Trim()))
  338. {
  339. MessageBox.Show("数据库IP地址不符合IP规则!");
  340. this.txtDBIP.Focus();
  341. return;
  342. }
  343. #endregion
  344. // 连接串
  345. string conStr = string.Format(DataManager.ConnectionStringFormat,
  346. this.txtDBIP.Text.Trim(),
  347. this.txtDBPort.Text.Trim(),
  348. this.txtSID.Text.Trim(),
  349. this.txtUser.Text.Trim(),
  350. this.txtPassword.Text.Trim()
  351. );
  352. IDBConnection oracleConn = ClsDbFactory.CreateDBConnection(DataBaseType.ORACLE, conStr);
  353. try
  354. {
  355. oracleConn.Open();
  356. string sql = "select 1 from dual";
  357. DataSet returnDataSet = oracleConn.GetSqlResultToDs(sql);
  358. DialogResult dialogResult = MessageBox.Show("数据库连接测试成功。\r\n是否保存数据库配置?",
  359. this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  360. if (DialogResult.Yes == dialogResult)
  361. {
  362. this.btnDBSave_Click(sender, e);
  363. }
  364. }
  365. catch (Exception ex)
  366. {
  367. MessageBox.Show("数据库连接测试失败。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  368. LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
  369. }
  370. finally
  371. {
  372. if (oracleConn.ConnState == ConnectionState.Open)
  373. {
  374. oracleConn.Close();
  375. }
  376. }
  377. }
  378. #endregion
  379. #region 自定义方法
  380. /// <summary>
  381. /// 获取系统配置相关信息
  382. /// </summary>
  383. public void GetPublishConfig()
  384. {
  385. try
  386. {
  387. ConfigConst.StartupPath = System.Windows.Forms.Application.StartupPath;
  388. #region 获取对应的配置信息
  389. ConfigConst.LogControl = false;
  390. //if (System.Configuration.ConfigurationManager.AppSettings["LogControl"] != null)
  391. //{
  392. // ConfigConst.LogControl = System.Configuration.ConfigurationManager.AppSettings["LogControl"].ToString().Trim().ToUpper().Equals("TRUE") ? true : false;
  393. ConfigConst.LogControl = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("LogSetting", "LogControl").ToUpper().Equals("TRUE") ? true : false;
  394. //}
  395. ///日志文件
  396. //if (System.Configuration.ConfigurationManager.AppSettings["LogFileName"] != null)
  397. //{
  398. // ConfigConst.LogFileName = System.Configuration.ConfigurationManager.AppSettings["LogFileName"].Trim();
  399. ConfigConst.LogFileName = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("LogSetting", "LogFileName");
  400. //}
  401. //配置文件名称
  402. //if (System.Configuration.ConfigurationManager.AppSettings["ConfigFileName"] != null)
  403. //{
  404. // ConfigConst.ConfigFileName = System.Configuration.ConfigurationManager.AppSettings["ConfigFileName"].Trim();
  405. //}
  406. #endregion
  407. }
  408. catch (Exception ex)
  409. {
  410. throw ex;
  411. }
  412. }
  413. /// <summary>
  414. /// 获取程序所需要的配置信息并显示在对应的控件中
  415. /// </summary>
  416. public void GetProgramSetting()
  417. {
  418. this.txtWcfIp.Text = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("WCFSetting", "WCFIP");//System.Configuration.ConfigurationManager.AppSettings["WCFIP"].Trim();
  419. this.txtWcfPort.Text = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("WCFSetting", "WCFPort");//System.Configuration.ConfigurationManager.AppSettings["WCFPort"].Trim();
  420. this.txtServerName.Text = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("WCFSetting", "ServerName");
  421. string v = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("VersionSetting", "ClientVersion");
  422. this.Text = string.Format(this._formTitle, this.txtServerName.Text, v);
  423. this.notifyIcon1.Text = "【" + this.txtServerName.Text + "】" + Constant.M_SYSTEM_NAME + "-" + v;//this.Text;
  424. }
  425. /// <summary>
  426. /// 根据XML节点名称获取对应的数据
  427. /// </summary>
  428. /// <param name="pXMLOperate"></param>
  429. /// <param name="pNodeName"></param>
  430. /// <returns></returns>
  431. public string GetXElementName(XDocument pXDocument, string pNodeName)
  432. {
  433. try
  434. {
  435. XElement xelemnt = (from p in pXDocument.Root.Elements() where p.Name == pNodeName select p).FirstOrDefault();
  436. if (xelemnt == null)
  437. return "";
  438. return xelemnt.Value;
  439. }
  440. catch (Exception ex)
  441. {
  442. LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
  443. throw ex;
  444. }
  445. }
  446. /// <summary>
  447. /// 获取XML中对应的节点
  448. /// </summary>
  449. /// <param name="pXDocument"></param>
  450. /// <param name="pNodeName"></param>
  451. /// <returns></returns>
  452. public XElement GetFirstNode(XDocument pXDocument, string pNodeName)
  453. {
  454. try
  455. {
  456. XElement xelemnt = (from p in pXDocument.Root.Elements() where p.Name == pNodeName select p).FirstOrDefault();
  457. return xelemnt;
  458. }
  459. catch (Exception ex)
  460. {
  461. LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
  462. throw ex;
  463. }
  464. }
  465. /// <summary>
  466. /// 保存数据的验证处理
  467. /// </summary>
  468. public void IsCheckInfo()
  469. {
  470. string strWcfIp = this.txtWcfIp.Text.Trim();
  471. if (string.IsNullOrEmpty(strWcfIp))
  472. {
  473. throw new Exception("WCF服务的主机IP地址不能为空。");
  474. }
  475. string strIpRegEdp = @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$";
  476. Regex regEdpIP = new Regex(strIpRegEdp, RegexOptions.None);
  477. if (!regEdpIP.IsMatch(strWcfIp))
  478. {
  479. throw new Exception("WCF服务的主机IP地址不符合IP地址规则。");
  480. }
  481. string strWcfPort = this.txtWcfPort.Text.Trim();
  482. if (string.IsNullOrEmpty(strWcfPort))
  483. {
  484. throw new Exception("WCF服务的主机端口号不能为空。");
  485. }
  486. var strPortRegEdp = @"/^[1-9]$|(^[1-9][0-9]$)|(^[1-9][0-9][0-9]$)|(^[1-9][0-9][0-9][0-9]$)|(^[1-6][0-5][0-5][0-3][0-5]$)/";
  487. Regex regEdpPort = new Regex(strPortRegEdp, RegexOptions.None);
  488. if (!regEdpPort.IsMatch(strWcfPort))
  489. {
  490. throw new Exception("WCF服务的主机端口号不符合端口号规则。");
  491. }
  492. }
  493. /// <summary>
  494. /// 保存设置到XML文件
  495. /// </summary>
  496. public void SetProgramSetting()
  497. {
  498. INIUtility.Instance(INIUtility.IniFile.Config).WriteIniData("WCFSetting", "WCFIP", this.txtWcfIp.Text.Trim());
  499. INIUtility.Instance(INIUtility.IniFile.Config).WriteIniData("WCFSetting", "WCFPort", this.txtWcfPort.Text.Trim());
  500. }
  501. /// <summary>
  502. /// 添加数据到XML集合中
  503. /// </summary>
  504. /// <param name="pXMLOperate"></param>
  505. /// <param name="pNodeName"></param>
  506. /// <param name="pNameValue"></param>
  507. public void SetXElementName(XDocument pXDocument, string pNodeName, string pNameValue)
  508. {
  509. try
  510. {
  511. XElement xmlElem = GetFirstNode(pXDocument, pNodeName);
  512. if (xmlElem != null)
  513. {
  514. xmlElem.Value = pNameValue;
  515. }
  516. else
  517. {
  518. XElement tempXElem = new XElement(pNodeName);
  519. tempXElem.Value = pNameValue;
  520. pXDocument.Root.Add(tempXElem);
  521. }
  522. }
  523. catch (Exception ex)
  524. {
  525. LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
  526. throw ex;
  527. }
  528. }
  529. public void WCFStart()
  530. {
  531. try
  532. {
  533. if (this.ServiceHosts != null)
  534. {
  535. this.ServiceHosts.Dispose();
  536. this.ServiceHosts = null;
  537. }
  538. this.ServiceHosts = new ServiceHostCollection();
  539. this.ServiceHosts.IP = this.txtWcfIp.Text.Trim();
  540. this.ServiceHosts.Port = this.txtWcfPort.Text.Trim();
  541. this.ServiceHosts.Init();
  542. foreach (ServiceHost host in ServiceHosts)
  543. {
  544. host.Opening += host_Opening;
  545. host.Opened += host_Opened;
  546. host.Closing += host_Closing;
  547. host.Closed += host_Closed;
  548. }
  549. ServiceHostOpen();
  550. }
  551. catch (Exception ex)
  552. {
  553. LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
  554. throw ex;
  555. }
  556. }
  557. public void ServiceHostOpen()
  558. {
  559. try
  560. {
  561. foreach (ServiceHost host in ServiceHosts)
  562. {
  563. //
  564. //可以增加对控制
  565. //
  566. host.Open();
  567. }
  568. }
  569. catch (Exception ex)
  570. {
  571. LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
  572. throw ex;
  573. }
  574. }
  575. public void WCFStop()
  576. {
  577. try
  578. {
  579. if (this.ServiceHosts != null)
  580. {
  581. this.ServiceHosts.Dispose();
  582. this.ServiceHosts = null;
  583. }
  584. }
  585. catch (Exception ex)
  586. {
  587. LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
  588. throw ex;
  589. }
  590. }
  591. void host_Opening(object sender, EventArgs e)
  592. {
  593. SendOrPostCallback callback = delegate(object state)
  594. {
  595. string strInfo = "服务" + (sender as ServiceHost).BaseAddresses[0] + "打开中......";
  596. LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
  597. this.txtLogInfo.Text += strInfo + Environment.NewLine;
  598. };
  599. _syncContext.Post(callback, null);
  600. }
  601. void host_Opened(object sender, EventArgs e)
  602. {
  603. SendOrPostCallback callback = delegate(object state)
  604. {
  605. string strInfo = "服务" + (sender as ServiceHost).BaseAddresses[0] + "打开完毕。";
  606. LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
  607. this.txtLogInfo.Text += strInfo + Environment.NewLine;
  608. };
  609. _syncContext.Post(callback, null);
  610. }
  611. void host_Closed(object sender, EventArgs e)
  612. {
  613. SendOrPostCallback callback = delegate(object state)
  614. {
  615. string strInfo = "服务" + (sender as ServiceHost).BaseAddresses[0] + "关闭完毕。";
  616. LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
  617. this.txtLogInfo.Text += strInfo + Environment.NewLine;
  618. };
  619. _syncContext.Post(callback, null);
  620. }
  621. void host_Closing(object sender, EventArgs e)
  622. {
  623. SendOrPostCallback callback = delegate(object state)
  624. {
  625. string strInfo = "服务" + (sender as ServiceHost).BaseAddresses[0] + "关闭中......";
  626. LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
  627. this.txtLogInfo.Text += strInfo + Environment.NewLine;
  628. };
  629. _syncContext.Post(callback, null);
  630. }
  631. /// <summary>
  632. /// 服务开始
  633. /// </summary>
  634. /// <param name="sender"></param>
  635. /// <param name="e"></param>
  636. void DKIMSSWCFService_ServiceDoingEvent(object sender, ServiceEventArgs e)
  637. {
  638. SendOrPostCallback callback = delegate(object state)
  639. {
  640. string strInfo = "服务" + e.MethodName + " 服务开始。 ";
  641. LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
  642. };
  643. _syncContext.Post(callback, null);
  644. }
  645. /// <summary>
  646. /// 服务结束
  647. /// </summary>
  648. /// <param name="sender"></param>
  649. /// <param name="e"></param>
  650. void DKIMSSWCFService_ServiceDoneEvent(object sender, ServiceEventArgs e)
  651. {
  652. SendOrPostCallback callback = delegate(object state)
  653. {
  654. string strInfo = "服务" + e.MethodName + " 服务结束。 ";
  655. LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
  656. };
  657. _syncContext.Post(callback, null);
  658. }
  659. /// <summary>
  660. /// 服务异常
  661. /// </summary>
  662. /// <param name="sender"></param>
  663. /// <param name="e"></param>
  664. void DKIMSSWCFService_ServiceExceptionEvent(object sender, ServiceEventArgs e)
  665. {
  666. //_syncContext.Post(state => txtDoing.Text += e.MethodName + " has Exception ... " + "\r\n", null);
  667. SendOrPostCallback callback = delegate(object state)
  668. {
  669. string strInfo = "服务" + e.MethodName + " 服务异常。 ";
  670. LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
  671. };
  672. _syncContext.Post(callback, null);
  673. }
  674. /// <summary>
  675. /// 获取数据库配置
  676. /// </summary>
  677. private void GetDBSetting()
  678. {
  679. this.txtDBIP.Text = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("DBSetting", "DBIP");
  680. this.txtDBPort.Text = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("DBSetting", "DBPort");
  681. this.txtSID.Text = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("DBSetting", "SID");
  682. this.txtUser.Text = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("DBSetting", "DBUser");
  683. this.txtPassword.Text = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("DBSetting", "DBPassword");
  684. //try
  685. //{
  686. // this.txtPassword.Text = Encryption.DecryptDES(this.txtPassword.Text.Trim(), Constant.SECRET_KEY);
  687. //}
  688. //catch
  689. //{
  690. //}
  691. string password = null;
  692. try
  693. {
  694. password = Encryption.DecryptDES(this.txtPassword.Text.Trim(), Constant.S_ENCRYPTION_KEY);
  695. }
  696. catch
  697. {
  698. }
  699. this.txtPassword.Text = password;
  700. // 更改连接串
  701. DataManager.ConnectionString = string.Format(DataManager.ConnectionStringFormat,
  702. this.txtDBIP.Text.Trim(),
  703. this.txtDBPort.Text.Trim(),
  704. this.txtSID.Text.Trim(),
  705. this.txtUser.Text.Trim(),
  706. this.txtPassword.Text.Trim()
  707. );
  708. }
  709. /// <summary>
  710. /// 内网地址
  711. /// </summary>
  712. private void GetLocalIPAddress()
  713. {
  714. string localIP = null;
  715. try
  716. {
  717. IPHostEntry ipHost = System.Net.Dns.GetHostEntry(Dns.GetHostName());// Dns.Resolve(Dns.GetHostName()); ;
  718. localIP = "unknown";
  719. foreach (var item in ipHost.AddressList)
  720. {
  721. localIP = item.ToString();
  722. //判断IP是否合法
  723. if (System.Text.RegularExpressions.Regex.IsMatch(localIP, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
  724. {
  725. break;
  726. }
  727. }
  728. this.lblLocalIPAddress.Text = localIP;
  729. }
  730. catch
  731. {
  732. this.lblLocalIPAddress.Text = "unknown";
  733. }
  734. }
  735. /// <summary>
  736. /// 外网地址
  737. /// </summary>
  738. private void GetInternetIPAddress()
  739. {
  740. //string internetAddress = "";
  741. //try
  742. //{
  743. // using (WebClient webClient = new WebClient())
  744. // {
  745. // internetAddress = webClient.DownloadString("http://www.coridc.com/ip");//从外部网页获得IP地址
  746. // //判断IP是否合法
  747. // if (!System.Text.RegularExpressions.Regex.IsMatch(internetAddress, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
  748. // {
  749. // internetAddress = webClient.DownloadString("http://fw.qq.com/ipaddress");//从腾讯提供的API中获得IP地址
  750. // }
  751. // }
  752. // this.lblInternetIPAddress.Text = internetAddress;
  753. //}
  754. //catch
  755. //{
  756. // this.lblInternetIPAddress.Text = "unknown";
  757. //}
  758. this.lblInternetIPAddress.Text = "unknown";
  759. }
  760. #endregion
  761. private void notifyIcon1_DoubleClick(object sender, EventArgs e)
  762. {
  763. if (!this.Visible)
  764. {
  765. this.Visible = true;
  766. //this.WindowState = FormWindowState.Normal;
  767. }
  768. this.WindowState = FormWindowState.Normal;
  769. this.Activate();
  770. }
  771. private void 回收ToolStripMenuItem_Click(object sender, EventArgs e)
  772. {
  773. try
  774. {
  775. GC.Collect();
  776. }
  777. catch
  778. {
  779. }
  780. }
  781. private void timer1_Tick(object sender, EventArgs e)
  782. {
  783. try
  784. {
  785. GC.Collect();
  786. }
  787. catch
  788. {
  789. }
  790. }
  791. private void btnServerName_Click(object sender, EventArgs e)
  792. {
  793. INIUtility.Instance(INIUtility.IniFile.Config).WriteIniData("WCFSetting", "ServerName", this.txtServerName.Text.Trim());
  794. string v = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("VersionSetting", "ClientVersion");
  795. this.Text = string.Format(this._formTitle, this.txtServerName.Text, v);
  796. this.notifyIcon1.Text = "【" + this.txtServerName.Text + "】" + Constant.M_SYSTEM_NAME + "-" + v;//this.Text;
  797. }
  798. private void btnOpen_Click(object sender, EventArgs e)
  799. {
  800. System.Diagnostics.Process.Start(txtExePath.Text);
  801. }
  802. private void button1_Click(object sender, EventArgs e)
  803. {
  804. F_LicCode lc = new F_LicCode(this.txtSID.Text);
  805. lc.ShowDialog();
  806. }
  807. }
  808. }