我有兩個具有相同配置的MVC項目,它們使用Unity進行依賴注入。僅在一個控制器上依賴注入失敗
第一個有10個控制器,第二個有20個控制器,並且依賴注入完全適用於所有這些,並且已經運行了一年多了。
今天,我想1個控制器從第一個項目複製到第二個,當我運行它,我得到:
[MissingMethodException: No parameterless constructor defined for this object.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +113
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232
System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
System.Activator.CreateInstance(Type type) +6
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +55
[InvalidOperationException: An error occurred when trying to create a controller of type 'Web.Admin.Controllers.ApplyController'. Make sure that the controller has a parameterless public constructor.]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +179
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +197
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +49
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
看起來好像該控制器僅適用的DefaultcontrollerActivator而不是團結之一。
我已經去了web.config超過一個小時,它似乎很好,統一註冊和所有的依賴項,和Application_Start是相同的第一個項目,使用相同的控制器。 我不可能想象爲什麼除了一個控制器之外,一切都會起作用。
我試圖簡化控制器,以袒露基本知識,這仍然發生。
我在下面發佈一些代碼,但我不認爲這會有所幫助,因爲我有一個相同的實例可以工作,而一個不可以。無論哪種方式的應用開始看起來是這樣的:
var container = Ioc.ContainerWrapper.Container as IUnityContainer;
container
.ConfigureAutoRegistration()
.ExcludeAssemblies(t => !t.FullName.StartsWith("Alpari."))
.Include(If.ImplementsITypeName, Then.Register())
.ApplyAutoRegistration();
container.RegisterType<INHibernateContext, NHibernateContext>();
container.RegisterType<HttpContextBase>(new InjectionFactory(x => new HttpContextWrapper(HttpContext.Current)));
container.RegisterType<IAuthentication, FormsAuthenticationWrapper>();
container.RegisterType<IApplyService, ApplyService>();
/* register loads of interfaces */
AreaRegistration.RegisterAllAreas();
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new LocalizedViewEngine());
DependencyResolver.SetResolver(new UnityDependencyResolver());
的依賴解析器是用在這裏,這適用於除ApplyController所有控制器,這似乎是試圖使用默認的解析器。
構造有10個接口,但我想只是這樣做,我也得到了同樣的錯誤:
public ApplyController(IApplyService applyService)
{
this.applyService = applyService;
}
在這一點上我需要建議,什麼可能是錯誤的或如何調試這一點。
Ioc無法解析一個或多個對象已在ApplyController的構造函數中定義。您還應該發佈ApplyController的構造函數。 –
你確定嗎?我試着只使用一個接口加載應用程序控制器,並且我遇到了同樣的問題。 – Nick
你在ApplyController中調用了什麼接口對象? –