title: Accessing Unity-specific functionality in .NET Core dependency injection
body: <p>I have a .net core 2.1 console app. I'm building the container as below:</p>
  
  <pre><code>public IServiceProvider ConfigureServices()
  {
      var services = new ServiceCollection();
  
      // Register services here.
  
      // Unity container interface:
      var containerFactory = new ServiceProviderFactory(null);
      IUnityContainer container = containerFactory.CreateBuilder(services);
      IServiceProvider serviceProvider = containerFactory.CreateServiceProvider(container);
  
      return serviceProvider;
  }
  </code></pre>
  
  <p>I need to provide constructor arguments to some of the services registered in the container. However, I do not want to use something like <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.1" rel="nofollow noreferrer">options pattern</a>. Instead, I would like to access the Unity functionality with something like below:</p>
  
  <pre><code>var container = (UnityContainer)serviceProvider;
  var foo = container.Resolve&lt;IFoo&gt;(new ParameterOverrides&lt;Foo&gt; { "name": "myFoo" });
  </code></pre>
  
  <p>The above approach is not working and throw the error:</p>
  
  <pre><code>Unable to cast object of type 'Unity.Microsoft.DependencyInjection.ServiceProvider' to type 'Unity.UnityContainer'.
  </code></pre>
  
  <p>Is there a way to do this or am I restricted to <code>IServiceProvider</code> interface even though I'm using Unity implementation?</p>
  
  <p>EDIT:</p>
  
  <p>So, I can't find a way to switch from <code>IServiceProvider</code> to <code>IUnityContainer</code> with something like a cast. So, I changed the above code a bit to return Unity container instead:</p>
  
  <pre><code>public IServiceProvider ConfigureServices()
  {
      var services = new ServiceCollection();
  
      // Register services here.
  
      // Unity container interface:
      var containerFactory = new ServiceProviderFactory(null);
      IUnityContainer container = containerFactory.CreateBuilder(services);
  
      return container;
  }
  </code></pre>
  
  <p>Now, I can use the usual Unity DI in the .Net Core App.</p>