假设我有以下课程
public class class1
{
public static void class1TextMsg()
{
Console.WriteLine("Hello from Class one");
}
}
[JobFilter2]
public class Class2
{
[JobFilter2]
public static void class2TextMsg()
{
Console.WriteLine("Hello from Class one");
}
}
public class JobFilter1 : IServerFilter, IServerExceptionFilter, IApplyStateFilter
{
public void OnPerforming(PerformingContext filterContext)
{
// log to log file
}
public void OnPerformed(PerformedContext filterContext)
{
// log to log file
}
public void OnServerException(ServerExceptionContext filterContext)
{
// log to log file
}
public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
{
// log to log file
}
public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
{
// log to log file
}
}
public class JobFilter2 : IServerFilter, IServerExceptionFilter, IApplyStateFilter
{
public void OnPerforming(PerformingContext filterContext)
{
// Do nothing
}
public void OnPerformed(PerformedContext filterContext)
{
// Do nothing
}
public void OnServerException(ServerExceptionContext filterContext)
{
// Do nothing
}
public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
{
// Do nothing
}
public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
{
// Do nothing
}
}
在上面的代码中,我定义了 hangfire 全局过滤器如下: Startup.cs
services.AddHangfire((provider, configuration) =>
{
configuration.UseRedisStorage(hangfireConnectionString,
new RedisStorageOptions
{
Prefix = hangfirePrefix
});
configuration.UseFilter(new JobFilter1());
configuration.UseFilter(new JobFilter2());
});
RecurringJob.AddOrUpdate("classOneJob", () =>class1.class1TextMsg() , Cron.MinuteInterval(1));
RecurringJob.AddOrUpdate("classTwoJob", () =>Class2.class2TextMsg() , Cron.MinuteInterval(1));
基本上上面提到的过滤器应该适用于 Hangfire 安排的所有任务,并且它按预期工作。
但在这里我想要实现的是我的 class2 应该只使用 JobFilter2 而不是 JobFilter1 ...为此我将 JobFilter2 作为属性添加到 class2 但它不遵守那个。
有人能指出我正确的方向吗?