window service 插件服务插件开发

1、新建一个Window类库项目,如下图所示:

image

 

2、添加插件接口引用:

image

 

3、引用命名空间

 

image

 

 

4、TimeLogPlug类实现接口IService

 

image

 

5、在类上加入属性定义:

image

 

6、完整实现类如下:

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Text;
   4:  
   5: using WindowServices.Interface;
   6: using System.ComponentModel;
   7:  
   8: namespace TimeLog
   9: {
  10:     [DisplayName("时间日志服务")]
  11:     [Description("每10秒写入日志文件一个时间。")]
  12:     [System.Runtime.InteropServices.Guid("2301DE7F-22A0-415E-9E35-0BE71BD62C76")]
  13:     [Serializable]
  14:     public class TimeLogPlug:IService
  15:     {
  16:         private ServiceEntity _serviceEntity;
  17:         private bool _isRuning;
  18:         public void Initialize(ServiceEntity serviceEntity)
  19:         {
  20:             _serviceEntity = serviceEntity;
  21:         }
  22:  
  23:         public void Pause()
  24:         {
  25:         }
  26:  
  27:         public void Start()
  28:         {
  29:             _isRuning = true;
  30:             using (var sw = System.IO.File.AppendText("d:\\timelog.txt"))
  31:             {
  32:                 sw.WriteLine("插件【{0}】正在运行。\n", _serviceEntity.Name);
  33:             }
  34:             while (_isRuning)
  35:             {
  36:                 using (var sw =System.IO.File.AppendText("d:\\timelog.txt"))
  37:                 {
  38:                     sw.WriteLine("当前时间:{0:yyyy-MM-dd HH:mm:ss}\n",DateTime.Now);
  39:                 }
  40:                 System.Threading.Thread.Sleep(10000);
  41:             }
  42:         }
  43:  
  44:         public void Stop()
  45:         {
  46:             _isRuning = false;
  47:         }
  48:     }
  49: }

7、打包插件

image

 

8、安装插件

image

 

image

image

安装成功

image

9、运行结果

image

 

10.示例打包

Download

posted @ 2011-10-09 15:50  cdboy  阅读(1420)  评论(0编辑  收藏  举报