Skip to content

Instantly share code, notes, and snippets.

@odinserj
Created December 13, 2018 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odinserj/855f72b726f7fdb0f718c3ab07ba3a82 to your computer and use it in GitHub Desktop.
Save odinserj/855f72b726f7fdb0f718c3ab07ba3a82 to your computer and use it in GitHub Desktop.
public class MyAutofacActivator : JobActivator
{
private readonly ILifetimeScope _lifetimeScope;
public MyAutofacActivator(ILifetimeScope lifetimeScope)
{
_lifetimeScope = lifetimeScope;
}
public override JobActivatorScope BeginScope(JobActivatorContext context)
{
var connection = context.GetJobParameter<string>("Connection");
return new AutofacScope(_lifetimeScope.BeginLifetimeScope(config =>
{
config.Register(x => new SqlConnection(connection)).As<SqlConnection>();
}));
}
class AutofacScope : JobActivatorScope
{
private readonly ILifetimeScope _lifetimeScope;
public AutofacScope(ILifetimeScope lifetimeScope)
{
_lifetimeScope = lifetimeScope;
}
public override object Resolve(Type type)
{
return _lifetimeScope.Resolve(type);
}
public override void DisposeScope()
{
_lifetimeScope.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment