创建Windows服务N种方式总结

上传人:M****1 文档编号:514058094 上传时间:2022-12-19 格式:DOCX 页数:8 大小:40.69KB
返回 下载 相关 举报
创建Windows服务N种方式总结_第1页
第1页 / 共8页
创建Windows服务N种方式总结_第2页
第2页 / 共8页
创建Windows服务N种方式总结_第3页
第3页 / 共8页
创建Windows服务N种方式总结_第4页
第4页 / 共8页
创建Windows服务N种方式总结_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《创建Windows服务N种方式总结》由会员分享,可在线阅读,更多相关《创建Windows服务N种方式总结(8页珍藏版)》请在金锄头文库上搜索。

1、最近由于工作需要,写了一些windows服务程序,有一些经验,我现在总结写出来。 目前我知道的创建创建Windows服务有3种方式:a. 利用.net 框架类 Ser viceBaseb. 利用组件Topshelfc. 利用小工具in sts rv和srvany下面我利用这3种方式,分别做一个windows服务程序,程序功能就是每隔5秒往程序目录下记录日志:a利用.net框架类ServiceBase本方式特点:简单,兼容性好通过继承.net框架类Ser viceBase实现第1步:新建一个Windows服务public partial class Servicel : ServiceBaser

2、eadonly Timer _timer;private static readonly string FileName = Path.GetDirectoryName( Assembly.GetExecutingAssembly ()Location ) + + test txt;public Servicel ()InitializeComponent ();_timer new Timer ( 5000 )AutoReset true ,Enabled true;_timer.Elapsed +delegate ( object sender , ElapsedEventArgs e )

3、this.witre ( string.Format ( Run DateTime 0 , DateTime.Now );;protected override void OnStart ( string args )this.witre ( string.Format ( Start DateTime 0 , DateTime.Now );protected override void OnStop ()this.witre ( string.Format ( Stop DateTime 0 , DateTime.Now ) +Environment.NewLine );void witre

4、 ( string context )StreamWriter sw = File.AppendText ( FileName );sw.WriteLine ( context );sw.Flush ();sw.Close ();第2步:添加InstallerRunInstaller (true )public partial class Installer1 : System.Configuration.Install.Installerprivate ServiceInstaller serviceInstaller;private ServiceProcessInstaller proc

5、essInstaller;public Installer1 ()InitializeComponent ();processInstaller newServiceProcessInstaller);serviceInstaller newServiceInstaller);processInstaller.Account = ServiceAccount.LocalSystem;serviceInstaller.StartType = ServiceStartMode.Automatic;serviceInstaller.ServiceName my_WindowsService;serv

6、iceInstaller.Description WindowsService_Description;serviceInstaller.DisplayName WindowsService_DisplayName;Installers.Add ( serviceInstaller );Installers.Add ( processInstaller );第3步:安装,卸载Cmd命令in stallutilWin dowsSe rvice_test.exe (安装 Win dows 服务)in stallutil /u Win dowsSe rvice_test.exe (卸载 Wi ndo

7、ws 服务)代码下载:http:/files.c nb ron g/Wi ndowsSe rvice_test .rarb利用组件Topshelf本方式特点:代码简单,开源幺日件,Windows服务可运行多个实例Topshelf是一个开源的跨平台的服务框架,支持Windows和Mono,只需要几行代码就可以构建 一个很方便使用的服务.官方网站:http:/topshelf-第 1 步:引用程序集TopShelf.dll 和 log4net.dll第2步:创建一个服务类MyClass,里面包含两个方法Start和Stop,还包含一个定时器Timer,每隔 5秒往文本文件中写入字符public

8、class MyClassreadonly Timer _timer;private static readonly string FileName = Directory.GetCurrentDirectory ()+ + test.txt;public MyClass ()_timer new Timer ( 5000 )AutoReset true ,Enabled true;_timer.Elapsed +delegate ( object sender , ElapsedEventArgs e )this.witre ( string.Format ( Run DateTime 0

9、, DateTime.Now );void witre ( string context )StreamWriter sw = File.AppendText ( FileName );sw.WriteLine ( context );sw.Flush ();sw.Close ();public void Start ()this.witre ( string.Format ( Start DateTime 0 , DateTime.Now );public void Stop ()this.witre ( string.Format ( Stop DateTime 0 , DateTime.

10、Now ) +Environment.NewLine );第3步:使用Topshelf宿主我们的服务,主要是Topshelf如何设置我们的服务的配置和启动和停止 的时候的方法调用class Programstatic void Main ( string args )HostFactory.Run ( x =x.Services.SetServiceNameser);s.ConstructUsing ( name new MyClass ();s.WhenStarted ( ( t ) = t.Start ();s.WhenStopped ( ( t ) = t.Stop ();); x.Ru

11、nAsLocalSystem ();/服务的描述x.SetDescriptionTopshelf_Description );/服务的显示名称x.SetDisplayNameTopshelf_DisplayName );/服务名称x.SetServiceNameTopshelf_ServiceName ););第4步:cmd命令Con soleApp_Topshelf.exe in stall(安装 Win dows 服务)Con soleApp_Topshelf.exe unin stall (卸载 Win dows 服务)代码下载:http:/files.c nb ron g/C on

12、soleApp_Topshelf. rarc利用小工具instsrv和srvany本方式特点:代码超级简单,WindowsForm程序即可,并支持程序交互(本人最喜欢的特点),好像不支持 win7,支持 xp win2003首先介绍2个小工具:in sts rv.exe :用以安装和卸载可执行的服务srvany.exe:用于将任何EXE程序作为Windows服务运行这2个工具都是是Microsoft Windows Resource Kits工具集的实用的小工具你可以通过下载并安装Microsoft Windows Resource Kits获得 http:/ partial class Fo

13、rml : FormTimer timer; private static readonly string FileName = Application.StartupPath + + test.txt;public Form1 ()InitializeComponent ();private void Form1_Load ( object sender , EventArgs e )_timer new Timer ()Enabled true ,Interval 5=)00;_timer.Tick +delegate ( object _sender , EventArgs _e )th

14、is.witre ( string.Format ( Run DateTime 0 , DateTime.Now );;void _timer_Tick ( object sender , EventArgs e )throw new NotImplementedException ();void witre ( string context )StreamWriter sw = File.AppendText ( FileName );sw.WriteLine ( context );sw.Flush ();sw.Close ();private void button1_Click ( object sender , EventArgs e )MessageBox.Show Hello);第2步:安装,卸载服务的安装步骤分5小步:(1) 打开CMD,输入以下内容,其中WindowsForms_WindowsService为你要创建的服务名称 格式:目录绝对路径instsrv WindowsForms_WindowsServic

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号