asp.net web api2 - How to inject UrlHelper in Web API 2 using StructureMap.WebApi2 nuget package -
i using structuremap.webapi2 nuget package web api 2 project managing depedency injection. web api controllers use constructor injection inject urlhelper dependency should resolved structuremap ioc. trying following approach set urlhelper web api controller:
public class foocontroller : apicontroller { private urlhelper _urlhelper; public modelfactory(httprequestmessage request) { _urlhelper = new urlhelper(request); } }
but above code getting following error:
no default instance registered , cannot automatically determined type 'system.net.http.httpmethod' there no configuration specified system.net.http.httpmethod
can suggest me best possible ways resolve above issue?
your problem structuremap tries resolve greediest constructor first, in instance taking @ source code httprequestmessage
reveals following constructors:
public httprequestmessage(httpmethod method, string requesturi); public httprequestmessage(httpmethod method, uri requesturi);
this httpmethod
issue coming from. structuremap tries create instance of httprequestmessage
has no idea how resolve method
, requesturi
dependencies.
in instance need manually configure dependency within structuremap configuration knows how create instance of httprequestmessage
so:
this.for<httprequestmessage>().use(new httprequestmessage());
alternatively, create instances of overloaded constructors you'll need manually resolve overloaded dependencies (i recommend using factory in order keep sm configuration nice , clean. here example of how can this).
Comments
Post a Comment