Search code examples
controllerbreeze

Why is my BreezeController constructed several times?


I would expect that my API controller that I use for Breeze is constructed only once. However, if I set a break point in the constructor, start my application and log in as a user... my controller gets constructed several times.

Where can I find a description of the work flow where the BreezeController is constructed? Is there one instances of the controller for each route?

  [BreezeController]
  public class DomainController : ApiController
  {
    private readonly IUnitOfWork _unitOfWork;
    private readonly ApplicationUserManager _userManager;

    public DomainController(IUnitOfWork unitOfWork)
    {
      _unitOfWork = unitOfWork;
      _userManager = HttpContext.Current.GetOwinContext()
                                .GetUserManager<ApplicationUserManager>();   
    }
  ...

Solution

  • The reason why this is constructed every time is that a BreezeController is just a Web Api Controller. Web Api Controllers are instantiated by default on every request. If you are interested in more info on the lifetime of Api Controllers and why they instantiate on every request there are several good posts on Stackoverflow about this. The BreezeController attribute just changes some of the filter providers on the API controller and also changes the JSON formatter to be one that works with Breeze Clients.