I spent yesterday retrofitting StructureMap into an ASP.Net MVC site that I wrote earlier in the year. I ran into a number of minor issues that made me stop and think, and decided to post them here for future reference.
I had already added a constructor to each of my controller classes that allowed services to be passed in as parameters (as it improves testability), so was pretty well prepared for letting StructureMap AutoWire the services as needed.
The site consumed implementations of three interfaces I had defined:
I configured default instances of the implementations in the BootStrapStructureMap procedure which I called from the Application_Start method in the Global.asax:Good times… all was fine until I tried to call a method on a controller that required authentication.The following exception was raised: Where AssemblyName is the name of my applications assembly.
I’d originally built the site using the “out of the box” AccountController class which is added by default in the ASP.Net MVC project template. This class calls a MembershipProvider object to provide for account authentication. I’d configured the application to use the ActiveDirectoryMembershipProvider in production and I wired up a dummy MembershipProvider class for use during development and testing.
Within the AccountController.cs file there are a number of other objects declared:
- AccountMembershipService (Implements IMembershipService)
- FormsAuthenticationService (Implements IFormsAuthentication)