-
Notifications
You must be signed in to change notification settings - Fork 34
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
Implemented job activation per lifetime scope #4
Conversation
Hangfire does not support per request containers. This means that all dependencies will be shared between jobs, which is far from ideal, especially when you have IDbContext. This is on the road map of Hangfire 2.0. We need a work around this. This is why we add our own AutofacJobActivator and a JobFilter to manage a per job lifetime scope.
Nice! Have you thought about using the Instance per matching lifetime scope concept and implement the |
{ | ||
public static class HangfirePerLifetimeScopeConfigurer | ||
{ | ||
public static void Configure(IContainer container) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it is better to leverage the new GlobalConfiguration
class and implement the UseScopedAutofacActivator
(or so) extension method for IGlobalConfiguration
interface? In this case configuration will be straight:
GlobalConfiguration.Configuration
.UseSqlServerStorage()
.UseScopedAutofacActivator();
It would be nice to see this pull request merged. I've come to depend on this AutofacPerLifetimeScopeJobActivator. I've been adding it to all my projects that use Hangfire. |
@odinserj Is there a reason this is not being merged? |
@SevenSpikes Will this PR resolve my |
@cottsak That is a separate issue Matt. That says you are trying to use dependencies registered with .InstancePerHttpRequest. If you change those to instance per lifetime scope you end up here because we need a new lifetime scope per job. but i am not sure of all the implications of doing this. The best solution is to have a lifetime scope per job and switch all dependencies to not use per http request. cheers |
Hi there! I've just released Hangfire 1.5.0-beta1 and Hangfire.Autofac 1.2.0-beta1 (Hangfire.Ninject updated too) with child lifetime scope during the background job processing feature implemented. Almost every IoC container supports instance re-use and deterministic disposal, and I decided to integrate this feature to Hangfire.Core to make it simpler to use. Hangfire 1.5.0 introduced a new class, Now Hangfire.Autofac resolves components in a child, tagged lifetime scope. You can register components in the following way: builder.RegisterType<Database>().InstancePerBackgroundJob(); Please read the updated README.md and Release Notes for more details. Now I'm closing this PR. Thank you @SevenSpikes, this PR helped to accelerate these features and helped other users! And sorry for the silence on this topic :-( |
Awesome! I see that a single container can now be shared by using both
I also understand that I'm happy this is supported now, but it still means redoing my Autofac registrations when I'm trying to add Hangfire to an existing MVC project. |
|
Hangfire does not support per lifetime scope containers. This means that all
dependencies will be shared between jobs, which is far from ideal,
especially when you have IDbContext. This is on the road map of Hangfire 2.0.
We need a work around this. This is why we add our own
AutofacJobActivator and a JobFilter to manage a per job lifetime scope.