一、引言
在软件开发过程中windows服务有的时候非常有用,用于同步数据,发送邮件,宿主WF引擎服务等,但是快速搭建一个好用多线程进行多任务处理的程序往往是一个项目必须考虑的问题。自己在项目中也经常碰到类似的问题,但是一直没有做过这方面总结,每次需要相关windows服务,也重头写一次。自己几乎没有写博客的习惯,前不久看到博客园中有篇关于windows服务的框架写的非常好(抱歉,当时看完以后忘记收藏),感觉自己这些年对它也有一定的认识,但是很少拿出来和大家分享。其实大家都知道通过分享才能找到问题并能提高代码的质量。经过国庆期间的初步整理一个windows服务快速实现的解决方案终于调试通过,当然里面也有一定的问题,希望大伙能指出,谢谢。
二、通用Windows服务接口
定义ICommand作为windows service服务接口,所有自定义的服务都应实现ICommand
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA3QAAACsCAIAAAC8d3kzAAAKjUlEQVR4nO3dba6jOBqA0eynN5ktzEpmifSPKyG3P14MMcSQc1QqEceAUzNSPTKV268FAAAGeX17AQAAPIe4BABgGHEJAMAw4hIAgGHEJQAAw4hLAACGEZcAAAwjLgEAGEZcAgAwjLgEAGCYj+Ly/X5/MggAwMMcj8uTynLmDJ15bQAAM5grLuevt/lXCADwRQfj8jfL8s9d1gkAcL1Z4rJ17mp9mb6VzcnezQazt7KR6jWr8/s/FADArzkSl5eVZXkcDKa/x4PV4yAffUUJAKDTFHEZnN7TfP1xmV62Mx+VJQBAv91xeUZZxvOD7cb0oDMuN3cxs8HyLWUJANAyUVwu3R1ZHfzwsfjmeuKZAAAse+Py1LLMTnwnsgmlJenI1mD19OBG5adQlgAAsenich4P+AgAABc7/kPUH6y1kQkAQExcAgAwjLgEAGAYcQkAwDDikmVZlte76/8J6bTOUwCAn/JRH/zUl8fv5fV+rb+yl2UUZu9W56wzg5fxBYPLAgCPcfwv+1FlmQZQ+nJ9t39JF+TLSZ00fNnrBdM/2+q9NidUJ8fV2NmmAMDzfD8ul//2zYf9cWq+3OihcH9cpuPxnmW2FRpcUFwCwM86+Jf92Afid4zLyVVbsNWO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0PD8uz/t57Keaf4UAAKWJ4lJZZu6yTgCA1ZPjsjWt+pR8tb5M38rmZO9mg9lb2Uj1mtX5/Z8UAGASs8Tld8uyPA4G09/jwepxkI+33mcFAFgeHJd7r9nTfP1xmV62Mx+VJQDwAFPE5RllWZ0fnx5sN6YHnXG5uYuZDcZbqgAAt/DwuFy62y47Pu+x+OYi45kAADP7flyeWpbpiUHYtb5MU0qv1hqsnh7cqFybsgQAbuon4nLI6eeZdmEAAHt9Py5/WWsjEwDgpsQlAADDiEsAAIYRlwAADCMuAQAYRlxO5PV6ZQcAAPdyy4hppdfr1XxrWZZX4aTlHZOtZ7blAQD0eFrBxEmWFtvZ9bb3+uISAHiApxVMf1yebde9qpP1JQBwO9/Pl79n2X8dlR6XL6sj2XhnXKYHf8fls/Lq0/PsqXo6obxUcHprYccGAQAmMUWpZPkYDG6+3IzLakRmB63jY2f1vKsjAYBnmCJfrozL7GB9GexQts4qBzfjMtjRjK8MAHALs+TL+lg8GymP45fH/s3lJ7V3bOdy75UBAG5hlny5Pi7L/cgvPhYvR5QlAHBHExVM8DWdcqRz/L9Xy6WD63E5v3qR4MrZ+Obp61vBy81xAIAZKJWJxP+4c/MtAICvUyoAAAwjLgEAGEZcAgAwjLgEAGAYcQkAwDC747L1k3QAAOBIJopLAACqxCUAAMOISwAAhhGXAAAMczAT9SUAACU7lwAADCMuAQAYRlwCADCMuAQAYBj/hR4AAIaRiQAADCMuAQAYRlwCADCMuAQAYBhxCQDAMOISAIBh/gWln8RZeO8OggAAAABJRU5ErkJggg==" alt="" />
三、定义WindowsServiceItem对象与WindowsServiceItemCollection集合
服务的每个任务对应一个WindowsServiceItem,一个服务对应多个任务这样定义WindowsServiceItemCollection集合进行维护每个任务。其实WindowsServiceItem中实现了WindowsTimer周期属性,Type为自定义服务的类型.这里只是简单的对象定义大伙应该一看就明白。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAi4AAAJPCAIAAADUgMh3AAAgAElEQVR4nO3dzY7suJ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FU1iSIsiKJxo2goZ0fR2ul7tCja39cqakeJH55VO15WJoqwCPvmfHuUha3n6nJcVWJUMxb0XtQokqsc5VKeXP1o1a8aLDuSfXUvhjyjKuu0nq8dvDoqezmotYP2RGnMZdWql71uVoWHsWjmaVMWli/V+kYvq4b0Kimf2IfLPhfzmhM3mx97ddjbzhFRhEXcVVH5gSxTx1movuzW2RBF5Vvly3J6lXU8hTLzPN0ZNe1/GOjukXPw6stuib3vkjq3ziJaVtX3lJSzZ/XTrqk2ro5KrePpaNZCyOio9UuMfOn8KHZ/gTDGo+6mrEwUYXHzKLLZlzutl0dFkdqv2mnrl1lnGHhe2h11+zI2XxtFzt+vd0aR+ov8vCYhnH3JwlZHO3dz3hdFzg9YC1GEnR4dRXN7ioyJIrUvu83uhhv2qDsST4Oe8TivijbPxWr5qsBIjCLn7hjlh1wVdRt0jocowio3j6JuLMVEkdrRUW2evUetvo5doJvdASCfq404S7ZdFbUGcMYCnWyw9dYZC3St3ruDaUVRVZMowiI0iibtVoJJ3NGg1pTVWoVleVdr0axcrJArTvbKhrHA1erILlTbbNWctdnH7svfkVrZ2Lw8hmr9SrU8VRa2ymU1WblVUq2PzcXUKcneu+OULagdeTZXj1X50nPijNOx6gOm6h5Pddjv4k+xlG95Comie8i5KgIuTc2GeIMMw08OmCjCIuij7L9MAUZmXKZgAxbosOAbBSANUYQFUQQgDVGEBVEEIA1/rwgLoggAkKz5H2fwD7MAgBgr/gM6AADOQBQBAJIRRQCAZEQRACCZlTekEQAgAFdFAIBkRBEAIBlRBABIRhQBAJLxf1sAACQjbwAAyYgiAEAyoggAkIwoAgAkI4oAAMmIIgBAMqIIAJCMKAIAJCOKAADJiCIAQLL/Az5wlAXWNtZuAAAAAElFTkSuQmCC" alt="" />
以下是WindowsTimer的定义.
public class WindowsTimer : IDisposable
{
/// <summary>
/// 周期任务描述
/// </summary>
public string Description = string.Empty; /// <summary>
/// 时钟周期控件
/// </summary>
public Timer Timer = new Timer(); /// <summary>
/// 是否准备好了可以接受下一个周期的任务
/// </summary>
public bool Prepared = true; /// <summary>
/// Windows服务的时间控件异常回调委托
/// </summary>
public WindowsServiceTimerExceptionCallBack CallBack; /// <summary>
/// 构造函数
/// </summary>
/// <param name="description">周期任务描述</param>
/// <param name="interval">执行周期间隔毫秒数</param>
public WindowsTimer(string description, double interval)
{
Description = description;
Timer.Interval = interval;
Timer.Enabled = false;
} #region IDisposable Members /// <summary>
/// 析构函数
/// </summary>
~WindowsTimer()
{
Dispose(false);
} /// <summary>
///
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
} /// <summary>
///
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (Timer != null)
{
Timer.Dispose();
}
}
} #endregion
}
四、快速搭建Windows服务核心Windows服务类
该类继承 System.ServiceProcess.ServiceBase重写相关的基类方法。在此之前定义两个默认Command:WindowsServiceDefaultCommand和WindowsServiceEmptyCommand
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkwAAAFeCAIAAAD48NeZAAAYDUlEQVR4nO3dQZLrNpYFUO3H6+tBV/Tgb8Er8b5q2FGDnqkHrpJpAnh4pEiKenlOKBwSBAIgCeIm9ZXpxxMAinp8egAAcBYhB0BZQg6AsoQcAGUJOQDKmoTcr1+/3ikEgA+KQu6khLtzHN55bABsdXXI3T9F7j9CAJKGIfczE+5P3zJOAGLXhdxo25fXy+Vbqzqrd1eFq7dWJd02u/XzOwXAnfVD7rKEa58Hhcv/xoXd50GM+SoNQEkXhVyweSZ78iG3bDYZYxIOoKpOyJ2RcHH94PZr+SQZctO7ulVh+5aEA6jh0pB7pvOsW/jmx5XT8cQ1Afg665A7NeFWG/5aWFVoPRd5Nirsbh501O6FhAOo5AMhdx8FdgGAQPTL4IWNbuwAqOSHhhwAP4GQA6AsIQdAWReF3OPXY/V4lXcrT0tGbwU1dzi2tdu61UHLn+vdY1hOv3ZOvtn11spHtVZvrh64R1sXnGRryQo1Ts1oL+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pADoCwhB0BZQg6AsvzFEwDKEmYAlCXkAChLyAFQlpADoCwhB0BZQg6AsoQcAGUJOQDKEnIAlCXkAChLyAFQlpADoCwhB0BZ/w9sPkytX8L7YwAAAABJRU5ErkJggg==" alt="" />
核心WindowsService代码如下:
[Serializable]
public class WindowsService : System.ServiceProcess.ServiceBase
{
private bool _fHasInitServerRemotingObject;
private readonly object _fHasInitServerRemotingObjectLock = new object(); private readonly WindowsServiceItemCollection _fTimerServices = new WindowsServiceItemCollection(); private readonly object _fTimerServiceObjectsLock = new object();
private readonly Dictionary<Type, ICommand> _fTimerServiceObjects = new Dictionary<Type, ICommand>(); /// <summary>
/// 构造函数
/// </summary>
/// <param name="timerServices"></param>
public WindowsService(WindowsServiceItemCollection timerServices)
{
if (timerServices != null)
{
foreach (WindowsServiceItem item in timerServices)
{
_fTimerServices.Add(item);
}
}
} /// <summary>
/// 服务启动执行的操作
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
if (_fTimerServices.Count == )
{
var wsiEmpty = new WindowsServiceItem
{
WindowsTimer = new WindowsTimer("默认的一个Command的轮询周期,设置为5分钟。", ),
CommandType = typeof (WindowsServiceEmptyCommand)
};
_fTimerServices.Add(wsiEmpty);
} var wsi = new WindowsServiceItem
{
WindowsTimer = new WindowsTimer("默认的一个Command的轮询周期,设置为5秒钟。", ),
CommandType = typeof (WindowsServiceDefaultCommand)
};
_fTimerServices.Add(wsi); foreach (WindowsServiceItem kvp in _fTimerServices)
{
kvp.WindowsTimer.Timer.Elapsed -= Timer_Elapsed;
kvp.WindowsTimer.Timer.Elapsed += Timer_Elapsed;
kvp.WindowsTimer.Timer.Enabled = true;
} } private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
#region 获取Command对象 WindowsServiceItem wsItem = _fTimerServices.GetItemByTimer((Timer)sender);
Type commandType = wsItem.CommandType;
ICommand command = null;
if (!_fTimerServiceObjects.ContainsKey(commandType))
{
lock (_fTimerServiceObjectsLock)
{
if (!_fTimerServiceObjects.ContainsKey(commandType))
{
var cmd = Activator.CreateInstance(commandType) as ICommand;
_fTimerServiceObjects.Add(commandType, cmd);
command = cmd;
}
}
}
else
{
command = _fTimerServiceObjects[commandType];
} #endregion if (!wsItem.WindowsTimer.Prepared)
{
return;
} if (command != null)
{
lock (wsItem.WindowsTimer)
{
try
{
wsItem.WindowsTimer.Prepared = !wsItem.WindowsTimer.Prepared;
command.Execute();
}
catch (Exception ex)
{
//这里不应该导致整个服务终止,而只是把这个错误信号传递到外面去。
if (wsItem.WindowsTimer.CallBack != null)
{
var args = new WindowsServiceTimerExceptionArgs(ex, e);
try
{
wsItem.WindowsTimer.CallBack(wsItem.WindowsTimer, args);
}
catch (Exception ex1)
{
LogHelper.WriteErrorLog(ex1.ToString());
}
}
}
finally
{
wsItem.WindowsTimer.Prepared = !wsItem.WindowsTimer.Prepared;
}
}
}
}
catch (Exception ex)
{
LogHelper.WriteErrorLog(ex.ToString());
}
} /// <summary>
/// 服务停止执行的操作
/// </summary>
protected override void OnStop()
{
try
{
foreach (WindowsServiceItem kvp in _fTimerServices)
{
kvp.WindowsTimer.Timer.Enabled = false;
try
{
kvp.WindowsTimer.Timer.Dispose();
}
catch (Exception ex)
{
LogHelper.WriteErrorLog(ex.ToString());
}
}
}
catch (Exception ex)
{
LogHelper.WriteErrorLog(ex.ToString());
}
} /// <summary>
/// 释放Timer资源
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (_fTimerServices != null)
{
var timers = new List<WindowsTimer>();
foreach (WindowsServiceItem wt in _fTimerServices)
{
timers.Add(wt.WindowsTimer);
} _fTimerServices.Clear();
foreach (WindowsTimer wt in timers)
{
wt.Dispose();
}
}
}
base.Dispose(disposing);
}
}
五、服务demo
开始服务demo的时候,这里我们这里稍微介绍服务帮助类WindowsServiceHelper.cs主要用于执行服务和异常的管理
/// <summary>
/// Windows服务辅助类
/// </summary>
[Serializable]
public class WindowsServiceHelper
{
/// <summary>
///
/// </summary>
static WindowsServiceHelper()
{ } /// <summary>
/// 执行Windows服务
/// </summary>
public static void RunServices(WindowsServiceItemCollection timerServices, WindowsServiceTimerExceptionCallBack callBack)
{
WindowsServiceItemCollection.IsWindowsServiceEnviroments = true; try
{
if (timerServices == null)
{
throw new ArgumentNullException("timerServices");
} if (callBack != null)
{
foreach (WindowsServiceItem kvp in timerServices)
{
kvp.WindowsTimer.CallBack += callBack;
}
} var servicesToRun = new ServiceBase[]
{
new WindowsService(timerServices)
}; foreach (ServiceBase service in servicesToRun)
{
service.AutoLog = true;
} AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; ServiceBase.Run(servicesToRun);
}
catch (Exception ex)
{
LogHelper.WriteErrorLog(ex.ToString());
throw;
}
} /// <summary>
/// 未处理异常的处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
if (e != null)
{
if (e.ExceptionObject != null)
{
if (e.ExceptionObject is Exception)
{
LogHelper.WriteErrorLog(e.ExceptionObject.ToString());
}
}
}
}
catch (Exception ex)
{
LogHelper.WriteErrorLog(ex.ToString());
}
}
}
接下来我们创建一个简单的Demo
重新创建一个控制台程序,创建一个SendSmsCommand.cs 用于发信息,再创建一个SysDataToDataBase.cs用于同步数据到数据库。
public class SendSmsCommand:ICommand
{
public void Execute()
{
// 服务的业务逻辑核心代码
DoSomething();
}
}
public class SysDataToDataBase:ICommand
{
public void Execute()
{
// 服务的业务逻辑核心代码
DoSomething();
}
}
接下来就是将业务相关的服务进行注册,打开Program.cs修改main函数的代码如下:
static void Main(string[] args)
{
try
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
var timerServices = new WindowsServiceItemCollection(); RegisterSendSmsService(timerServices);
RegisterSynDateService(timerServices);
WindowsServiceHelper.RunServices(timerServices, WindowsServiceTimerExceptionCallBack); }
catch(Exception ex)
{
WriterLog(ex.ToString(), "error");
} }
RegisterSendSmsService方法就是用来注册发送信息服务,RegisterSynDateService用来注册同步数据服务。相关实现如下:
private static void RegisterSynDateService(WindowsServiceItemCollection timerServices)
{
try
{
const double interval = ;
var sendTimer = new WindowsTimer("SendSynDataInterval", interval);
timerServices.Add(sendTimer, typeof(SysDataToDataBase));
}
catch (Exception ex)
{
Console.Write(ex);
WriterLog(ex.ToString(), "error");
}
} static void RegisterSendSmsService(WindowsServiceItemCollection timerServices)
{
#region 发送短信服务
try
{
const double interval = ;
var sendTimer = new WindowsTimer("SendSMSInterval", interval);
timerServices.Add(sendTimer, typeof(SendSmsCommand));
}
catch (Exception ex)
{
Console.Write(ex);
WriterLog(ex.ToString(), "error");
}
#endregion
}
六、写在最后
本快速搭建windows服务解决方案,主要好处就接口开发容易搭建多任务服务。服务框架也业务逻辑分离。把核心代码抽取到底层类库或者底层框架中,为团队其他成员创建服务带来便捷,他们不需要关心服务的如何具体实现的,只专注业务逻辑开发。
本人很少写博客,也很少写总结相关文档,这次算是第一次写博客,语言和条理上自己感觉也比较混乱,自认为自己还是在学习阶段,需要想园区很多同仁学习,如果存在问题或者不理解地方欢迎大家文明讨论。再次谢谢大家。