Self-made gadgets monitor whether the wcf Service is normal and self-made monitor the wcf Service
Because there are two wcf services in the project that use netTcpBinding, they often fail to provide services, and the failure to find the cause is serious. Changing InstanceContextMode and ConcurrencyMode configuration is not good, after a problem occurs, the user gives feedback, and then we can restart the service quickly. Therefore, we need to write a small tool to automatically monitor whether the server program is normal. If not, the service program can be restarted automatically, this method can only cure the symptoms and never cure the problem. You can give some debugging comments to those who know about wcf.
The program uses the chartcontrol control of devexpress to scroll through the fact page, as shown in the figure:
The line chart displays 50 points each time, and then dynamically deletes and adds points to maintain 50 points. Then, each access interface determines whether access is allowed and whether access times out. If the number of access times reaches the configured limit, directly execute an external program to restart the wcf, and then wait for a while to continue monitoring:
Load the configuration and start the task:
1 private void FrmMain_Load (object sender, EventArgs e) 2 {3 DataBaseTaskScanningMessage = new EventWaitHandle (false, EventResetMode. autoReset); 4 p1 = this. chartControl1.Series [0]. points; 5 p2 = this. chartControl1.Series [1]. points; 6 7 _ appFilePath = ReportInfo. monitor. m. default. _ appFilePath; 8 _ pointCount = m. default. _ pointCount; 9 _ taskTimeSpan = m. default. _ taskTimeSpan; 10 _ timeOutSpan = m. defa Ult. _ timeOutSpan; 11 _ ntsdPath = m. default. _ ntsdPath; 12 13 Run (); 14} 15 16 private void Run () 17 {18 errorCount = 0; 19 20 t = new Thread (() => 21 {22 try23 {24 ShowMess ("Run ...... "); 25 taskWait = ThreadPool. unsafeRegisterWaitForSingleObject (DataBaseTaskScanningMessage, (x, e) => RunSc (), null, _ taskTimeSpan, true); 26 this. btn_start.Invoke (MethodInvoker) () => 27 {28 this. btn_start.Enabled = false; 29 This. btn_stop.Enabled = true; 30}); 31 32} 33 catch (Exception ex) 34 {35 ShowMess ("Service Startup Exception:" + ex. toString (); 36 Log. logService. logError (string. format ("Service Startup exception: {0 }! ", Ex. toString (), "btn_start_Click"); 37 38 39 this. btn_start.Invoke (MethodInvoker) () => 40 {41 this. btn_start.Enabled = true; 42 this. btn_stop.Enabled = false; 43}); 44} 45}); 46 t. isBackground = true; 47 t. start (); 48}
If the core task is 3 times longer, a rest.txt file will be placed. When the server reads this file, it will commit suicide and then be reborn. This is to solve the problem that the monitoring program has no permission to terminate the process when the server executes the system permission through the task plan:
1 # region scheduled task 2 3 public static WaitHandle DataBaseTaskScanningMessage {get; private set;} 4 private static RegisteredWaitHandle taskWait; 5 6 public void RunSc () 7 {8 try 9 {10 string dt = DateTime. now. toString ("mm-ss"); 11 Stopwatch sw = new Stopwatch (); 12 sw. start (); 13 bool state = (new IMServerAgent ()). connectionTest (); 14 sw. stop (); 15 16 ShowMess (string. format ("execution interface: {0} result: {1} Time: {2}", "Mess Age ", state? "Successful": "= Failed ====", sw. ElapsedMilliseconds ),! State); 17 18 if (sw. elapsedMilliseconds> _ timeOutSpan) 19 state = false; 20 21 22 if (p1.Count> _ pointCount) 23 p1.RemoveAt (0); 24 p1.Add (new DevExpress. xtraCharts. seriesPoint (dt, sw. elapsedMilliseconds); 25 26 sw. restart (); 27 bool state2 = (new ClientLogInfoAgent ()). addErrorLog ("RunSc", "Monitor"); 28 sw. stop (); 29 30 31 ShowMess (string. format ("execution interface: {0} result: {1} Time: {2}", "Log", state2? "Successful": "= Failed ======", sw. ElapsedMilliseconds ),! State); 32 33 if (sw. elapsedMilliseconds> _ timeOutSpan) 34 state2 = false; 35 36 if (p2.Count> _ pointCount) 37 p2.RemoveAt (0); 38 p2.Add (new DevExpress. xtraCharts. seriesPoint (dt, sw. elapsedMilliseconds); 39 40 41 # Check whether region has an exception 42 43 if (! State |! State2) 44 Interlocked. increment (ref errorCount); 45 else46 Interlocked. exchange (ref errorCount, 0); 47 48 if (errorCount> = 3) 49 {50 Interlocked. exchange (ref errorCount, 0); 51 // restart the service with an exception 52 if (File. exists (_ appFilePath) 53 {54 ShowMess ("rest ...... ", true); 55 56 57 string path = @" D: \ ReportServer \ WebHelp \ rest.txt "; 58 if (! System. IO. File. Exists (path) 59 {60 if (! System. IO. directory. exists (Path. getDirectoryName (path) 61 System. IO. directory. createDirectory (Path. getDirectoryName (path); 62 System. IO. file. writeAllText (path, DateTime. now. toString (); 63} 64 65 System. diagnostics. process. start (_ appFilePath); 66 67 System. threading. thread. sleep (5*1000); 68 int wCount = 0; 69 while (Process. getProcessesByName ("ReportInfo. IMServer "). count () = 0) 70 {71 wCount ++; 72 if (wCount> 100) 73 break; 74 System. threading. thread. sleep (1000); 75} 76 77} 78} 79 80 # endregion81 82 83 84} 85 catch (Exception ex) 86 {87 p1.Clear (); 88 p2.Clear (); 89 ShowMess ("RunSc exception:" + ex. toString (); 90} 91 finally92 {93 taskWait = ThreadPool. unsafeRegisterWaitForSingleObject (DataBaseTaskScanningMessage, (x, e) => RunSc (), null, _ taskTimeSpan, true); 94} 95} 96 97 # endregion
After all, the problem cannot be solved. It can only be discovered immediately when a problem exists, so that the user cannot feel the problem.