[英]use hangfire as windows service by topshelf (.net core 2.2)
I am trying to use hangfire as windows service by using Topshelf in console app .net core 2.2 . 我试图通过在控制台应用程序.net core 2.2中使用Topshelf将hangfire用作Windows服务。 I just want to load hangfire dashboard , not adding any job or anything else.
我只想加载hangfire仪表板 ,而不添加任何作业或其他任何东西。
Program.cs Program.cs
using System;
using Topshelf;
namespace HangfireAsService
{
class Program
{
static void Main(string[] args)
{
HostFactory.Run(config =>
{
config.Service<Bootstrap>(service =>
{
service.ConstructUsing(s => new Bootstrap());
service.WhenStarted(s => s.Start());
service.WhenStopped(s => s.Stop());
});
config.RunAsLocalSystem();
config.SetDescription("Hangfire as windows Service for DataCrawling Project");
config.SetDisplayName("Hangfire Service Custom");
});
}
}
}
Bootstrap.cs Bootstrap.cs
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Owin.Hosting;
namespace HangfireAsService
{
public class Bootstrap
{
private IDisposable _host;
public void Start()
{
var options = new StartOptions { Port = 8999 };
_host = WebApp.Start<Startup>(options);
Console.WriteLine();
Console.WriteLine("Hangfire has started");
Console.WriteLine("Dashboard is available at http://localhost:8999/hangfire");
Console.WriteLine();
}
public void Stop()
{
_host.Dispose();
}
}
}
Startup.cs 启动文件
using Hangfire;
using Microsoft.AspNetCore.Builder;
using Owin;
using System;
using System.Collections.Generic;
using System.Text;
namespace HangfireAsService
{
public class Startup
{
public void Configuration(IApplicationBuilder appBuilder)
{
GlobalConfiguration.Configuration
.UseSqlServerStorage("Server=111.111.11.1\\INS2017; Database=Hangfire; user=sa;
password=;");
appBuilder.UseHangfireDashboard();
appBuilder.UseHangfireServer();
}
}
}
As you can see, I created 2 classes for my self-host owin and after reviewing the event viewer I got the error displayed below: 如您所见,我为自托管owin创建了2个类,并在查看了事件查看器后得到了以下显示的错误:
The description for Event ID 0 from source HangfireAsService cannot be found.
找不到源HangfireAsService的事件ID 0的描述。 Either the component that raises this event is not installed on your local computer or the installation is corrupted.
引发此事件的组件未安装在本地计算机上,或者安装已损坏。 You can install or repair the component on the local computer.
您可以在本地计算机上安装或修复组件。
If the event originated on another computer, the display information had to be saved with the event.
如果事件起源于另一台计算机,则显示信息必须与事件一起保存。
The following information was included with the event:
该事件包括以下信息:
Service cannot be started.
服务无法启动。 System.NullReferenceException: Object reference not set to an instance of an object.
System.NullReferenceException:对象引用未设置为对象的实例。 at Microsoft.Owin.Hosting.Utilities.SettingsLoader.FromConfigImplementation..ctor() at Microsoft.Owin.Hosting.Utilities.SettingsLoader.<>c.b__1_0() at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Func
1 valueFactory) at Microsoft.Owin.Hosting.Utilities.SettingsLoader.LoadFromConfig(IDictionary
2 settings) at Microsoft.Owin.Hosting.Engine.StartContext..ctor(StartOptions options) at Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start(StartOptions options) at Microsoft.Owin.Hosting.Starter.HostingStarter.Start(StartOptions options) at HangfireAsService.Bootstrap.Start() in C:\\MyWorkSpace\\Data Crawling\\dataCrawlingConsole\\HangfireAsService\\Bootstrap.cs:line 17在Microsoft.Owin.Hosting.Utilities.SettingsLoader.FromConfigImplementation..ctor()在Microsoft.Owin.Hosting.Utilities.SettingsLoader。<> c.b__1_0()在System.Threading.LazyInitializer.EnsureInitializedCore [T](T&target,
1 valueFactory) at Microsoft.Owin.Hosting.Utilities.SettingsLoader.LoadFromConfig(IDictionary
上Microsoft.Owin.Hosting.Engine.StartContext..ctor(StartOptions options)上的Microsoft.Owin.Hosting.Utilities.SettingsLoader.LoadFromConfig1 valueFactory) at Microsoft.Owin.Hosting.Utilities.SettingsLoader.LoadFromConfig(IDictionary
2 settings)的Func1 valueFactory) at Microsoft.Owin.Hosting.Utilities.SettingsLoader.LoadFromConfig(IDictionary
C:\\ MyWorkSpace \\ Data Crawling \\ dataCrawlingConsole \\ HangfireAsService \\ Bootstrap.cs:line 17中的HangfireAsService.Bootstrap.Start()中Microsoft.Owin.Hosting.Starter.HostingStarter.Start(StartOptions选项)中的(StartOptions选项)
at HangfireAsService.Program.<>c.b__0_3(Bootstrap s) in C:\\MyWorkSpace\\Data Crawling\\dataCrawlingConsole\\HangfireAsService\\Program.cs:line 15在HangfireAsService.Program。<> c.b__0_3(Bootstrap s)在C:\\ MyWorkSpace \\ Data Crawling \\ dataCrawlingConsole \\ HangfireAsService \\ Program.cs:line 15
at Topshelf.ServiceConfiguratorExtensions.<>c__DisplayClass2_01.<WhenStarted>b__0(T service, HostControl control) at Topshelf.Builders.DelegateServiceBuilder
1.DelegateServiceHandle.Start(HostControl hostControl) at Topshelf.Runtime.Windows.WindowsServiceHost.OnStart(String[] args)<> c__DisplayClass2_0
1.<WhenStarted>b__0(T service, HostControl control) at Topshelf.Builders.DelegateServiceBuilder
b__0(1.<WhenStarted>b__0(T service, HostControl control) at Topshelf.Builders.DelegateServiceBuilder
位于Topshelf.Runtime.Windows.WindowsServiceHost.OnStart(字符串[]) args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)在System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(对象状态)
The message resource is present, but the message was not found in the message table. 存在消息资源,但是在消息表中找不到消息。
i used same code inside .net framework instead of .net core and work perfectly.after a test something else i notice this problem because of OWIN happened so after i removed it and use using .net core self-host instead of OWIN everything work perfectly. 我在.net框架内而不是.net core内使用了相同的代码,并且可以完美地工作。经过其他测试,我注意到这个问题是由于OWIN发生的,所以在删除它并使用.net核心自托管程序而不是OWIN之后,一切正常。 below link will help you a lot.
以下链接将为您提供很多帮助。
https://medium.com/@tocalai/create-windows-service-using-net-core-console-application-dc2f278bbe42 https://medium.com/@tocalai/create-windows-service-using-net-core-console-application-dc2f278bbe42
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.