title: Am I registering the ObjectContext using AutoFac correctly?
body: <p>I have a windows service and I want to make sure that my EF ObjectContext is disposed of between each time its runs. The service runs longer each time it executes. It seems like the ObjectContext keeps growing. Should my ObjectContext be registered differently or am I doing something wrong?</p>
  
  <p>A overview of what I'm doing.</p>
  
  <ul>
  <li>I'm using <a href="http://quartznet.sourceforge.net/">Quartz.NET</a> to schedule my service</li>
  <li>I'm using <a href="http://atlas.codeplex.com">Atlas</a> to configure and setup my windows service</li>
  <li>I'm using <a href="http://code.google.com/p/autofac/">Autofac</a> as my IoC</li>
  <li>I'm using Entity Framework as my data model</li>
  </ul>
  
  <p>Walkthru of the code:</p>
  
  <ul>
  <li>So the program kicks off by registering the service using Atlas.</li>
  <li>Atlas will register my autofac registrations which are housed in module MyModule</li>
  <li>MyModule registers the ObjectContext at InstancePerLifetimeScope  (Is this the right scope?), then it will have once instance of my custom UnitOfWork as an InstancePerLifetimeScope (Is this the right scope?).</li>
  <li>MyService is hosted by Atlas which gets a Quartz scheduler and AutofacJobListener property injected and schedules the job (MyJob) to run every 5 minutes when the service starts.</li>
  <li>MyJob which gets the instance of the ObjectContext property injected into it from the AutofacJobListener. This job calls out to the database and gets me my stuff. </li>
  </ul>
  
  <p>When the job runs it calls out and gets my stuff every time it runs it takes longer (example: 2 minutes the first time it runs, 4 minutes the second time the service runs, 6 minutes the next, 8 the next and so on). It seems like my ObjectContext is getting larger and larger each time. The data its pulling hasn't changed, still the same number of rows and columns. So I'm thinking my registrations are wrong, is this the case? If not can you see a problem with what I'm doing?</p>
  
  <p><strong>Program</strong></p>
  
  <pre><code>static class Program
  {
      /// &lt;summary&gt;
      /// The main entry point for the application.
      /// &lt;/summary&gt;
      static void Main(string[] args)
      {
          var configuration =
              Host.Configure&lt;MyService&gt;(c =&gt;
              {
                  c.AllowMultipleInstances();
                  c.WithRegistrations(b =&gt; b.RegisterModule(new MyModule()));
              }, args);
          Host.Start(configuration);
      }
  }
  </code></pre>
  
  <p><strong>Module</strong></p>
  
  <pre><code>public class MyModule : Module
  {
      protected override void Load(ContainerBuilder builder)
      {
          LoadQuartz(builder);
          LoadServices(builder);
  
          LoadInfrastructure(builder);
      }
  
      private void LoadInfrastructure(ContainerBuilder builder)
      {
          builder.Register(c =&gt; new ObjectContext())
                  .As&lt;IObjectContext&gt;()
                  .InstancePerLifetimeScope();
  
          builder.Register(c =&gt; new UnitOfWork(c.Resolve&lt;IObjectContext&gt;()))
              .As&lt;ISession&gt;().As&lt;IObjectContextProvider&gt;()
              .InstancePerLifetimeScope();
      }
  
      private void LoadQuartz(ContainerBuilder builder)
      {
          builder.Register(c =&gt; new StdSchedulerFactory().GetScheduler()).As&lt;IScheduler&gt;().InstancePerLifetimeScope();
          builder.Register(c =&gt; new AutofacJobListener(c)).As&lt;IJobListener&gt;();
      }
  
      private void LoadServices(ContainerBuilder builder)
      {
          builder.RegisterType&lt;MyService&gt;().As&lt;IAmAHostedProcess&gt;().PropertiesAutowired();
      }
  }
  </code></pre>
  
  <p><strong>AutofacJobListener</strong></p>
  
  <pre><code>public class AutofacJobListener : IJobListener
  {
      private readonly IComponentContext _container;
  
      public AutofacJobListener(IComponentContext container)
      {
          _container = container;
      }
  
      public void JobToBeExecuted(JobExecutionContext context)
      {
          _container.InjectUnsetProperties(context.JobInstance);
      }
  
      public void JobExecutionVetoed(JobExecutionContext context)
      {
          /*noop*/
      }
  
      public void JobWasExecuted(JobExecutionContext context, JobExecutionException jobException)
      {
          /*noop*/
      }
  
      public string Name
      {
          get { return "AutofacInjectionJobListener"; }
      }
  }
  </code></pre>
  
  <p><strong>MyService</strong></p>
  
  <pre><code>public class MyService : IAmAHostedProcess
  {
      public IScheduler Scheduler { get; set; }
      public IJobListener AutofacJobListener { get; set; }
  
      #region Implementation of IAmAHostedProcess
  
      public void Start()
      {
          var trigger = TriggerUtils.MakeMinutelyTrigger(5);
          trigger.Name = @"Job Trigger";
  
          Scheduler.ScheduleJob(new JobDetail("Job", null, typeof(MyJob)), trigger);
          Scheduler.AddGlobalJobListener(AutofacJobListener);
          Scheduler.Start();
      }
  
      public void Stop()
      {
          Scheduler.Shutdown();
      }
  
      public void Resume()
      {
      }
  
      public void Pause()
      {
      }
  
      #endregion
  }
  </code></pre>
  
  <p><strong>My Job</strong></p>
  
  <pre><code>public class MyJob : IJob
  {
      public IObjectContext ObjectContext { get; set; }
  
      public void Execute(JobExecutionContext context)
      {
          var myStuff = ObjectContext.GetIQueryable&lt;Stuff&gt;();
      }
  }
  </code></pre>