0.概述:
工程结构
1.新建windows service工程
2.编写服务程序
新建项
编写代码
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Diagnostics; 6 using System.Linq; 7 using System.ServiceProcess; 8 using System.Text; 9 using System.ServiceModel; 10 11 namespace WindowsService_Test 12 { 13 public partial class Service1 : ServiceBase 14 { 15 public Service1() 16 { 17 InitializeComponent(); 18 } 19 private ServiceHost host; 20 protected override void OnStart(string[] args) 21 { 22 Logger.Write("服务启动......"); 23 try 24 { 25 host = new ServiceHost(typeof(TestAgentService)); 26 host.Open(); 27 } 28 catch (Exception ex) 29 { 30 Logger.WriteException("服务ErpInnerService启动失败.", ex); 31 } 32 string msg = string.Empty; 33 if (!new ServiceWorker().Start(ref msg)) 34 { 35 throw new Exception(msg); 36 } 37 Logger.Write("服务启动成功."); 38 } 39 40 protected override void OnStop() 41 { 42 host.Close(); 43 } 44 } 45 }