c# topshelf 编写windows service
Posted _iorilan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c# topshelf 编写windows service相关的知识,希望对你有一定的参考价值。
使用topself写windows service
1. 在console程序的program.cs main函数中注册服务信息(安装服务时会调用)
...
static void Main(string[] args)
log4net.Config.XmlConfigurator.Configure();
log.Info("Main entry point");
try
var serviceName = ConfigurationManager.AppSettings["ServiceName"];
var serviceDisplayName = ConfigurationManager.AppSettings["ServiceDisplayName"];
var serviceDescription = ConfigurationManager.AppSettings["ServiceDescription"];
var serviceRunner = HostFactory.Run(config =>
config.Service<YourService>(service =>
service.ConstructUsing(name => new YourService());
service.WhenStarted(serviceConfig => serviceConfig.Start());
service.WhenStopped(serviceConfig => serviceConfig.Stop());
);
config.RunAsLocalSystem();
config.SetServiceName(serviceName);
config.SetDisplayName(serviceDisplayName);
config.SetDescription(serviceDescription);
);
var exitCode = (int)Convert.ChangeType(serviceRunner, serviceRunner.GetTypeCode());
Environment.ExitCode = exitCode;
log.Info("Main end point");
//StartConnect();
catch (Exception e)
log.Error("Exception in main", e);
...
2. 在YourService.cs:暴露start和stop方法即可。
public class YourService
public void Start()
...
public void Stop
...
以上是关于c# topshelf 编写windows service的主要内容,如果未能解决你的问题,请参考以下文章