c# - Register types using Unity in SPA (MvcControllers and WebApiControllers) -
i create spa application. there mvccontrollers , webapicontrollers.
i install unity using package manager , register services using unity.
when test on mvccontroller - works. when start test on webapicontroller - error. after research found answer here : http://www.asp.net/web-api/overview/advanced/dependency-injection
but have question-problem : should register services - in unityconfig file or in webapiconfig file or in both?
registering of types in webapiconfig :
var container = new unitycontainer(); container.registertype<iproductrepository, productrepository>(new hierarchicallifetimemanager()); config.dependencyresolver = new unityresolver(container);
registering of types in unityconfig:
public static void registertypes(iunitycontainer container) { // note: load web.config uncomment line below. make sure add microsoft.practices.unity.configuration using statements. // container.loadconfiguration(); container.registertype<iproductrepository, productrepository>(new hierarchicallifetimemanager()); }
you should create 1 single unity container, way, have register services in 1 single place.
i suggest registering in unityconfig, , changing webapiconfig use existing container instead of creating new one.
replace 3 lines you mention in question single line :
config.dependencyresolver = new unityresolver(unityconfig.getconfiguredcontainer());
Comments
Post a Comment