title: Resolving named registrations from Unity 2.0 using MVC3 IDependencyResolver
body: <p>everybody!</p>
  
  <p>I have a problem with resolving named registrations from Unity 2.0 using MVC3 IDependencyResolver.</p>
  
  <p>I configured the type registrations as</p>
  
  <pre><code>public class Implementations
  {
      private IUnityContainer unityContainer;
  
      internal Implementations()
      {
          this.unityContainer = new UnityContainer();
      }
  
      public static IUnityContainer GetUnityContainer()
      {
          Implementations implementations = new Implementations();
  
          implementations.RegisterImplementations();
  
          return implementations.unityContainer;
      }
  
      private void RegisterImplementations()
      {
          this.unityContainer
              .RegisterType&lt;IService, TestService&gt;()
              .RegisterType&lt;IService, TestService2&gt;("ts2");
      }
  }
  </code></pre>
  
  <p>Code for IDependencyResolver is</p>
  
  <pre><code>public class UnityDependencyResolver : IDependencyResolver
  {
      IUnityContainer container = null;
      string namedRegistration = null;
  
      public UnityDependencyResolver(IUnityContainer container, string name)
      {
          this.container = container;
          this.namedRegistration = name;
      }
  
      public object GetService(Type serviceType)
      {
          try
          {
              return this.container.Resolve(serviceType, this.namedRegistration);
          }
          catch
          {
              return null;
          }
      }
  
      public IEnumerable&lt;object&gt; GetServices(Type serviceType)
      {
          try
          {
              return this.container.ResolveAll(serviceType);
          }
          catch
          {
              return new List&lt;object&gt;();
          }
      }
  }
  </code></pre>
  
  <p>and method call in Application_Start is</p>
  
  <pre><code>protected void Application_Start()
  {
      var dependency =
          new UnityDependencyResolver(Implementations.GetUnityContainer(), "ts2");
      DependencyResolver.SetResolver(dependency);
  
      AreaRegistration.RegisterAllAreas();
  
      RegisterGlobalFilters( GlobalFilters.Filters );
      RegisterRoutes( RouteTable.Routes );
  }
  </code></pre>
  
  <p>What I would like to do, be it in UnityDependencyResolver or any other way, is to resolve my named instance or default instance if named doesn't exist. Current code always resolves to default instance (no named ones).</p>
  
  <p>Does anybody have an idea on how to do it?</p>
  
  <p>Thank you.</p>