1

In the new project template for for ASP.NET MVC 5, there are 2 constructors for Account and Manage controllers.

public AccountController()
    {
    }

public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
    {
        UserManager = userManager;
        SignInManager = signInManager;
    }

I'm initializing some properties in the constructor and I've been initially confused which one to use. When stepping through the code I see only the first default constructor is ever called.

I'm wondering whether the 2nd controller is only a placeholder for Dependency Injection, which I'm not using currently. Or could Identity ever be calling it in the background for some other purposes when DI is not used? (I'm figuring based on going through the framework, that both UserManager and SignInManager are already built in Startup, so there should be no need to recreate them in the second constructor).

Turo
  • 1,537
  • 2
  • 21
  • 42
  • I've only ever use the second, parameterized, controller. I don't even include the default constructor. Yes, the services are registered in Startup.cs and are not created here, they are only _injected_ here. – nurdyguy Aug 15 '17 at 19:58
  • Hmm, I'm not yet convinced.. And actually I was just playing with it and saw "No parameterless constructor defined for this object." error when I did not include it. – Turo Aug 15 '17 at 20:12
  • Here is a highly trusted source example: https://github.com/IdentityServer/IdentityServer4.Quickstart.UI/blob/release/Quickstart/Account/AccountController.cs – nurdyguy Aug 15 '17 at 20:25
  • I just tested it and either, both or no constructors declared seem to work exactly the same. Not sure why I got this error earlier, cannot replicate it. – Turo Aug 15 '17 at 20:58
  • I just read this useful post: https://stackoverflow.com/questions/26671358/asp-net-mvc-identity-default-implementation. Though I'm worried author stating "How MVC decides which constructor to use is not clear ... ". When instantiating other properties it could be perhaps beneficial to do it in both constructors to be on the safe side. Unreasonable, but I don't find enough documentation to support otherwise. http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application. Or do DI. – Turo Aug 15 '17 at 21:28
  • The 'No parameterless constructor ...' error is a common and annoying one but it basically means the DI didn't work correctly. Yes, adding the default constructor usually solves the error but that actually has other repercussions on the DI. What controller are you extending? – nurdyguy Aug 15 '17 at 21:46

1 Answers1

0

The first constructor is there to not let the second one override it. I am not sure the first one has some internal use for Asp.Net Identity or not but definitely they wanted to keep that for some reasons and not let it be override.

Hadee
  • 1,392
  • 1
  • 14
  • 25