Gee*_*ekn 6 c# hangfire .net-core
我正在尝试使用 HangFire 来安排任何实现我调用的特定接口的类IScheduledService。这些服务按预期运行,但 HangFire UI 在 HangFire 仪表板中始终为每个服务显示相同的名称PerformService()。我知道这是设计使然,因为我将相同的函数名称传递给每个作业,但用户并不真正知道正在运行的特定作业。
我创建了接口,ServiceName因为我认为可以将它传递给 HangFire 以覆盖可见的作业名称而不是被调用的函数的名称,但看不到修改作业名称的能力。有没有办法提供自定义作业名称,以便 HangFire UI 将根据ServiceName属性值显示每个作业的标题?
public interface IScheduledService
{
string ServiceId { get; }
string ServiceName { get; }
void PerformService();
}
public class Service1 : IScheduledService
{
public string ServiceId { get => "e56643b1-f0cf-44b2-81ef-bf7a085de760"; }
public string ServiceName { get => this.GetType().Name; }
public void PerformService()
{
Console.WriteLine($"Hello world from {ServiceName}");
}
}
Run Code Online (Sandbox Code Playgroud)
尝试使用 JobDisplayNameAttribute。
我在代码中看到了一些可能会被渲染的地方......
在 RecurringJobsPage 和 JobDetailsPage 中,您可以看到它在调用 HtmlHelper.JobName
HtmlHelper 有一些寻找 JobDisplayNameAttribute 的代码
您可以在发布 1.7.0 后关闭的问题中看到对它的引用。如果你在那个版本。
https://github.com/HangfireIO/Hangfire/issues/1136
在旧问题中也有一些关于在方法上使用 DisplayNameAttribute 的参考。
尝试使用 JobDisplayName 或 DisplayName 装饰方法,如下所示
[JobDisplayName("RunJobNumberOne")]
public void RunSomeJob(string arg){
Run Code Online (Sandbox Code Playgroud)
您可以显示多个参数。在我的实现中,我有 DisplayName 而不是 JobDisplayName (Hangfire ver 1.7.5)
public static class Func
{
[DisplayName("JobID: {0} => [{1}:{2}]")]
public static void Execute(long requestID, string stepName, string stepLocation)
{
// do work here
}
}
// above method is called by the background.enqueue call
BackgroundJob.Enqueue(() => Func.Execute(longId, stepName, stepLocation);
Run Code Online (Sandbox Code Playgroud)
这似乎提供了与执行的作业相关的信息。
| 归档时间: |
|
| 查看次数: |
4048 次 |
| 最近记录: |