Note that there are some explanatory texts on larger screens.

plurals
  1. POWeb API controller parameterized constructor called only once, parameterless constructor on subsequent requests
    primarykey
    data
    text
    <p>I'm attempting to use Unity to inject a dependency per this article:</p> <p><a href="http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver" rel="nofollow">http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver</a></p> <p>Here is what I have in my global.asax</p> <pre><code>void ConfigureApi(HttpConfiguration config) { var unity = new UnityContainer(); unity.RegisterType&lt;CustomerController&gt;(); unity.RegisterType&lt;TPS.Data.Can.IUnitOfWork, TPS.Data.Can.EFRepository.UnitOfWork&gt;(new HierarchicalLifetimeManager()); config.DependencyResolver = new IoCContainer(unity); } protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); ConfigureApi(GlobalConfiguration.Configuration); } </code></pre> <p>Here is my API controller:</p> <pre><code>public class CustomerController : ApiController { private TPS.Data.Can.IRepository&lt;tblCustomer&gt; _repo; private TPS.Data.Can.IUnitOfWork _uow; public CustomerController() { } public CustomerController(TPS.Data.Can.IUnitOfWork uow) { _uow = uow; _repo = uow.CustomerRepository; } // GET api/customer/5 public IEnumerable&lt;Customer&gt; Get() { string identity = HttpContext.Current.User.Identity.Name; //REFACTOR THIS if (String.IsNullOrWhiteSpace(identity)) identity = "chardie"; var customers = from c in _repo.Get() where c.SalesRep == identity select new Customer { IDCUST = null, CustCode = c.CustCode, CustName = c.CustName }; return customers.ToList(); } </code></pre> <p>This works when I first start debugging my application. If I set a breakpoint in the parameterized constructor, the breakpoint will be hit when I hit the Web API for the first time. When I hit refresh in my browser, the constructor does <em>not</em> get called, the dependency doesn't get injected, and the Get() action throws an exception because the expected repository is null.</p> <p>Can anyone tell me why my constructor isn't being called after the first request?</p> <p>Thanks!</p> <p>Chris</p> <p><strong>EDIT</strong></p> <p>FWIW, I removed the parameterless constructor entirely from the Web API controller, and on my second request to it, I get the exception:</p> <pre><code>Type 'TPS.Website.Api.CustomerController' does not have a default constructor </code></pre> <p>So it appears I'm getting my repo dependency injected on the first request, but after that every instantiation of the Web API controller is done through the parameterless constructor. </p>
    singulars
    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