<div id="wrapper">
<div id="content">
引用安装
1、官网:http://topshelf-project.com/ 这里面有详细的文档及下载
2、Topshelf的代码托管在 http://github.com/topshelf/Topshelf/downloads ,可以在这里下载到最新的代码。
3、新建一个项目,只需要引用Topshelf.dll 即可,为了日志输出显示,建议也同时引用Topshelf.Log4Net。程序安装命令
Install-Package Topshelf
Install-Package Topshelf.Log4Net
Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的介绍使用使用Topshelf创建Windows 服务。Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务宿主。
引用安装
1、官网:http://topshelf-project.com/ 这里面有详细的文档及下载
2、Topshelf的代码托管在 http://github.com/topshelf/Topshelf/downloads ,可以在这里下载到最新的代码。
3、新建一个项目,只需要引用Topshelf.dll 即可,为了日志输出显示,建议也同时引用Topshelf.Log4Net。程序安装命令
Install-Package Topshelf
Install-Package Topshelf.Log4Net
使用
官网文档给过来的例子非常简单,直接使用即可以跑起来,官网文档地址:http://docs.topshelf-project.com/en/latest/configuration/quickstart.html
public class TownCrier
{
readonly Timer _timer;
public TownCrier()
{
_timer = new Timer(1000) {AutoReset = true};
_timer.Elapsed += (sender, eventArgs) => Console.WriteLine("It is {0} and all is well", DateTime.Now);
}
public void Start() { _timer.Start(); }
public void Stop() { _timer.Stop(); }
}
public class Program
{
public static void Main()
{
HostFactory.Run(x => //1
{
x.Service<TownCrier>(s => //2
{
s.ConstructUsing(name=> new TownCrier()); //3
s.WhenStarted(tc => tc.Start()); //4
s.WhenStopped(tc => tc.Stop()); //5
});
x.RunAsLocalSystem(); //6
x.SetDescription(“Sample Topshelf Host”); //7
x.SetDisplayName(“Stuff”); //8
x.SetServiceName(“Stuff”); //9
}); //10
}
}
程序跑起来后,每隔一秒钟有输出,看到的效果如下:
配置运行 没错,整个程序已经开发完了,接下来,只需要简单配置一下,即可以当服务来使用了。安装很方便:安装:TopshelfDemo.exe install
启动:TopshelfDemo.exe start
卸载:TopshelfDemo.exe uninstall
安装成功后,接下来,我们就可以看到服务里多了一个服务:
我们将上面的程序代码改一下:
HostFactory.Run(x => //1
{
x.Service<TownCrier>(s => //2
{
s.ConstructUsing(name => new TownCrier()); //配置一个完全定制的服务,对Topshelf没有依赖关系。常用的方式。
//the start and stop methods for the service
s.WhenStarted(tc => tc.Start()); //4
s.WhenStopped(tc => tc.Stop()); //5
});
x.RunAsLocalSystem(); // 服务使用NETWORK_SERVICE内置帐户运行。身份标识,有好几种方式,如:x.RunAs("username", "password"); x.RunAsPrompt(); x.RunAsNetworkService(); 等
x.SetDescription("Sample Topshelf Host服务的描述"); //安装服务后,服务的描述
x.SetDisplayName("Stuff显示名称"); //显示名称
x.SetServiceName("Stuff服务名称"); //服务名称
});
<div class="entry">
<div id="cnblogs_post_body" class="blogpost-body"><h1>概述</h1>
Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的介绍使用使用Topshelf创建Windows 服务。Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务宿主。
引用安装
1、官网:http://topshelf-project.com/ 这里面有详细的文档及下载
2、Topshelf的代码托管在 http://github.com/topshelf/Topshelf/downloads ,可以在这里下载到最新的代码。
3、新建一个项目,只需要引用Topshelf.dll 即可,为了日志输出显示,建议也同时引用Topshelf.Log4Net。程序安装命令
- Install-Package Topshelf
- Install-Package Topshelf.Log4Net
使用
官网文档给过来的例子非常简单,直接使用即可以跑起来,官网文档地址:http://docs.topshelf-project.com/en/latest/configuration/quickstart.html
public class TownCrier { readonly Timer _timer; public TownCrier() { _timer = new Timer(1000) {AutoReset = true}; _timer.Elapsed += (sender, eventArgs) => Console.WriteLine("It is {0} and all is well", DateTime.Now); } public void Start() { _timer.Start(); } public void Stop() { _timer.Stop(); } }
public class Program
{
public static void Main()
{
HostFactory.Run(x => //1
{
x.Service<TownCrier>(s => //2
{
s.ConstructUsing(name=> new TownCrier()); //3
s.WhenStarted(tc => tc.Start()); //4
s.WhenStopped(tc => tc.Stop()); //5
});
x.RunAsLocalSystem(); //6
x.SetDescription(“Sample Topshelf Host”); //7
x.SetDisplayName(“Stuff”); //8
x.SetServiceName(“Stuff”); //9
}); //10
}
}
程序跑起来后,每隔一秒钟有输出,看到的效果如下:
配置运行
没错,整个程序已经开发完了,接下来,只需要简单配置一下,即可以当服务来使用了。安装很方便:
安装成功后,接下来,我们就可以看到服务里多了一个服务:
扩展说明
Topshelf Configuration 简单配置
官方文档,对HostFactory 里面的参数做了详细的说明:http://docs.topshelf-project.com/en/latest/configuration/config_api.html ,下面只对一些常用的方法进行简单的解释:
我们将上面的程序代码改一下:
HostFactory.Run(x => //1 { x.Service<TownCrier>(s => //2 { s.ConstructUsing(name => new TownCrier()); //配置一个完全定制的服务,对Topshelf没有依赖关系。常用的方式。
//the start and stop methods for the service
s.WhenStarted(tc => tc.Start()); //4 s.WhenStopped(tc => tc.Stop()); //5 }); x.RunAsLocalSystem(); // 服务使用NETWORK_SERVICE内置帐户运行。身份标识,有好几种方式,如:x.RunAs("username", "password"); x.RunAsPrompt(); x.RunAsNetworkService(); 等 x.SetDescription("Sample Topshelf Host服务的描述"); //安装服务后,服务的描述 x.SetDisplayName("Stuff显示名称"); //显示名称 x.SetServiceName("Stuff服务名称"); //服务名称 });
重装安装运行后:
通过上面,相信大家都很清楚 SetDescription、SetDisplayName、SetServiceName区别。不再细说。
Service Configuration 服务配置
Topself的服务一般有主要有两种使用模式。
一、简单模式。继承ServiceControl接口,实现该接口即可。
实例:
namespace TopshelfDemo { public class TownCrier : ServiceControl { private Timer _timer = null; readonly ILog _log = LogManager.GetLogger(typeof(TownCrier)); public TownCrier() { _timer = new Timer(1000) { AutoReset = true }; _timer.Elapsed += (sender, eventArgs) => _log.Info(DateTime.Now);} </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">bool</span><span style="color: #000000;"> Start(HostControl hostControl) { _log.Info(</span><span style="color: #800000;">"</span><span style="color: #800000;">TopshelfDemo is Started</span><span style="color: #800000;">"</span><span style="color: #000000;">); _timer.Start(); </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">bool</span><span style="color: #000000;"> Stop(HostControl hostControl) { </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span><span style="color: #000000;"> NotImplementedException(); } } </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Program { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args) { </span><span style="color: #0000ff;">var</span> logCfg = <span style="color: #0000ff;">new</span> FileInfo(AppDomain.CurrentDomain.BaseDirectory + <span style="color: #800000;">"</span><span style="color: #800000;">log4net.config</span><span style="color: #800000;">"</span><span style="color: #000000;">); XmlConfigurator.ConfigureAndWatch(logCfg); HostFactory.Run(x </span>=><span style="color: #000000;"> { x.Service</span><TownCrier><span style="color: #000000;">(); x.RunAsLocalSystem(); x.SetDescription(</span><span style="color: #800000;">"</span><span style="color: #800000;">Sample Topshelf Host服务的描述</span><span style="color: #800000;">"</span><span style="color: #000000;">); x.SetDisplayName(</span><span style="color: #800000;">"</span><span style="color: #800000;">Stuff显示名称</span><span style="color: #800000;">"</span><span style="color: #000000;">); x.SetServiceName(</span><span style="color: #800000;">"</span><span style="color: #800000;">Stuff服务名称</span><span style="color: #800000;">"</span><span style="color: #000000;">); }); } }
}
二、常用模式。
实例:
namespace TopshelfDemo { public class TownCrier { private Timer _timer = null; readonly ILog _log = LogManager.GetLogger( typeof(TownCrier)); public TownCrier() { _timer = new Timer(1000) { AutoReset = true }; _timer.Elapsed += (sender, eventArgs) => _log.Info(DateTime.Now); } public void Start(){ _timer.Start();} public void Stop() { _timer.Stop(); } }</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Program { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args) { </span><span style="color: #0000ff;">var</span> logCfg = <span style="color: #0000ff;">new</span> FileInfo(AppDomain.CurrentDomain.BaseDirectory + <span style="color: #800000;">"</span><span style="color: #800000;">log4net.config</span><span style="color: #800000;">"</span><span style="color: #000000;">); XmlConfigurator.ConfigureAndWatch(logCfg); HostFactory.Run(x </span>=><span style="color: #000000;"> { x.Service</span><TownCrier>(s =><span style="color: #000000;"> { s.ConstructUsing(name </span>=> <span style="color: #0000ff;">new</span><span style="color: #000000;"> TownCrier()); s.WhenStarted(tc </span>=><span style="color: #000000;"> tc.Start()); s.WhenStopped(tc </span>=><span style="color: #000000;"> tc.Stop()); }); x.RunAsLocalSystem(); x.SetDescription(</span><span style="color: #800000;">"</span><span style="color: #800000;">Sample Topshelf Host服务的描述</span><span style="color: #800000;">"</span><span style="color: #000000;">); x.SetDisplayName(</span><span style="color: #800000;">"</span><span style="color: #800000;">Stuff显示名称</span><span style="color: #800000;">"</span><span style="color: #000000;">); x.SetServiceName(</span><span style="color: #800000;">"</span><span style="color: #800000;">Stuff服务名称</span><span style="color: #800000;">"</span><span style="color: #000000;">); }); } }
}
两种方式,都使用了Log4Net,相关配置:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections>
<log4net>
<appender name=“RollingLogFileAppender” type=“log4net.Appender.RollingFileAppender”>
<!–日志路径–>
<param name= “File” value= “D:\App_Data\servicelog”/>
<!–是否是向文件中追加日志–>
<param name= “AppendToFile” value= “true”/>
<!–log保留天数–>
<param name= “MaxSizeRollBackups” value= “10”/>
<!–日志文件名是否是固定不变的–>
<param name= “StaticLogFileName” value= “false”/>
<!–日志文件名格式为:2008-08-31.log–>
<param name= “DatePattern” value= “yyyy-MM-dd".log"”/>
<!–日志根据日期滚动–>
<param name= “RollingStyle” value= “Date”/>
<layout type=“log4net.Layout.PatternLayout”>
<param name=“ConversionPattern” value="%d [%t] %-5p %c - %m%n %loggername" />
</layout>
</appender>
<!-- 控制台前台显示日志 -->
<appender name=<span style="color: #800000;">"</span><span style="color: #800000;">ColoredConsoleAppender</span><span style="color: #800000;">"</span> type=<span style="color: #800000;">"</span><span style="color: #800000;">log4net.Appender.ColoredConsoleAppender</span><span style="color: #800000;">"</span>>
<mapping>
<level value=<span style="color: #800000;">"</span><span style="color: #800000;">ERROR</span><span style="color: #800000;">"</span> />
<foreColor value=<span style="color: #800000;">"</span><span style="color: #800000;">Red, HighIntensity</span><span style="color: #800000;">"</span> />
</mapping>
<mapping>
<level value=<span style="color: #800000;">"</span><span style="color: #800000;">Info</span><span style="color: #800000;">"</span> />
<foreColor value=<span style="color: #800000;">"</span><span style="color: #800000;">Green</span><span style="color: #800000;">"</span> />
</mapping>
<layout type=<span style="color: #800000;">"</span><span style="color: #800000;">log4net.Layout.PatternLayout</span><span style="color: #800000;">"</span>>
<conversionPattern value=<span style="color: #800000;">"</span><span style="color: #800000;">%n%date{HH:mm:ss,fff} [%-5level] %m</span><span style="color: #800000;">"</span> />
</layout>
<filter type=<span style="color: #800000;">"</span><span style="color: #800000;">log4net.Filter.LevelRangeFilter</span><span style="color: #800000;">"</span>>
<param name=<span style="color: #800000;">"</span><span style="color: #800000;">LevelMin</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">Info</span><span style="color: #800000;">"</span> />
<param name=<span style="color: #800000;">"</span><span style="color: #800000;">LevelMax</span><span style="color: #800000;">"</span> value=<span style="color: #800000;">"</span><span style="color: #800000;">Fatal</span><span style="color: #800000;">"</span> />
</filter>
</appender>
<root>
<!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) -->
<level value=<span style="color: #800000;">"</span><span style="color: #800000;">all</span><span style="color: #800000;">"</span> />
<appender-<span style="color: #0000ff;">ref</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">ColoredConsoleAppender</span><span style="color: #800000;">"</span>/>
<appender-<span style="color: #0000ff;">ref</span> <span style="color: #0000ff;">ref</span>=<span style="color: #800000;">"</span><span style="color: #800000;">RollingLogFileAppender</span><span style="color: #800000;">"</span>/>
</root>
</log4net>
</configuration>
推荐使用第二种常用模式。