dependency injection - Web Api Start up Exceptions with IDependencyResolver implimentation -


i developing web api , decided use custom dependencyresolver. refer this [dependency injection web api controllers] article. working far in terms of dependency injection controllers. code snippet of configuration owin startup class

private void registerioc(httpconfiguration config) {     _unitycontainer = new unitycontainer();     _unitycontainer.registertype<iaccountservice, accountservice>();     .........     .........     config.dependencyresolver = new unityresolver(_unitycontainer); } 

but @ time when api starts first time resolutionfailedexception thrown (but catched) inside unityresolver's getservice method. here exception message

"exception occurred while: while resolving.  exception is: invalidoperationexception -  current type, system.web.http.hosting.ihostbufferpolicyselector,  **is interface , cannot constructed. missing type mapping?**" 

above same exception thrown following types

system.web.http.hosting.ihostbufferpolicyselector system.web.http.tracing.itracewriter system.web.http.metadata.modelmetadataprovider system.web.http.tracing.itracemanager system.web.http.dispatcher.ihttpcontrollerselector system.web.http.dispatcher.iassembliesresolver system.web.http.dispatcher.ihttpcontrollertyperesolver system.web.http.controllers.ihttpactionselector system.web.http.controllers.iactionvaluebinder system.web.http.validation.ibodymodelvalidator system.net.http.formatting.icontentnegotiator 

i know these resolutionfailedexception thrown because did not provide mappings in unity configuration above types.

now here question :-, if implement custom unity dependencyresolver need define mappings of above types , if need define corresponding default implementation types or there alternative way implement dependencyresolver. concerned though application running fine now, failing resolve above type can cause serious issue later. please help.

one final addition:- following types, same resolutionfailedexception thrown when make request action web api

system.web.http.dispatcher.ihttpcontrolleractivator system.web.http.validation.imodelvalidatorcache system.web.http.controllers.ihttpactioninvoker 

i running in same issue using unity webapi , owin/katana.

the solution me use unitydependencyresolver defined in unity.webapi nuget package instead of own custom implementation (like @omar alani above)

install-package unity.webapi 

note package try , add file named unityconfig.cs in app_start (the filename used myself).

in unityconfig.cs file package add code register container against globalconfiguration.configuration.dependencyresolver not want owin.

so instead of using:

globalconfiguration.configuration.dependencyresolver = new unitydependencyresolver(container); 

change use:

config.dependencyresolver = new unitydependencyresolver(container); 

for completeness:

my unityconfig.cs

public static class unityconfig {     public static void register(httpconfiguration config)     {         var container = new unitycontainer();          // mappings here          config.dependencyresolver = new unitydependencyresolver(container);     } } 

my startup.cs

[assembly: owinstartup(typeof(unitytest.businesslayer.api.apistartup))] namespace unitytest.businesslayer.api {     public partial class apistartup     {         public void configuration(iappbuilder app)         {              app.usecors(microsoft.owin.cors.corsoptions.allowall);              httpconfiguration httpconfig = new httpconfiguration();              unityconfig.register(httpconfig);              configureauth(app); //in app_start ->startup.auth              webapiconfig.register(httpconfig);              app.usewebapi(httpconfig);     }   } } 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -