title: Autofac, ASP.NET and Microsoft.Practices.ServiceLocation
body: <p>I've been working thru the details of implementing IoC in my web apps but in a way that leverages Microsoft.Practices.ServiceLocation. I am specifically using Autofac and the asp.net integration, but I wanted to leave myself open to other containers. Along the lines of <a href="https://stackoverflow.com/questions/644747/autofac-in-web-applications-where-should-i-store-the-container-for-easy-access">this question</a>, i was concerned about how to access the container in my web app code.</p>
  
  <p>I have a 'core' library that primarily defines interfaces to be resolved. This core library is used by my web app and other apps as well. Very handy to have common interfaces defined. I thought this was an excellent place to put access to the IoC container, and I did so with a static class. The trick is injecting the container into the static class.</p>
  
  <p>It's tricky in a web environment becuase the container may be different for each request, while in a non-web app it will probably be the same all the time. At first I tried injecting the container direclty with a method but that quickly failed on the next web request! So I came up with this:</p>
  
  <pre><code>public static class IoCContainer
  {
      public static void SetServiceLocator(Func&lt;IServiceLocator&gt; getLocator)
      {
          m_GetLocator = getLocator;
      }
      static private Func&lt;IServiceLocator&gt; m_GetLocator = null;
  
      public static T GetInstance&lt;T&gt;(string typeName)
      {
          return m_GetLocator().GetInstance&lt;T&gt;(typeName);
      }
  }
  </code></pre>
  
  <p>Now in my global.asax.cs I do this:</p>
  
  <pre><code>protected void Application_Start(object sender, EventArgs e)
  {
      var builder = new Autofac.Builder.ContainerBuilder();
      ... register stuff ...
      var container = builder.Build();
      _containerProvider = new Autofac.Integration.Web.ContainerProvider(container);
      Xyz.Core.IoCContainer.SetServiceLocator(() =&gt; 
          new AutofacContrib.CommonServiceLocator.AutofacServiceLocator
              (_containerProvider.RequestContainer));
  }
  public IContainerProvider ContainerProvider
  {
      get { return _containerProvider; }
  }
  static IContainerProvider _containerProvider;
  </code></pre>
  
  <p>And calls to resolve dependences look like</p>
  
  <pre><code>var someService = Xyz.Core.GetInstance&lt;ISomeService&gt;();
  </code></pre>
  
  <p>So rather than pass a specific container I pass a delegate that knows how to GET a container. For non-web applications the delegate would probably just return what builder.Build() serves up.</p>
  
  <p>My question to the experts is, does this make sense? I have an easy way to get to something that can resolve dependencies without knowing what the container product is or where the container itself comes from. What do you think?</p>