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

Outer context for job execution by UnitOfWorkManager. #330

Closed

Conversation

itmagination
Copy link

UnitOfWorkManager provides outer context for job execution which is useful for scopes management (database transactions, DI container scopes etc.) and for externalization of any other kind of repetitive code on begin or end of each executed task.

To use UnitOfWorkManager you have to implement interface IUnitOfWorkManager and provide your implementation to Hangfire configuration. This is very similar to JobActivator usage.

For example to create WindorContainer scope for each job execution context you can use blow implementation:

class WindsorContainerScopeUnitOfWorkManager : IUnitOfWorkManager
{
  private readonly IWindsorContainer _container;

  public WindsorContainerScopeUnitOfWorkManager(IWindsorContainer container)
  {
     _container = container;
  }

  public object Begin()
  {
    return _container.BeginScope();
  }

  public void End(object context, Exception ex = null)
  {
    ((IDisposable) context).Dispose();
  }
}

And register it in Hangfire like below:

app.UseHangfire(config =>
{
    config.UseActivator(new WindsorJobActivator(_container.Kernel));
    config.UseUnitOfWorkManager(new WindsorContainerScopeUnitOfWorkManager(_container));
});

@odinserj
Copy link
Member

Hi @itmagination, sorry for the looong delay 😦 In Hangfire 1.5.0-beta1 I've added the JobActivator.BeginScope method as well as the base JobActivatorScope class to allow support for custom scopes/child containers without any job filters to simplify the user experience. Please see my answer on a corresponding forum topic for some details.

I'm closing this PR. Thank you for it, you shown the way how to solve this problem using current features to many users! ❤️

@odinserj odinserj closed this Jul 27, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants