简体   繁体   English

通过topshelf(.net core 2.2)将hangfire用作Windows服务

[英]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中使用Topshelfhangfire用作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.LoadFromConfig 1 valueFactory) at Microsoft.Owin.Hosting.Utilities.SettingsLoader.LoadFromConfig(IDictionary 2 settings)的Func 1 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_0 1.<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.

相关问题 .NET Core Topshelf 服务和 Serilog - .NET Core Topshelf Service and Serilog .Net Core 上带有 Topshelf 的 Windows 服务,它也有一个 Kestrel API - 依赖注入问题 - Windows Service with Topshelf on .Net Core that also has a Kestrel API - Dependency Injection Issues 如何使用Topshelf将asp.net Core 2.1 MVC应用程序部署为Windows服务 - How to deploy asp.net core 2.1 MVC application as windows service using topshelf 如何从 .NET Core 2.1/2.2 创建 Windows 服务 - How to make a Windows Service from .NET Core 2.1/2.2 将ASP.Net OWIN引入现有的TopShelf Windows服务 - Introducing ASP.Net OWIN to an existing TopShelf Windows Service 如何在 .net 内核和 mongodb 中使用挂火? - How to use hangfire in .net core with mongodb? linux下的Topshelf和.net core - Topshelf and .net core under linux .net core 2.2 Windows服务完全启动后,如何配置托管服务才能开始? - How to configure hosted service to begin after .net core 2.2 windows service fully started? 如何在 asp.net 核心上使用自定义配置将 Hangfire 添加为服务 - How to add Hangfire as service with custom configuration on asp.net core 使用 .NET 4.6.1 让 Hangfire 在 windows 服务中工作 - Getting Hangfire to work in a windows service using .NET 4.6.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM