在 .Net 5 的工作服务中配置 Hangfire 仪表板?
我正在使用 Hangfire 来安排我的工作人员服务中的作业,并希望使用 Hangfire 仪表板。但似乎没有办法配置这个。所有文档都使用 Startup 类,但我的工作服务中没有任何启动。另外,.Net 5 不支持 OWIN NuGet 包。这是我尝试过的,
var hostBuilder = CreateHostBuilder(args)
.Build();
var services = hostBuilder.Services;
var applicationBuilder = new ApplicationBuilder(services);
applicationBuilder.UseRouting();
applicationBuilder.UseHangfireDashboard("/hangfire");
applicationBuilder.UseEndpoints(endpoints =>
{
endpoints.MapHangfireDashboard();
});
hostBuilder.Run();
并且我已经像这样配置了hangfire,
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage("connection string",
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true
}));
// Add the processing server as IHostedService
services.AddHangfireServer();
请注意,我能够在当前实现中通过hangfire 安排和执行作业,所有我现在需要的是配置hangfire 仪表板。
I am using Hangfire to schedule jobs in my worker service and want to use the hangfire dashboard. But it seems that there is no way to configure this. All the documentation uses the Startup class but I don't have any startup in my worker service. Also, the OWIN NuGet package is not supported in .Net 5. Here is what I've tried,
var hostBuilder = CreateHostBuilder(args)
.Build();
var services = hostBuilder.Services;
var applicationBuilder = new ApplicationBuilder(services);
applicationBuilder.UseRouting();
applicationBuilder.UseHangfireDashboard("/hangfire");
applicationBuilder.UseEndpoints(endpoints =>
{
endpoints.MapHangfireDashboard();
});
hostBuilder.Run();
and I've configured hangfire like this,
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage("connection string",
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true
}));
// Add the processing server as IHostedService
services.AddHangfireServer();
Please note that I am able to schedule and execute jobs by hangfire in the current implementation, all I need now is to configure the hangfire dashboard.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用以下
Program.cs
配置 Hangfire 仪表板和您的辅助服务:Hangfire 仪表板将在
http://localhost:5000/hangfire
上提供。Use the following
Program.cs
to configure Hangfire dashboard and your worker service:Hangfire dashboard will be available at
http://localhost:5000/hangfire
.在某些情况下只有
app.UseHangfireDashboard();
已经完成了这项工作(启用 Hangfire Dashboard)。.NET 6
Program.cs
In some cases only the
app.UseHangfireDashboard();
already do the job(enables Hangfire Dashboard)..NET 6
Program.cs
如果没有
using Microsoft.AspNetCore.Hosting;
,此示例将无法工作;感谢问题中的 LMio [https://stackoverflow.com/questions/72828395/hangfire-server-in-net-worker-service]This example does not work without
using Microsoft.AspNetCore.Hosting;
Thanks to LMio from question[ https://stackoverflow.com/questions/72828395/hangfire-server-in-net-worker-service]