使用Topshelf 5步創(chuàng)建Windows 服務
使用Topshelf創(chuàng)建Windows 服務簡要的介紹了創(chuàng)建Windows服務的另一種方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通過5個步驟詳細的介紹使用使用Topshelf創(chuàng)建Windows 服務。Topshelf是一個開源的跨平臺的宿主服務框架,支持Windows和Mono,只需要幾行代碼就可以構建一個很方便使用的服務宿主。
1、Topshelf的代碼托管在http://topshelf-project.com/,可以在這里下載到最新的代碼。
2、使用Visual Studio創(chuàng)建一個控制臺應用程序引用程序集TopShelf.dll 合log4net.dll 。
3、創(chuàng)建一個簡單的服務類,里面包含兩個方法Start和Stop,這個服務只是演示代碼,所以我們每隔5秒輸出一個日志。
using System;
using System.Timers;
using log4net;
namespace SampleWindowsService
{
public class SampleService
{
private Timer _timer = null;
readonly ILog _log = LogManager.GetLogger(typeof(SampleService));
public SampleService()
{
double interval = 5000;
_timer = new Timer(interval);
_timer.Elapsed += new ElapsedEventHandler(OnTick);
}
protected virtual void OnTick(object sender, ElapsedEventArgs e)
{
_log.Debug("Tick:" + DateTime.Now.ToLongTimeString());
}
public void Start()
{
_log.Info("SampleService is Started");
_timer.AutoReset = true;
_timer.Enabled = true;
_timer.Start();
}
public void Stop()
{
_log.Info("SampleService is Stopped");
_timer.AutoReset = false;
_timer.Enabled = false;
}
}
}
using System.Timers;
using log4net;
namespace SampleWindowsService
{
public class SampleService
{
private Timer _timer = null;
readonly ILog _log = LogManager.GetLogger(typeof(SampleService));
public SampleService()
{
double interval = 5000;
_timer = new Timer(interval);
_timer.Elapsed += new ElapsedEventHandler(OnTick);
}
protected virtual void OnTick(object sender, ElapsedEventArgs e)
{
_log.Debug("Tick:" + DateTime.Now.ToLongTimeString());
}
public void Start()
{
_log.Info("SampleService is Started");
_timer.AutoReset = true;
_timer.Enabled = true;
_timer.Start();
}
public void Stop()
{
_log.Info("SampleService is Stopped");
_timer.AutoReset = false;
_timer.Enabled = false;
}
}
}
4、在Main方法中使用Topshelf宿主我們的服務,主要是告訴Topshelf如何設置我們的服務的配置和啟動和停止的時候的方法調用。
using System.IO;
using log4net.Config;
using Topshelf;
namespace SampleWindowsService
{
class Program
{
static void Main(string[] args)
{
XmlConfigurator.ConfigureAndWatch(
new FileInfo(".\\log4net.config"));
var host = HostFactory.New(x =>
{
x.EnableDashboard();
x.Service<SampleService>(s =>
{
s.SetServiceName("SampleService");
s.ConstructUsing(name => new SampleService());
s.WhenStarted(tc =>
{
XmlConfigurator.ConfigureAndWatch(
new FileInfo(".\\log4net.config"));
tc.Start();
});
s.WhenStopped(tc => tc.Stop());
});
x.RunAsLocalSystem();
x.SetDescription("SampleService Description");
x.SetDisplayName("SampleService");
x.SetServiceName("SampleService");
});
host.Run();
}
}
}
using log4net.Config;
using Topshelf;
namespace SampleWindowsService
{
class Program
{
static void Main(string[] args)
{
XmlConfigurator.ConfigureAndWatch(
new FileInfo(".\\log4net.config"));
var host = HostFactory.New(x =>
{
x.EnableDashboard();
x.Service<SampleService>(s =>
{
s.SetServiceName("SampleService");
s.ConstructUsing(name => new SampleService());
s.WhenStarted(tc =>
{
XmlConfigurator.ConfigureAndWatch(
new FileInfo(".\\log4net.config"));
tc.Start();
});
s.WhenStopped(tc => tc.Stop());
});
x.RunAsLocalSystem();
x.SetDescription("SampleService Description");
x.SetDisplayName("SampleService");
x.SetServiceName("SampleService");
});
host.Run();
}
}
}
4、配置Log4net和運行我們的服務,服務可以當作控制臺來運行,這在開發(fā)的時候是非常方便的。服務的安裝很方便
SampleWindowsService.exe install
安裝成功后,可以通過服務控制臺啟動,或者也可以通過一下命令運行
SampleWindowsService.exe start
服務的卸載方法也非常簡單了
SampleWindowsService.exe uninstall
相關文章:
A WCF calculator in a windows service with TopShelf
WCF service with Topshelf using as a host, log4net as logging tool, and HTML as output
歡迎大家掃描下面二維碼成為我的客戶,扶你上云

浙公網(wǎng)安備 33010602011771號