ConfigMain.cs 35 KB

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