创建,安装,调试 Windows Service

把步骤写下来备忘。

目的: 要从windows service里定时调用wcf服务, 做一些事情。

1.新创建一个windows service项目, 然后添加一个LzdCallWcfService.cs的windows service类.

2.添加StartHour,EndHour和Interval三个参数,这样可以通过OnStart(string[] args)方法,在windows service初始启动时传进来间隔时间等参数。

public int StartHour = 1;
public int EndHour = 23;
public int Interval = 1800000; //默认为半小时启动一次。

3.添加timer,让windows service能定时激发,在TmrTimersTimer_Elapsed中执行一些事情。

using System.Timers;
private void TmrTimersTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{

}

4.添加wcf服务引用,通过windows service调用wcf中的业务逻辑代码,这样,windows service端不必部署什么逻辑,只起个定时器的作用。

using (Reference1.LocalWCFClient obj = new Reference1.LocalWCFClient())
{
   obj.DoWork();
}

5.如果wcf部署在有安全证书保护的iis站点内,还需要IgnoreCertificateErrorHandler来规避证书问题。

using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;


private void TmrTimersTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
 {
            if (DateTime.Now.Hour > StartHour && DateTime.Now.Hour < EndHour)
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(IgnoreCertificateErrorHandler);
                using (Reference1.LocalWCFClient obj = new Reference1.LocalWCFClient())
                {
                    obj.DoWork();
                }
            }
}

public static bool IgnoreCertificateErrorHandler(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
          return true;
}

6.完成的代码如下

using System.Text;
using System.Timers;
using System.Configuration;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

namespace YourCompanyWinService
{
    public partial class YourCompanyWinService : ServiceBase
    {
        public int StartHour = 1;
        public int EndHour = 23;
        public int Interval = 1800000; //默认为半小时启动一次。
        private System.Timers.Timer timer1 = null;

        public YourCompanyWinService()
        {
            InitializeComponent();
            timer1 = new Timer();       
        }

        protected override void OnStart(string[] args)
        {
            //服务从StartHour开始可以运行
            try
            {
                StartHour = Convert.ToInt32(args[0]);
            }
            catch { }
            //服务从EndHour开始不再运行
            try
            {
                EndHour = Convert.ToInt32(args[1]);
            }
            catch { }
            //Interval
            try
            {
                Interval = Convert.ToInt32(args[2]);
            }
            catch { }

            //start to work
            timer1.Enabled = true;
            timer1.Interval = Interval;
            timer1.Elapsed += new ElapsedEventHandler(TmrTimersTimer_Elapsed);
            timer1.Start();           
        }

        protected override void OnStop()
        {
            timer1.Enabled = false;
            timer1.Stop();
        }

        private void TmrTimersTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (DateTime.Now.Hour > StartHour && DateTime.Now.Hour < EndHour)
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(IgnoreCertificateErrorHandler);
                using (Reference1.LocalWCFClient obj = new Reference1.LocalWCFClient())
                {
                    obj.DoWork();
                }
            }
        }

        public static bool IgnoreCertificateErrorHandler(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }
    }
}

7.添加安装向导installer

在windows service设计界面,右键快捷菜单上选择添加installer,如下图:

 

在installer界面中,右键serviceProcessInstaller1属性,设置Account=LocalSystem,如下图:

 

继续在installer界面中,右键serviceInstaller1属性,设置Description,DisplayName,ServiceName,StartType等属性,如下图:

displayname最重要, 它用来显示在service列表里, 再写一下description.

 

8.编译项目.

9.安装/卸载

  安装 Installutil.exe D:\TestService\LzdCallWcfService.exe
  卸载 Installutil.exe /u D:\TestService\LzdCallWcfService.exe

  笔者服务器win2008 x64, installutil.exe的位置在C:\Windows\Microsoft.NET\Framework\v4.0.30319, 而不是 C:\Windows\Microsoft.NET\Framework64\v4.0.30319.

  具体步骤:右键vs2010里的命令行tools,选择run as administrator, 在cmd窗口中输入 Installutil.exe D:\TestService\LzdCallWcfService.exe

 

得到如下结果,表示windows service 这就安装成功了。

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>Installutil.exe D:\TestService3\LzdService.exe
Microsoft (R) .NET Framework Installation utility Version 4.0.30319.1

Copyright (c) Microsoft Corporation.  All rights reserved.


Running a transacted installation.

Beginning the Install phase of the installation.
See the contents of the log file for the D:\TestService3\LzdService.exe
...

The Commit phase completed successfully.

The transacted install has completed.

10.启动

在管理工具>服务中找到该服务,右键属性,在参数中输入启动参数。

10.调试

Windows service调试起来不象普通的项目, 直接运行起来(或按f5调试),  需要先install,然后attach a debugger到该service进程.  install-load-start

原文地址:https://www.cnblogs.com/liuzhendong/p/2282375.html