Note that there are some explanatory texts on larger screens.

plurals
  1. POWebAPI: Accessing Child Container as a Service Locator
    primarykey
    data
    text
    <p>In normal ASP.MVC projects we configure the dependency resolver with Unity and the Unity.Mvc3 package from <a href="http://unitymvc3.codeplex.com/" rel="nofollow">http://unitymvc3.codeplex.com/</a></p> <p>We have this test service registered with a <a href="http://msdn.microsoft.com/en-us/library/microsoft.practices.unity.hierarchicallifetimemanager%28v=pandp.20%29.aspx" rel="nofollow">HierarchicalLifetimeManager</a></p> <pre><code>container.RegisterType&lt;ITestService, TestService&gt;(new HierarchicalLifetimeManager()); </code></pre> <p>And we hook up the container with Mvc in Global.asax.cs:</p> <pre><code>System.Web.Mvc.DependencyResolver.SetResolver(new Unity.Mvc3.UnityDependencyResolver(container)); </code></pre> <p>And we run this test controller:</p> <pre><code>public class TestController : Controller { private readonly ITestService _service; public TestController(ITestService service) { this._service = service; } public ActionResult Test() { var locatedService = System.Web.Mvc.DependencyResolver.Current.GetService&lt;ITestService&gt;(); if (_service == locatedService) return View("Success - Same Service");//This is always the result in an MVC controller else throw new Exception("Failure - Different Service Located");//This is never the result in an MVC controller } } </code></pre> <p>However, on this project we are adding a number of WebAPI controllers.</p> <p>We have this configuration in global.asax.cs (using <a href="http://unitywebapi.codeplex.com/" rel="nofollow">http://unitywebapi.codeplex.com/</a> for now. But I am open to suggestions):</p> <pre><code>System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container); </code></pre> <p>We have created an <code>ApiTestController</code> similar to <code>TestController</code> inheriting from <code>ApiController</code> rather than from <code>Controller</code>. However, the ApiTestController fails its test. I understand that the <code>System.Web.Mvc.DependencyResolver</code> class and the <code>System.Web.Mvc.DependencyResolver.Current</code> property are specific to Mvc. But does WebAPI have an equivalent?</p> <p><code>System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver.GetService</code> does not work because the <code>System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver</code> instance is the parent container that I configured. It is not the child controller that was used to inject the ITestService into the constructor.</p> <p>This user seems to have a similar problem: <a href="http://unitywebapi.codeplex.com/discussions/359413" rel="nofollow">http://unitywebapi.codeplex.com/discussions/359413</a> But I feel that this probably has more to do with ASP.NET's WebAPI than it has to do with Unity.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload