Hangfire处理autofac依赖项,定义为SingleInstance

时间:2015-03-23 12:47:16

标签: c# .net autofac idisposable hangfire

在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处理掉。

我错过了什么或这是一个错误吗?

这是callstack:

  

MyDll.dll!MyType.Dispose()第316行C#       Hangfire.Core.dll!Hangfire.Common.Job.Dispose(对象实例)未知       Hangfire.Core.dll!Hangfire.Common.Job.Perform(Hangfire.JobActivator激活器,Hangfire.IJobCancellationToken cancellationToken)未知       Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.PerformJobWithFilters.AnonymousMethod__6()未知       Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.InvokePerformFilter(Hangfire.Server.IServerFilter过滤器,Hangfire.Server.PerformingContext preContext,System.Func continuation)未知       Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.PerformJobWithFilters.AnonymousMethod__8()未知       Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.InvokePerformFilter(Hangfire.Server.IServerFilter过滤器,Hangfire.Server.PerformingContext preContext,System.Func continuation)未知       Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.PerformJobWithFilters.AnonymousMethod__8()未知       Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.PerformJobWithFilters(Hangfire.Server.PerformContext context,Hangfire.Server.IJobPerformer performer,System.Collections.Generic.IEnumerable filters)Unknown       Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.Run(Hangfire.Server.PerformContext context,Hangfire.Server.IJobPerformer performer)Unknown       Hangfire.Core.dll!Hangfire.Server.Worker.ProcessJob(字符串jobId,Hangfire.Storage.IStorageConnection连接,Hangfire.Server.IJobPerformanceProcess进程,System.Threading.CancellationToken shutdownToken)未知       Hangfire.Core.dll!Hangfire.Server.Worker.Execute(System.Threading.CancellationToken cancellationToken)Unknown       Hangfire.Core.dll!Hangfire.Server.AutomaticRetryServerComponentWrapper.ExecuteWithAutomaticRetry(System.Threading.CancellationToken cancellationToken)Unknown       Hangfire.Core.dll!Hangfire.Server.AutomaticRetryServerComponentWrapper.Execute(System.Threading.CancellationToken cancellationToken)Unknown       Hangfire.Core.dll!Hangfire.Server.ServerSupervisor.ExecuteComponent()未知       Hangfire.Core.dll!Hangfire.Server.ServerSupervisor.RunComponent()未知       mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state)未知       mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,object state,bool preserveSyncCtx)Unknown       mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,object state,bool preserveSyncCtx)Unknown       mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext,System.Threading.ContextCallback callback,object state)Unknown       mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()未知       [原生于管理过渡]

1 个答案:

答案 0 :(得分:0)

最新(beta)版本的hangfire修复了此问题,详见https://github.com/HangfireIO/Hangfire/issues/329