【问题标题】:use hangfire as windows service by topshelf (.net core 2.2)通过 topshelf (.net core 2.2) 使用 hangfire 作为 Windows 服务
【发布时间】:2019-09-21 11:57:42
【问题描述】:

我正在尝试通过在控制台 app .net core 2.2 中使用 Topshelfhangfire 用作 Windows 服务。我只想加载hangfire仪表板,而不是添加任何工作或其他任何内容。

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

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();

  }
 }
} 

如您所见,我为我的 self-host owin 创建了 2 个类,在查看 事件查看器 后,我得到了如下所示的错误:

来自源 HangfireAsService 的事件 ID 0 的描述不能是 成立。引发此事件的组件未安装在 您的本地计算机或安装已损坏。你可以安装 或修复本地计算机上的组件。

如果事件源自另一台计算机,则显示信息 必须与事件一起保存。

活动中包含以下信息:

服务无法启动。 System.NullReferenceException:对象 引用未设置为对象的实例。在 Microsoft.Owin.Hosting.Utilities.SettingsLoader.FromConfigImplementation..ctor() 在 Microsoft.Owin.Hosting.Utilities.SettingsLoader.c.b__1_0() 在 System.Threading.LazyInitializer.EnsureInitializedCore[T](T& 目标,函数1 valueFactory) at Microsoft.Owin.Hosting.Utilities.SettingsLoader.LoadFromConfig(IDictionary2 设置)在 Microsoft.Owin.Hosting.Engine.StartContext..ctor(StartOptions 选项) 在 Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start(StartOptions 选项)在 Microsoft.Owin.Hosting.Starter.HostingStarter.Start(StartOptions 选项)在 HangfireAsService.Bootstrap.Start() 中 C:\MyWorkSpace\数据 Crawling\dataCrawlingConsole\HangfireAsService\Bootstrap.cs:第 17 行
在 HangfireAsService.Program.c.b__0_3(Bootstrap s) 中 C:\MyWorkSpace\数据 Crawling\dataCrawlingConsole\HangfireAsService\Program.cs:第 15 行
在 Topshelf.ServiceConfiguratorExtensions.c__DisplayClass2_01.<WhenStarted>b__0(T service, HostControl control) at Topshelf.Builders.DelegateServiceBuilder1.DelegateServiceHandle.Start(HostControl 主机控制)在 Topshelf.Runtime.Windows.WindowsServiceHost.OnStart(String[] args)
在 System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(对象 州)

消息资源存在,但在消息表中找不到该消息。

【问题讨论】:

  • 请格式化您的代码示例。格式错误的代码是懒惰的,并且会发送一条消息,表明该问题不值得回答。
  • 如果您指的是错误部分,那与事件查看器显示的内容完全相同,但我认为大约 3 部分代码易于阅读。
  • 不,我的意思是代码示例。因为您编写了这段代码,所以您读起来很清楚。如果您希望人们帮助您,您应该使您的问题尽可能容易让他们阅读。我赞成您的问题,因为我认为这是一个有价值的问题,但是如果您希望更多人帮助您,请重新格式化您的代码。

标签: c# hangfire topshelf


【解决方案1】:

我在 .net 框架而不是 .net 核心中使用了相同的代码并且工作完美。在测试之后我注意到这个问题是因为 OWIN 发生了所以在我删除它并使用 .net 核心自主机而不是 OWIN 之后一切正常。 下面的链接会对你有很大帮助。

https://medium.com/@tocalai/create-windows-service-using-net-core-console-application-dc2f278bbe42

【讨论】:

    猜你喜欢
    • 2023-01-23
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 2018-03-15
    相关资源
    最近更新 更多