Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hangfire disposes of autofac dependencies which are defined as SingleInstance #329

Closed
grisharav opened this issue Mar 21, 2015 · 6 comments

Comments

@grisharav
Copy link

Using hangfire 1.3.4 with hangfire.autofac 1.0.0 in an ASP.NET application.
I have the following scenario:

     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();

What I see is that Autofac executes the recurrent job as requested but then disposes of instance of MyType, which obviously causes failures on subsequent calls for it, since it's defined as a singleton, and should be disposed of by Autofac upon shutdown.

Am I missing something or is this a bug?

@grisharav grisharav reopened this Mar 22, 2015
@odinserj
Copy link
Member

If the problem still actual? I'm just curious, what do you call in the Dispose method?

@grisharav
Copy link
Author

Well the issue is still there, we redesigned the code slightly to work around it, but I think this should be fixed somehow. Even a class which only sets a member tonull in it's dispose method is problematic, since the logic won't work on another time hangfire needs the class.
In our code we disposed of a UserManager<..,..> class which is IDisposable...

@odinserj
Copy link
Member

So you declare singleton class that implements IDisposable. Who is responsible to call the Dispose method in this case?

@odinserj
Copy link
Member

@grisharav, any news?

@grisharav
Copy link
Author

I agree it's not a great design, but theoretically, upon shutdown of the process the termination of all singletons (which are evil, I agree) should be done, either explicitly by the user, or by a framework (in my case Autofac)

@odinserj
Copy link
Member

@grisharav, after some months, I've changed this behavior in Hangfire 1.5.0-beta1 and Hangfire.Autofac 1.2.0-beta1. In the new Hangfire release I've added the JobActivationScope, an abstraction on IoC container scopes. It allowed to use the child lifetime scopes in Autofac integration.

Now, Autofac owns the IDisposable components and is responsible to call the Dispose method where appropriate after background job processing. For example, InBackgroundJobScope, InstancePerDependency and so on are disposed, but SingleInstance, ExternallyOwned are not. So this implementation is more natural to dependency management using Autofac.

Thank you for doubts in the previous implementation! I'm closing this issue, please feel free to comment it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants