I have an controller that use dependency injection:
public FeedController(IFeedProcessor feedProcessor)
{
_feedProcessor = feedProcessor;
}
This my config code:
public static void Config()
{
ObjectFactory.Initialize(x => x.Scan(scan =>
{
x.For<IFeedProcessor>().Use<FeedProcessor>();
}));
}
My FeedProcessor class has parameter constructor:
public FeedProcessor(IFeedParserFactory ifeedParserFactory)
{
_ifeedParserFactory = ifeedParserFactory;
}
This StructureMapControllerFactory :
public class StructureMapControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
if (requestContext.HttpContext.Request.Url != null)
throw new InvalidOperationException(string.Format("Page not found: {0}", requestContext.HttpContext.Request.Url.AbsoluteUri.ToString(CultureInfo.InvariantCulture)));
return ObjectFactory.GetInstance(controllerType) as Controller;
}
}
and i get this error: Type 'Api.Controllers.FeedController' does not have a default constructor How can i config structure map for this scenario?
WebApi does not use the DefaultControlFactory to create instances of Api controllers. Instead, it uses DefaultHttpControllerSelector. However, unless you have a specific reason to create a controller factory, I would instead use the Dependency Resolver system built into MVC.
http://buchanan1966.tumblr.com/post/2192279804/mvc3-using-dependencyresolver-with-structuremap
http://marisks.net/2012/08/19/configuring-structuremap-in-aspnet-webapi/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With