c# - Can't get Unity Bootstrapper for MVC working -
i have created basic web api project single controller , trying use unity mvc bootstrapper package nuget. unfortunately when try , visit url http://localhost/api/runtypes throws error
{"message":"an error has occurred.","exceptionmessage":"type 'se.flexapplication.webapi.controllers.runtypecontroller' not have default constructor","exceptiontype":"system.argumentexception","stacktrace":" @ system.linq.expressions.expression.new(type type)\r\n @ system.web.http.internal.typeactivator.create[tbase](type instancetype)\r\n @ system.web.http.dispatcher.defaulthttpcontrolleractivator.getinstanceoractivator(httprequestmessage request, type controllertype, func`1& activator)\r\n @ system.web.http.dispatcher.defaulthttpcontrolleractivator.create(httprequestmessage request, httpcontrollerdescriptor controllerdescriptor, type controllertype)"}
my controller class
public class runtypecontroller : apicontroller { private readonly istatictypesrepository _statictypesrepository; private readonly idatamodelconverter _datamodelconverter; private readonly ibusinessmodelconverter _businessmodelconverter; public runtypecontroller(istatictypesrepository statictypesrepository, idatamodelconverter datamodelconverter, ibusinessmodelconverter businessmodelconverter) { _statictypesrepository = statictypesrepository; _datamodelconverter = datamodelconverter; _businessmodelconverter = businessmodelconverter; } public runtype[] get() { return _statictypesrepository.getruntypes().select(r => _datamodelconverter.convert(r)).toarray(); } public httpresponsemessage post(runtype type) { _statictypesrepository.addruntype(_businessmodelconverter.convert(type)); _statictypesrepository.save(); var response = request.createresponse(system.net.httpstatuscode.created, type); return response; } }
and unity activator
[assembly: webactivatorex.preapplicationstartmethod(typeof(se.flexapplication.webapi.app_start.unitywebactivator), "start")] [assembly: webactivatorex.applicationshutdownmethod(typeof(se.flexapplication.webapi.app_start.unitywebactivator), "shutdown")] namespace se.flexapplication.webapi.app_start { /// <summary>provides bootstrapping integrating unity asp.net mvc.</summary> public static class unitywebactivator { /// <summary>integrates unity when application starts.</summary> public static void start() { var container = unityconfig.getconfiguredcontainer(); filterproviders.providers.remove(filterproviders.providers.oftype<filterattributefilterprovider>().first()); filterproviders.providers.add(new unityfilterattributefilterprovider(container)); dependencyresolver.setresolver(new unitydependencyresolver(container)); } /// <summary>disposes unity container when application shut down.</summary> public static void shutdown() { var container = unityconfig.getconfiguredcontainer(); container.dispose(); } } }
unity config class
public class unityconfig { #region unity container private static readonly lazy<iunitycontainer> container = new lazy<iunitycontainer>(() => { var container = new unitycontainer(); registertypes(container); return container; }); /// <summary> /// gets configured unity container. /// </summary> public static iunitycontainer getconfiguredcontainer() { return container.value; } #endregion /// <summary>registers type mappings unity container.</summary> /// <param name="container">the unity container configure.</param> /// <remarks>there no need register concrete types such controllers or api controllers (unless want /// change defaults), unity allows resolving concrete type if not registered.</remarks> public static void registertypes(iunitycontainer container) { try { //container.registerinstance<iunitofwork>(new produnitofwork()); container.registertype<ieventrepository, eventrepository>(); container.registertype<imodelrepository, modelrepository>(); container.registertype<isecurityrepository, securityrepository>(); container.registertype<istatictypesrepository, statictypesrepository>(); container.registertype<idatamodelconverter, datatomodelconverter>(); container.registertype<ibusinessmodelconverter, modeltodataconverter>(); } catch (exception ex) { console.writeline(ex.message); } } }
it seems controller isn't picking should using unity dependency resolver constructor injection. i've followed steps on tutorials online. have missed basic?
Comments
Post a Comment