ConfigMain.cs 34 KB

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