Why is BaseController's overloaded constructor not being executed?
I have a base controller which is defined as follows, but the constructor that takes the ISiteService
never gets executed:
public class BaseController : Controller
{
private ISiteService _siteService;
public BaseController() {}
public BaseController(ISiteService siteService)
{
_siteService = siteService; // this never gets hit..
}
protected override void Initialize(RequestContext rc)
{
string host = ((rc.HttpContext).Request).Url.Host;
Site site = _siteService.GetSiteByHost(host); // so _siteService is null...
base.Initialize(rc);
}
}
Can somebody tell me why this would be the case? What do I need to do to get this constructor to execute?
All controllers that implement BaseController have constructors that take various parameters supplied by StructureMap, and all those constructors are executed.
I don't know if it's relevant but this is how I'm configuring StructureMap for my dependency injection.
private void ConfigureNonOptionalDependencies()
{
// all other dependencies are registered same as this,
// and the constructors all get hit
ForRequestedType<ISiteService>()
.TheDefaultIsConcreteType<SiteService>();
}
I'm unfamiliar with StructureMap, so I don't know if it has anything to do with this issue, or if it's more of an MVC problem. Or is it even possible for this? Thanks
edit:
also, I've tried this:
public class GroupController : BaseController
{
private readonly IGroupService _groupService;
private readonly ISiteService _siteService;
publi开发者_如何学JAVAc GroupController() {}
public GroupController(
ISiteService siteService
): base(siteService)
{
_siteService = siteService;
}
}
Do your subclass constructors include a call to base
?
e.g.
public MyController(ISiteService siteService) : base(siteService)
{
//do some MyController specific stuff
}
If you are already loading the controllers via structuremap, why not just eliminiate the parameterless constructor? You don't need it anymore and it is probably confusing something somewheres . . .
Structure Map NuGet is different for MVC and API projects. Seems like you have installed NuGet only for MVC Project.
Try Installing StructureMap.WebApi2
精彩评论