c# - Topshelf 如何处理未处理的异常?
问题描述
我知道有一个事件 OnException 来记录未处理的异常,但它无法停止 Topshelf 服务。结果,在发生未处理的异常时,topshelf 服务停止。
预期:如果出现未处理的异常,topshelf 会记录它而不停止或退出 topshelf 服务。
请帮忙?
var rc = HostFactory.Run(x => //1
{
x.Service<TestService>(s =>
{
s.ConstructUsing(name => new TestService());
s.WhenStarted((tc,hc) => tc.Start(hc));
s.WhenStopped((tc,hc) => tc.Stop(hc));
});
x.SetInstanceName("TestService11");
x.SetDescription("Test Service"); //7
x.SetDisplayName("TestService 11"); //8
x.SetServiceName("TestService11");
x.RunAs(".\administrator", "passwor123");
x.StartAutomatically();
x.UseLog4Net();
x.UnhandledExceptionPolicy = UnhandledExceptionPolicyCode.LogErrorOnly;
x.EnableServiceRecovery(r =>
{
// Has no corresponding setting in the Recovery dialogue.
// OnCrashOnly means the service will not restart if the application returns
// a non-zero exit code. By convention, an exit code of zero means ‘success’.
r.OnCrashOnly();
// Corresponds to ‘First failure: Restart the Service’
// Note: 0 minutes delay means restart immediately
r.RestartService(0);
// Corresponds to ‘Second failure: Restart the Service’
// Note: TopShelf will configure a 1 minute delay before this restart, but the
// Recovery dialogue only shows the first restart delay (0 minutes)
r.RestartService(1);
// Corresponds to ‘Subsequent failures: Restart the Service’
r.RestartService(2);
// Corresponds to ‘Reset fail count after: 1 days’
r.SetResetPeriod(1);
});
x.OnException((ex) =>
{
HostLogger.Get<TestService>().Info("Exception occured Main : " + ex.ToString());
});
});
解决方案
推荐阅读
- r - 我如何在 prais winsten on r 中使用 rho
- api - BIM 360 和 Revit API
- java - 如何让 Byte Buddy 将多种类型加载到同一个包装类加载器中
- reactjs - 如何在页面上导航而不重新加载页面
- javascript - 我有两个独立的功能,一个收集表单文本,另一个切换图像的显示。我无法从其他变量中提取值
- apache-spark - 动态循环所有列名的数据集
- python - 通过在一段文本中查找关键字来过滤数据框
- php - 如何在 PHP 中从画布中删除黑色背景?
- excel - 使用 Excel VBA 宏在 Word 中查找和替换页脚文本
- python - Multiproces 具有可迭代和可迭代参数的函数