Topshelf Configuration z
Topshelf Configuration
While the Quickstart gives you enough to get going, there are many more features available in Topshelf. The following details the configuration options available, and how to use them in Topshelf services.
Service Name
Specify the base name of the service, as it is registered in the services control manager. This setting is optional and by default uses the namespace of the Program.cs file (well, basically, the calling assembly type namespace).
HostFactory.New(x =>
{
x.SetServiceName("MyService");
});
It is recommended that service names not contains spaces or other whitespace characters.
Each service on the system must have a unique name. If you need to run multiple instances of the same service, consider using the InstanceName command-line option when registering the service.
Service Description
Specify the description of the service in the services control manager. This is optional and defaults to the service name.
HostFactory.New(x =>
{
x.SetDescription("My First Topshelf Service");
});
Display Name
Specify the display name of the service in the services control manager. This is optional and defaults to the service name.
HostFactory.New(x =>
{
x.SetDisplayName("MyService");
});
Instance Name
Specify the instance name of the service, which is combined with the base service name and separated by a $. This is optional, and is only added if specified.
HostFactory.New(x =>
{
x.SetInstanceName("MyService");
});
This option is typically set using the command-line argument, but it allowed here for completeness.
Service Configuration
The service can be configured in multiple ways, each with different goals. For services that can handle a dependency on Topshelf, the ServiceControl interface provides a lot of value for implementing the service control methods. Additionally, a zero-dependency solution is also available when lambda methods can be used to call methods in the service class.
Simple Service
To configure a simple service, the easiest configuration method is available.
HostFactory.New(x =>
{
x.Service<MyService>();
}); // Service implements the ServiceControl methods directly and has a default constructor
class MyService : ServiceControl
{}
If the service does not have a default constructor, the constructor can be specified, allowing the service to be created by the application, such as when a container needs to be used.
HostFactory.New(x =>
{
x.Service<MyService>(() => ObjectFactory.GetInstance<MyService>());
}); // Service implements the ServiceControl methods directly and has a default constructor
class MyService : ServiceControl
{
public MyService(SomeDependency dependency)
{}
}
If the service needs access to the HostSettings during construction, they are also available as an overload.
HostFactory.New(x =>
{
x.Service<MyService>(hostSettings => new MyService(hostSettings));
}); // Service implements the ServiceControl methods directly and has a default constructor
class MyService : ServiceControl
{
public MyService(HostSettings settings)
{}
}
Custom Service
To configure a completely custom service, such as one that has no dependencies on Topshelf, the following configuration is available.
HostFactory.New(x =>
{
x.Service<MyService>(sc =>
{
sc.ConstructUsing(() => new MyService()); // the start and stop methods for the service
sc.WhenStarted(s => s.Start());
sc.WhenStopped(s => s.Stop()); // optional pause/continue methods if used
sc.WhenPaused(s => s.Pause());
sc.WhenContinued(s => s.Continue()); // optional, when shutdown is supported
sc.WhenShutdown(s => s.Shutdown());
});
});
Each of the WhenXxx methods can also take an argument of the HostControl interface, which can be used to request the service be stopped, request additional start/stop time, etc.
HostFactory.New(x =>
{
x.Service<MyService>(sc =>
{
sc.WhenStarted((s, hostControl) => s.Start(hostControl));
}
}
The HostControl interface can be retained and used as the service is running to Stop the service.
Service Start Modes
There are multiple service start modes, each of which can be specified by the configuration. This option is only used if the service is being installed.
HostFactory.New(x =>
{
x.StartAutomatically(); // Start the service automatically
x.StartAutomaticallyDelayed(); // Automatic (Delayed) -- only available on .NET 4.0 or later
x.StartManually(); // Start the service manually
x.Disabled(); // install the service as disabled
});
Service Recovery
Topshelf also exposes the options need to configure the service recovery options as well.
HostFactory.New(x =>
{
x.EnableServiceRecovery(r =>
{
//you can have up to three of these
r.RestartComputer(5, "message");
r.RestartService(0);
//the last one will act for all subsequent failures
r.RunProgram(7, "ping google.com"); //should this be true for crashed or non-zero exits
r.OnCrashOnly(); //number of days until the error count resets
r.SetResetPeriod(1);
});
});
Service Identity
Services can be configured to run as a number of different identities, using the configuration option that is most appropriate.
HostFactory.New(x =>
{
x.RunAs("username", "password");
});
Runs the service using the specified username and password. This can also be configured using the command-line.
HostFactory.New(x =>
{
x.RunAsPrompt();
});
When the service is installed, the installer will prompt for the username/password combination used to launch the service.
HostFactory.New(x =>
{
x.RunAsNetworkService();
});
Runs the service using the NETWORK_SERVICE built-in account.
HostFactory.New(x =>
{
x.RunAsLocalSystem();
});
Runs the service using the local system account.
HostFactory.New(x =>
{
x.RunAsLocalService();
});
Runs the service using the local service account.
Custom Install Actions
These settings allow user-specified code to be executed during the service install/uninstall process.
Before Install Actions
Topshelf allows actions to be specified that are executed before the service is installed. Note that this action is only executed if the service is being installed.
HostFactory.New(x =>
{
x.BeforeInstall(() => { ... });
});
After Install Actions
Topshelf allows actions to be specified that are executed after the service is installed. Note that this action is only executed if the service is being installed.
HostFactory.New(x =>
{
x.AfterInstall(() => { ... });
});
Before Uninstall Actions
Topshelf allows actions to be specified that are executed before the service is uninstalled. Note that this action is only executed if the service is being uninstalled.
HostFactory.New(x =>
{
x.BeforeUninstall(() => { ... });
});
After Uninstall Actions
Topshelf allows actions to be specified that are executed after the service is uninstalled. Note that this action is only executed if the service is being uninstalled.
HostFactory.New(x =>
{
x.AfterUninstall(() => { ... });
});
Service Dependencies
Service dependencies can be specified such that the service does not start until the dependent services are started. This is managed by the windows services control manager, and not by Topshelf itself.
HostFactory.New(x =>
{
x.DependsOn("SomeOtherService");
});
There are a number of built-in extension methods for well-known services, including:
HostFactory.New(x =>
{
x.DependsOnMsmq(); // Microsoft Message Queueing
x.DependsOnMsSql(); // Microsoft SQL Server
x.DependsOnEventLog(); // Windows Event Log
x.DependsOnIis(); // Internet Information Server
});
Advanced Settings
EnablePauseAndContinue
Specifies that the service supports pause and continue, allowing the services control manager to pass pause and continue commands to the service.
HostFactory.New(x =>
{
x.EnablePauseAndContinue();
});
EnableShutdown
Specifies that the service supports the shutdown service command, allowing the services control manager to quickly shutdown the service.
HostFactory.New(x =>
{
x.EnableShutdown();
});
Service Recovery
To configure the service recovery options, a configurator is available to specify one or more service recovery actions. The recovery options are only used when installing the service, and are set once the service has been successfully installed.
HostFactory.New(x =>
{
x.EnableServiceRecovery(rc =>
{
rc.RestartService(1); // restart the service after 1 minute
rc.RestartSystem(1, "System is restarting!"); // restart the system after 1 minute
rc.RunProgram(1, "notepad.exe"); // run a program
rc.SetResetPeriod(1); // set the reset interval to one day
})
});
The recovery actions are executed in the order specified, with the next action being executed after the previous action was run and the service failed again. There is a limit (based on the OS) of how many actions can be executed, and is typically 2-3 actions.
Topshelf Configuration z的更多相关文章
- 使用Topshelf创建Windows服务
概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的 ...
- Topshelf创建Windows服务
使用Topshelf创建Windows服务 概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps ...
- 【第三方插件】使用Topshelf创建Windows服务
概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的 ...
- 使用Topshelf创建Windows服务[转载]
概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的 ...
- u-boot 之配置分析 (2)
Makefile简要分析所有这些目录的编译连接都是由顶层目录的makefile来确定的. 1.在makefile中有: unconfig: @rm -f $(obj)include/config.h ...
- 移植 MIUI Framework
移植MIUI Framework 原文:http://www.miui.com/thread-409543-1-1.html 1. 为什么使用代码插桩 首先我们来回顾第一章中的Android软件架构图 ...
- TQ210 —— S5PV210 uboot顶层Makefile分析
转自:http://blog.csdn.net/wqx521/article/details/52469759 # (C) Copyright 2000-2008 # Wolfgang Denk, D ...
- JAVA简单上传图片至七牛
utils package com.example.demo.utils; import com.alibaba.fastjson.JSONObject; import com.qiniu.commo ...
- 【Python】使用torrentParser1.03对多文件torrent的分析结果
Your environment has been set up for using Node.js 8.5.0 (x64) and npm. C:\Users\horn1>cd C:\User ...
随机推荐
- Math.round、Math.floor、Math.ceil 区别
1.Math.round() 按照四舍五入的方式返回值 例如:Math.round(9.5)=10 Math.round(9.4)=9 2.Math.floor()返回最小整数 例如:Math. ...
- 解决chrome浏览器对于自动填充的input表单添加的默认的淡黄色背景问题 && 一般的浏览器input和button的高度不一致问题
解决chrome浏览器对于自动填充的input表单添加的默认的淡黄色背景问题 如果我们把一个表单设置位 autofocus ,这时这个表单在获取焦点后就会产生淡黄色的背景,我们就是使用!importa ...
- selenium+Python(定位 单选、复选框,多层定位)
1.定位一组元素webdriver 可以很方便的使用 findElement 方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,这时候就需要使用 findElements 方法.定位一组对象 ...
- 编写规范的javascript
js代码,前端都会写.但细节决定成败,代码是否优雅.规范,可以看得出一个JScoder的水平来. 曾经多次被项目组长吐槽,并被授予一本秘笈,上面有关于JS编程规范的一些总结. 无奈秘笈不能长借,无奈只 ...
- JNI注册调用完整过程-安卓4.4
在Android系统中,JNI方法是以C/C++语言来实现的,然后编译在一个so文件里面,以我之前的例子为例Android Studio使用JNI,调用之前要加载到当前应用程序的进程的地址空间中: s ...
- 带有Apache Spark的Lambda架构
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 目标 市场上的许多玩家已经建立了成功的MapReduce工作流程来每天处理以TB计的历史数据.但是谁愿意等待24小时才能获得最新的分析结果? ...
- 设置 mysql允许外网访问
mysql的root账户,我在连接时通常用的是localhost或127.0.0.1,公司的测试服务器上的mysql也是localhost所以我想访问无法访问,测试暂停. 解决方法如下: 1,修改表, ...
- 关于React Hooks,你不得不知的事
React Hooks是React 16.8发布以来最吸引人的特性之一.在开始介绍React Hooks之前,让咱们先来理解一下什么是hooks.wikipedia是这样给hook下定义的: In c ...
- springboot定时任务,去掉指定日期
今天用springboot写到一个需求:每周定时发送任务,但是要避开法定节假日. 网上找了些博客看,主要参考了https://www.cnblogs.com/lic309/p/4089633.html ...
- 微软的TransactionScope类是个好玩意
最近发现微软自带的TransactionScope(.Net Framework 2之后)是个好东东,提供的功能也很强大. 首先说说TransactionScope是什么,并能为我们做什么事情.其实看 ...