c# - Why is the controller's parameterless constructor called and not the one with the biggest number of parameters -
i have had website asp.net mvc solution
my controller had 2 constructors: 1 parameterless , 1 few parameter.
the code used go through ctor parameters, injection them di using unity container.
i have merged website one.
now controller initialized via parameterless ctor.
what missing? code:
the initializer code executed.
public static class initializer { private static bool isinitialize; private static readonly object lockobj = new object(); private static iunitycontainer defaultcontainer = new unitycontainer(); static initializer() { initialize(); } public static void initialize() { if (isinitialize) return; lock (lockobj) { iunitycontainer container = defaultcontainer; //registering unity web api globalconfiguration.configuration.dependencyresolver = new unity.webapi.unitydependencyresolver(container); //registering unity mvc dependencyresolver.setresolver(new unitydependencyresolver(container)); container.registertype<icritierialoader, critierialoader>(); container.registertype<iappsloader, appsloader>(); container.registertype<isavetesttobd, savetesttodb>(); container.registertype<itestoperations, testoperations>(); container.registertype<iservicesmanager, servicesmanager>(); container.registertype<imamconfigurationmanager, mamconfigurationsmanager>(); container.registertype<iappsdatamanager, appsdatamanager>(); container.registertype<imamconfiguration_v1manager, mamconfiguration_v1manager>(); container.registertype<ictidprod_v1manager, ctidprod_v1manager>(); container.registertype<ictidsaliasmanager, ctidsaliasmanager>(); container.registertype<idalfactory, dalfactory>(); container.registertype<idal<appsdatabl>, appsdatadal>(); container.registertype<iapplicationlogger, log4netlogger>(); container.registertype<idal<mamconfigurationbl>, mamconfigurationbldal>(); container.registertype<idal<conduit.mam.mamdbentityframework.mamconfiguration>, mamconfigurationdal>(); container.registertype<idal<addservicebl>, newservice>(); container.registertype<ialias_v1uitoefconvertor, alias_v1uitoefconvertor>(new containercontrolledlifetimemanager()); container.registertype<imamconfiguration_v1uitoefconvertor, mamconfiguration_v1uitoefconvertor>(new containercontrolledlifetimemanager()); container.registertype<ictidprodvalidator, ctidprodvalidator>(new containercontrolledlifetimemanager()); container.registertype<igeoprodvalidator, geoprodvalidator>(new containercontrolledlifetimemanager()); container.registertype<iconstlistsprovider, constlistsprovider>(new containercontrolledlifetimemanager()); container.registertype<ientitytouiconvertor, entitytouiconvertor>(); if (!isinitialize) { isinitialize = true; } } } }
.
protected void application_start() { log.application.warn("application started", "application started"); webapiconfig.configure(globalconfiguration.configuration); arearegistration.registerallareas(); filterconfig.registerglobalfilters(globalfilters.filters); routeconfig.registerroutes(routetable.routes); bundleconfig.registerbundles(bundletable.bundles); initializer.initialize();
.
[authorize] public class ctidprodcontroller : sapcontroller { #region members private iapplicationlogger mapplicationlog { get; set; } //private imamconfiguration_v1manager mmamconfigurationmanager { get; set; } private icritierialoader mcritierialoader { get; set; } private iappsloader mappsloder { get; set; } private imamconfiguration_v1uitoefconvertor mmamconfiguration_v1uitoefconvertor { get; set; } private ctidprodmodel mctidprodmodel { get; set; } private iconstlistsprovider mconstlistsprovider { get; set; } private ictidprod_v1manager mctidprod_v1manager { get; set; } private ictidsaliasmanager mctidsaliasmanager { get; set; } #endregion #region ctor public ctidprodcontroller() { } public ctidprodcontroller(iapplicationlogger logger, ictidprod_v1manager ctidprod_v1manager, icritierialoader critierialoader, iappsloader appsloder, imamconfiguration_v1uitoefconvertor mamconfiguration_v1uitoefconvertor, iconstlistsprovider constlistsprovider, ictidsaliasmanager ctidsaliasmanager) { mapplicationlog = logger; mctidprod_v1manager = ctidprod_v1manager; mcritierialoader = critierialoader; mappsloder = appsloder; mmamconfiguration_v1uitoefconvertor = mamconfiguration_v1uitoefconvertor; mconstlistsprovider = constlistsprovider; mctidsaliasmanager = ctidsaliasmanager; fillmodel(mconstlistsprovider); } #endregion }
the new website code: injection code:
public class ninjectdependencyscope : idependencyscope { private iresolutionroot resolver; internal ninjectdependencyscope(iresolutionroot resolver) { contract.assert(resolver != null); this.resolver = resolver; } public void dispose() { idisposable disposable = resolver idisposable; if (disposable != null) disposable.dispose(); resolver = null; } public object getservice(type servicetype) { if (resolver == null) throw new objectdisposedexception("this", "this scope has been disposed"); return resolver.tryget(servicetype); } public ienumerable<object> getservices(type servicetype) { if (resolver == null) throw new objectdisposedexception("this", "this scope has been disposed"); return resolver.getall(servicetype); } } public class ninjectdependencyresolver : ninjectdependencyscope, idependencyresolver { private ikernel kernel; public ninjectdependencyresolver(ikernel kernel) : base(kernel) { this.kernel = kernel; } public idependencyscope beginscope() { return new ninjectdependencyscope(kernel.beginblock()); } }
like darren davies pointed maybe missing attributes. unity takes default in fact constructor parameters, sure if have more 1 ctor, should decorate desired 1 injectionconstructor attribute! hope helps. kind regards
Comments
Post a Comment