gpt4 book ai didi

c# - topshelf 使用 hangfire 作为 windows 服务(.net core 2.2)

转载 作者:行者123 更新时间:2023-12-04 17:32:48 33 4
gpt4 key购买 nike

我正在尝试使用 吊火使用 作为 windows 服务置物架 在控制台 应用程序 .net 核心 2.2 .我只想加载hangfire仪表板 ,不添加任何工作或其他任何东西。

程序.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();

}
}
}

如您所见,我为 创建了 2 个类。自托管owin 并经过审核 事件查看器 我收到以下显示的错误:

The description for Event ID 0 from source HangfireAsService cannot be found. 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. 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, Func1 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
at HangfireAsService.Program.<>c.b__0_3(Bootstrap s) in 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)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)



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

最佳答案

我在 .net 框架中使用了相同的代码而不是 .net core 并且完美地工作。在测试其他一些东西之后我注意到这个问题,因为 OWIN 发生了所以在我删除它并使用 .net core self-host 而不是 OWIN 之后一切正常.
下面的链接会帮助你很多。

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

关于c# - topshelf 使用 hangfire 作为 windows 服务(.net core 2.2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57973215/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com