ConfigMain.cs 34 KB

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