在 ASP.NET 应用程序中使用 hangfire 1.3.4 和 hangfire.autofac 1.0.0。我有以下情况:
class MyType : IDisposable
{
public void Start()
{
RecurringJob.AddOrUpdate("background-update", () => ProcessData(), Cron.Daily());
RecurringJob.Trigger("background-update");
}
public void ProcessData(){...}
public void Dispose(){...}
}
...
var builder = new ContainerBuilder();
builder.RegisterType<MyType>().SingleInstance();
var cont = builder.Build();
app.UseHangfire(config =>
{
var options = new SqlServerStorageOptions();
config.UseAutofacActivator(cont);
config.UseSqlServerStorage("MyServer", options);
config.UseServer();
});
...
var c = cont.Resolve<MyType>();
c.Start();
我看到的是 Autofac 按要求执行循环作业,然后处理 MyType 的实例,这显然会导致后续调用失败,因为它被定义为单例,并且应该在关闭时由 Autofac 处理。
我错过了什么还是这是一个错误?
这是调用堆栈:
MyDll.dll!MyType.Dispose() 第 316 行 C# Hangfire.Core.dll!Hangfire.Common.Job.Dispose(object instance) Unknown Hangfire.Core.dll!Hangfire.Common.Job.Perform(Hangfire.JobActivator 激活器, Hangfire .IJobCancellationToken cancelToken) Unknown Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.PerformJobWithFilters.AnonymousMethod__6() Unknown Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.InvokePerformFilter(Hangfire.Server.IServerFilter filter, Hangfire.Server.PerformingContext preContext , System.Func 继续) Unknown Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.PerformJobWithFilters.AnonymousMethod__8() Unknown Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.InvokePerformFilter(Hangfire.Server.IServerFilter filter, Hangfire.Server. PerformingContext preContext,System.Func 继续)未知 Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.PerformJobWithFilters.AnonymousMethod__8()未知 Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.PerformJobWithFilters(Hangfire.Server.PerformContext 上下文,Hangfire.Server.IJobPerformer执行者,System.Collections.Generic.IEnumerable 过滤器)未知 Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.Run(Hangfire.Server.PerformContext 上下文,Hangfire.Server.IJobPerformer 执行者)未知 Hangfire.Core.dll!Hangfire.Server .Worker.ProcessJob(string jobId, Hangfire.Storage.IStorageConnection 连接, Hangfire.Server.IJobPerformanceProcess 进程, System.Threading.CancellationToken shutdownToken) 未知 Hangfire.Core.dll!Hangfire.Server.Worker.Execute(System.Threading.CancellationToken cancelToken) 未知 Hangfire.Core.dll!Hangfire.Server.AutomaticRetryServerComponentWrapper.ExecuteWithAutomaticRetry(System.Threading.CancellationToken cancelToken) 未知 Hangfire.Core.dll!Hangfire.Server.AutomaticRetryServerComponentWrapper.Execute(System.Threading.CancellationToken cancelToken) 未知 Hangfire。 Core.dll!Hangfire.Server.ServerSupervisor.ExecuteComponent() 未知 Hangfire.Core.dll!Hangfire.Server.ServerSupervisor.RunComponent() 未知 mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 未知 mscorlib.dll! System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback 回调,对象状态,bool preserveSyncCtx)未知 mscorlib.dll!系统。Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) 未知 mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading。 ContextCallback 回调,对象状态)未知 mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() 未知 [本机到托管转换]ThreadStart() 未知 [本机到托管转换]ThreadStart() 未知 [本机到托管转换]