ConfigMain.cs 36 KB

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