Skip to content

Instantly share code, notes, and snippets.

@Saanch
Last active December 6, 2018 11:27
Show Gist options
  • Save Saanch/2c1e63b225ff5d13d145 to your computer and use it in GitHub Desktop.
Save Saanch/2c1e63b225ff5d13d145 to your computer and use it in GitHub Desktop.
StructureMapJobFactory Quartz.net IoC using StructureMap
public class StructureMapJobFactory : IJobFactory
{
private static readonly ILog Logger = LogProvider.GetCurrentClassLogger();
private readonly IContainer _container;
public StructureMapJobFactory(IContainer container)
{
this._container = container;
}
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
{
var jobDetail = bundle.JobDetail;
var jobType = jobDetail.JobType;
try
{
Logger.DebugFormat("Creating instance of Job '{0}', class={1}", jobDetail.Key, jobType.FullName);
return (IJob)_container.GetInstance(bundle.JobDetail.JobType);
}
catch (Exception ex)
{
Logger.ErrorException(string.Format("Problem instantiating class {0}", jobType.FullName), ex);
throw;
}
}
public void ReturnJob(IJob job)
{
// ReSharper disable once SuspiciousTypeConversion.Global
var disposable = job as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
@benbenwilde
Copy link

Thank you, this is great, works great.

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