ConfigMain.cs 32 KB

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