title: C# .NET MVC 5 / How do I create Quartz.NET jobs that use Ninject?
body: <p>I have a project with three-level architecture:</p>
  
  <ul>
  <li>project.Data</li>
  <li>project.Domain</li>
  <li>project.Web    </li>
  </ul>
  
  <p>I need to implement Quartz.Net to user interface level. </p>
  
  <p>And use it following way:</p>
  
  <pre><code> public class EmailSender : IJob
   {
       ISomeService _service;
  
       public EmailSender(ISomeService service)
       {
          _service = service;
       }
  
       public void Execute(IJobExecutionContext context)
       {
          _service.Create();
       }
   }
  
  public class EmailScheduler
  {
      public static void Start()
      {
          IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
          scheduler.Start();
  
          IJobDetail job = JobBuilder.Create&lt;EmailSender&gt;().Build();
  
          ITrigger trigger = TriggerBuilder.Create()  
              .WithIdentity("trigger1", "group1")     
              .StartNow()                           
              .WithSimpleSchedule(x =&gt; x          
                  .WithIntervalInMinutes(1)          
                  .RepeatForever())                   
              .Build();                               
  
          scheduler.ScheduleJob(job, trigger);       
      }
  }
  </code></pre>
  
  <p>In global.asax.cs</p>
  
  <pre><code>public class MvcApplication : System.Web.HttpApplication
  {
      protected void Application_Start()
      {  
          EmailScheduler.Start();
      }
   }
  </code></pre>
  
  <h1>In project.Data</h1>
  
  <p>I use pattern of repositories. As I use few repositories for each entity, I use Unit of Work pattern to make easier connection to database.</p>
  
  <pre><code>public interface IUnitOfWork : IDisposable
  {
      ISomeRepository SomeRepository { get; }
      void Save();
  }
  </code></pre>
  
  <p>EFUnitOfWork class in constructor receives string with name of connection, which will be sent to data context constructor
  So we will interact with database through EFUnitOfWork class.</p>
  
  <pre><code>public class EFUnitOfWork : IUnitOfWork
  {
      private Context db;
  
      private SomeRepository _someRepository;
  
      public EFUnitOfWork(string connectionString)
      {
          db = new Context(connectionString);
      }
  
      public ISomeRepository SomeRepository 
      {
          get
          {
              if (_someRepository == null)
                  _someRepository = new SomeRepository(db);
              return _someRepository;
          }
      }
  }
  </code></pre>
  
  <h2>In project.Domain</h2>
  
  <p>ServiceModule is a special module Ninject, which is used for organizing dependencies matching.
  It replaces EFUnitOfWork with IUnitOfWork.</p>
  
  <pre><code>public class ServiceModule : NinjectModule
  {
      private string connectionString;
      public ServiceModule(string connection)
      {
          connectionString = connection;
      }
      public override void Load()
      {
          Bind&lt;IUnitOfWork&gt;().To&lt;EFUnitOfWork&gt;().WithConstructorArgument(connectionString);
      }
  }
  
  public interface ISomeService
  {
      void Create();
  }
  
  public class SomeService : ISomeService
  {
      IUnitOfWork Database { get; set; }
  
      public LetterService(IUnitOfWork uow)
      {
          Database = uow;
      }
  
      public void Create(){
      }
  }
  </code></pre>
  
  <h2>In project.Web</h2>
  
  <p>To set dependencies I use following class:</p>
  
  <pre><code>public class NinjectDependencyResolver : IDependencyResolver
  {
      private IKernel kernel;
      public NinjectDependencyResolver(IKernel kernelParam)
      {
          kernel = kernelParam;
          AddBindings();
      }
      public object GetService(Type serviceType)
      {
          return kernel.TryGet(serviceType);
      }
      public IEnumerable&lt;object&gt; GetServices(Type serviceType)
      {
          return kernel.GetAll(serviceType);
      }
      private void AddBindings()
      {
          kernel.Bind&lt;ISomeService&gt;().To&lt;SomeService&gt;();
      }
  }
  </code></pre>
  
  <p>In App_Start folder</p>
  
  <pre><code>[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(evenafter.WEB.App_Start.NinjectWebCommon), "Start")]
  [assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(evenafter.WEB.App_Start.NinjectWebCommon), "Stop")]
  
  namespace project.WEB.App_Start
  {
  using System;
  using System.Web;
  
  using Microsoft.Web.Infrastructure.DynamicModuleHelper;
  
  using Ninject;
  using Ninject.Web.Common;
  using Util;
  using Ninject.Modules;
  using Domain.Infrastructure;
  using Quartz.Impl;
  
  public static class NinjectWebCommon 
  {
      private static readonly Bootstrapper bootstrapper = new Bootstrapper();
  
      /// &lt;summary&gt;
      /// Starts the application
      /// &lt;/summary&gt;
      public static void Start() 
      {
          DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
          DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
          bootstrapper.Initialize(CreateKernel);
      }
  
      /// &lt;summary&gt;
      /// Stops the application.
      /// &lt;/summary&gt;
      public static void Stop()
      {
          bootstrapper.ShutDown();
      }
  
      /// &lt;summary&gt;
      /// Creates the kernel that will manage your application.
      /// &lt;/summary&gt;
      /// &lt;returns&gt;The created kernel.&lt;/returns&gt;
      private static IKernel CreateKernel()
      {
          // set connection string
          var modules = new INinjectModule[] { new ServiceModule("DefaultConnection") };
          var kernel = new StandardKernel(modules);
          try
          {
              kernel.Bind&lt;Func&lt;IKernel&gt;&gt;().ToMethod(ctx =&gt; () =&gt; new Bootstrapper().Kernel);
              kernel.Bind&lt;IHttpModule&gt;().To&lt;HttpApplicationInitializationHttpModule&gt;();
              RegisterServices(kernel);
              return kernel;
          }
          catch
          {
              kernel.Dispose();
              throw;
          }
  
      }
  
      /// &lt;summary&gt;
      /// Load your modules or register your services here!
      /// &lt;/summary&gt;
      /// &lt;param name="kernel"&gt;The kernel.&lt;/param&gt;
      private static void RegisterServices(IKernel kernel)
      {
          System.Web.Mvc.DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
      }        
  }
  }
  </code></pre>