由于产品中有多个windows 服务,而且二次项目的二次开发中也会经常需要编写windows service来实现一些功能,这就导致了项目中经常会出现多个windows service 注册到客户服务器上。这就有了问题,如果缺乏完善的文档记录,在更换实施人员的时候经常会不知道服务器有多少服务是自己项目的,升级的时候会忘记停止,或者没有完全停止,在覆盖升级包的时候会出现覆盖不完全,难免有些粗心的时候就造成了升级不彻底,对于后期的维护也会带来些不必要的麻烦。于是就决定写个调度式的服务,这个服务并不实现具体的功能,它只负责以一定的频率去执行我们注册好的类中的方法。好了不罗嗦了,上代码
1 List<MyTimer> list = null;
2 string[] assmebles = { "Demo.Class1,Class1", "Demo.Class2,Class2" };
3
4 public Service1()
5 {
6 InitializeComponent();
7
8 Init();
9 }
10
11 /// <summary>
12 /// 根据注册的类库,来初始化相应的timer
13 /// </summary>
14 private void Init()
15 {
16 list = new List<MyTimer>();
17 MyTimer timer = null;
18
19 foreach (var item in assmebles)
20 {
21 timer = new MyTimer()
22 {
23 Enabled = false,
24 AutoReset = true,
25 Interval = 1000,
26 };
27 timer.Paramas = item;
28 timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
29 this.list.Add(timer);
30 }
31 }
32
33 /// <summary>
34 /// 功能实现
35 /// </summary>
36 /// <param name="sender"></param>
37 /// <param name="e"></param>
38 void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
39 {
40 try
41 {
42 InterFace.IWindowsService i = null;
43 MyTimer timer = sender as MyTimer;
44 i = Activator.CreateInstance(System.Type.GetType(timer.Paramas)) as InterFace.IWindowsService;
45 if (null == i)
46 {
47 WriteLog("找不到类型" + timer.Paramas);
48 return;
49 }
50 if (i.Interval > 0)
51 timer.Interval = i.Interval;
52 i.Exe();
53 }
54 catch (Exception ex)
55 {
56 WriteLog(ex.Message);
57 }
58 }
2 string[] assmebles = { "Demo.Class1,Class1", "Demo.Class2,Class2" };
3
4 public Service1()
5 {
6 InitializeComponent();
7
8 Init();
9 }
10
11 /// <summary>
12 /// 根据注册的类库,来初始化相应的timer
13 /// </summary>
14 private void Init()
15 {
16 list = new List<MyTimer>();
17 MyTimer timer = null;
18
19 foreach (var item in assmebles)
20 {
21 timer = new MyTimer()
22 {
23 Enabled = false,
24 AutoReset = true,
25 Interval = 1000,
26 };
27 timer.Paramas = item;
28 timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
29 this.list.Add(timer);
30 }
31 }
32
33 /// <summary>
34 /// 功能实现
35 /// </summary>
36 /// <param name="sender"></param>
37 /// <param name="e"></param>
38 void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
39 {
40 try
41 {
42 InterFace.IWindowsService i = null;
43 MyTimer timer = sender as MyTimer;
44 i = Activator.CreateInstance(System.Type.GetType(timer.Paramas)) as InterFace.IWindowsService;
45 if (null == i)
46 {
47 WriteLog("找不到类型" + timer.Paramas);
48 return;
49 }
50 if (i.Interval > 0)
51 timer.Interval = i.Interval;
52 i.Exe();
53 }
54 catch (Exception ex)
55 {
56 WriteLog(ex.Message);
57 }
58 }
以上代码就完成了根据注册的类库信息,初始化了相应的Timer。在实际应用中assmebles 中的信息会保存在数据库中,在以后需要再编写服务来完成时,只需继承InterFace.IWindowsService 接口编写代码即可,这个调度中心会自动调用 子类中的 Exe()方法。
下面是接口代码 在服务开始、停止的控制
/// <summary>
/// 日志
/// </summary>
/// <param name="log"></param>
private void WriteLog(string log)
{
FileStream fs = new FileStream("c:\\test.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine(DateTime.Now.ToString() + "-----" + log);
sw.Flush();
fs.Close();
}
protected override void OnStart(string[] args)
{
WriteLog("服务启动");
//服务启动
foreach (var item in list)
{
item.Enabled = true;
item.Start();
}
}
protected override void OnStop()
{
foreach (var item in list)
{
item.Enabled = false;
item.Stop();
}
WriteLog("服务停止");
}
}
public class MyTimer : System.Timers.Timer
{
public string Paramas { get; set; }
}
namespace InterFace
{
public interface IWindowsService
{
void Exe();
double Interval { get; set; }
}
}
/Files/liuyuedeyv/ServiceFactory.rar/// 日志
/// </summary>
/// <param name="log"></param>
private void WriteLog(string log)
{
FileStream fs = new FileStream("c:\\test.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine(DateTime.Now.ToString() + "-----" + log);
sw.Flush();
fs.Close();
}
protected override void OnStart(string[] args)
{
WriteLog("服务启动");
//服务启动
foreach (var item in list)
{
item.Enabled = true;
item.Start();
}
}
protected override void OnStop()
{
foreach (var item in list)
{
item.Enabled = false;
item.Stop();
}
WriteLog("服务停止");
}
}
public class MyTimer : System.Timers.Timer
{
public string Paramas { get; set; }
}
namespace InterFace
{
public interface IWindowsService
{
void Exe();
double Interval { get; set; }
}
}