Last active
December 6, 2018 11:27
-
-
Save Saanch/2c1e63b225ff5d13d145 to your computer and use it in GitHub Desktop.
StructureMapJobFactory Quartz.net IoC using StructureMap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, this is great, works great.