由于项目中有2个使用netTcpBinding的wcf服务经常出现无法提供服务的问题,一直找原因也找不到导致影响严重,更换InstanceContextMode和ConcurrencyMode配置也不好使,出现问题后用户反馈,然后我这边在赶紧重启服务就好了,为此先写个小工具实现自动监控服务端程序是否正常如果不正常则实现自动重新启动服务程序功能,这方法只能治标不治本,对wcf了解的朋友可以给出些调试意见。
程序使用devexpress的chartcontrol控件事实滚动界面,看图:
折线图每次显示50个点,然后动态删除和新增点保持50个点,然后每次访问接口判断是否可以访问,以及是否访问超时,如果达到设置的次数则,直接执行外部程序实现重启wcf目的,然后等待一会后继续监控:
加载配置,启动任务:
private void FrmMain_Load(object sender, EventArgs e)
{
DataBaseTaskScanningMessage = new EventWaitHandle(false, EventResetMode.AutoReset);
p1 = this.chartControl1.Series[].Points;
p2 = this.chartControl1.Series[].Points; _appFilePath = ReportInfo.Monitor.m.Default._appFilePath;
_pointCount = m.Default._pointCount;
_taskTimeSpan = m.Default._taskTimeSpan;
_timeOutSpan = m.Default._timeOutSpan;
_ntsdPath = m.Default._ntsdPath; Run();
} private void Run()
{
errorCount = ; t = new Thread(() =>
{
try
{
ShowMess("Run......");
taskWait = ThreadPool.UnsafeRegisterWaitForSingleObject(DataBaseTaskScanningMessage, (x, e) => RunSc(), null, _taskTimeSpan, true);
this.btn_start.Invoke((MethodInvoker)(() =>
{
this.btn_start.Enabled = false;
this.btn_stop.Enabled = true;
})); }
catch (Exception ex)
{
ShowMess("服务启动异常:" + ex.ToString());
Log.LogService.LogError(string.Format("服务启动异常:{0}!", ex.ToString()), "btn_start_Click"); this.btn_start.Invoke((MethodInvoker)(() =>
{
this.btn_start.Enabled = true;
this.btn_stop.Enabled = false;
}));
}
});
t.IsBackground = true;
t.Start();
}
核心任务,超过3次则放置一个rest.txt文件,当服务端读取到这个文件存在时就会自杀然后重生,这是为了解决当服务端是通过任务计划system权限执行时,监控程序无权限结束进程的问题:
#region 定时任务 public static WaitHandle DataBaseTaskScanningMessage { get; private set; }
private static RegisteredWaitHandle taskWait; public void RunSc()
{
try
{
string dt = DateTime.Now.ToString("mm-ss");
Stopwatch sw = new Stopwatch();
sw.Start();
bool state = (new IMServerAgent()).ConnectionTest();
sw.Stop(); ShowMess(string.Format("执行接口:{0} 结果:{1} 用时:{2}", "Message", state ? "成功" : "=失败=====", sw.ElapsedMilliseconds), !state); if (sw.ElapsedMilliseconds > _timeOutSpan)
state = false; if (p1.Count > _pointCount)
p1.RemoveAt();
p1.Add(new DevExpress.XtraCharts.SeriesPoint(dt, sw.ElapsedMilliseconds)); sw.Restart();
bool state2 = (new ClientLogInfoAgent()).AddErrorLog("RunSc", "Monitor");
sw.Stop(); ShowMess(string.Format("执行接口:{0} 结果:{1} 用时:{2}", "Log ", state2 ? "成功" : "=失败======", sw.ElapsedMilliseconds), !state); if (sw.ElapsedMilliseconds > _timeOutSpan)
state2 = false; if (p2.Count > _pointCount)
p2.RemoveAt();
p2.Add(new DevExpress.XtraCharts.SeriesPoint(dt, sw.ElapsedMilliseconds)); #region 判读是否出现异常 if (!state || !state2)
Interlocked.Increment(ref errorCount);
else
Interlocked.Exchange(ref errorCount, ); if (errorCount >= )
{
Interlocked.Exchange(ref errorCount, );
//异常重启服务
if (File.Exists(_appFilePath))
{
ShowMess("rest......", true); string path = @"D:\ReportServer\WebHelp\rest.txt";
if (!System.IO.File.Exists(path))
{
if (!System.IO.Directory.Exists(Path.GetDirectoryName(path)))
System.IO.Directory.CreateDirectory(Path.GetDirectoryName(path));
System.IO.File.WriteAllText(path, DateTime.Now.ToString());
} System.Diagnostics.Process.Start(_appFilePath); System.Threading.Thread.Sleep( * );
int wCount = ;
while (Process.GetProcessesByName("ReportInfo.IMServer").Count() == )
{
wCount++;
if (wCount > )
break;
System.Threading.Thread.Sleep();
} }
} #endregion }
catch (Exception ex)
{
p1.Clear();
p2.Clear();
ShowMess("RunSc异常:" + ex.ToString());
}
finally
{
taskWait = ThreadPool.UnsafeRegisterWaitForSingleObject(DataBaseTaskScanningMessage, (x, e) => RunSc(), null, _taskTimeSpan, true);
}
} #endregion
毕竟不能根本解决问题,只能起到当有问题时第一时间发现,让用户感觉不到问题的存在。