我在使用 MVC4 项目时遇到问题,该项目在一台计算机(VS 2012)上运行良好,但在另一台计算机(VS 2010)上运行良好。
(我希望VS版本冲突不是问题,但该项目最初来自VS2010计算机,我在2012年做了一些工作,现在我试图将它放回2010年。)
我收到“确保控制器具有无参数的公共构造函数”错误,但我确实认为我的控制器具有默认值:
public class HomeController : AuthorizedControllerBase
{
public ViewResult Index()
{
return View();
}
}
引发错误的代码在我的 ControllerFactory 中:
public class ControllerFactory : DefaultControllerFactory
{
private readonly IStateProvider _sessionStateProvider;
public ControllerFactory(IStateProvider aStateProvider)
{
_sessionStateProvider = aStateProvider;
}
public override IController CreateController
(System.Web.Routing.RequestContext requestContext, string controllerName)
{
IController controller = base.CreateController(requestContext, controllerName);
if (controller is BaseController)
{
var baseController = (BaseController)controller; // ***This line***
baseController.SessionData = _sessionStateProvider;
}
return controller;
}
}
我看到了一个与此类似的问题,其中接口实现者没有公共构造函数,但同样,我认为我的确实有默认的:
public class SessionStateProvider : IStateProvider
{
public object this[string key]
{
get
{
return HttpContext.Current.Session[key];
}
set
{
HttpContext.Current.Session[key] = value;
}
}
public void Remove(string key)
{
HttpContext.Current.Session.Remove(key);
}
}
根据对其他问题的回答和堆栈跟踪,我正在研究 Autofac 问题。我刚刚做了Install-Package Autofac
并且在项目中有最新的(3.0.2),堆栈跟踪看起来像这样:
InnerException: Autofac.Core.DependencyResolutionException
Message=An exception was thrown while executing a resolve operation. See the InnerException for details. ---> Common Language Runtime detected an invalid program. (See inner exception for details.)
Source=Autofac
StackTrace:
at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)
at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable`1 parameters)
at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType, IEnumerable`1 parameters)
at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType)
at Autofac.Integration.Mvc.AutofacDependencyResolver.GetService(Type serviceType)
at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
InnerException: System.InvalidProgramException
Message=Common Language Runtime detected an invalid program.
Source=Autofac
StackTrace:
at Autofac.Core.Activators.Reflection.ConstructorParameterBinding..ctor(ConstructorInfo ci, IEnumerable`1 availableParameters, IComponentContext context)
at Autofac.Core.Activators.Reflection.ReflectionActivator.<>c__DisplayClass5.<GetConstructorBindings>b__4(ConstructorInfo ci)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Registration.ExternalRegistrySource.<>c__DisplayClass8.<RegistrationsFor>b__3(IComponentContext c, IEnumerable`1 p)
at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)
即使我确实像这样注册 IStateProvider 也会发生这种情况:builder.Register(context => new SessionStateProvider()).As<IStateProvider>();
在ContainerBuilder.BuildContainer()
. 在 Global.asax 中,我还注册了 ControllerFactory:
protected void Application_Start()
{
Container = ContainerBuilder.BuildContainer();
var autofacDependencyResolver = new AutofacDependencyResolver(Container);
DependencyResolver.SetResolver(autofacDependencyResolver);
ControllerBuilder.Current.SetControllerFactory(new ControllerFactory(Container.Resolve<IStateProvider>()));
Application_Start_MVC();
}
我不确定尝试解决此问题的下一个可能步骤是什么(我已经超出了解析 Autofac 堆栈跟踪的深度),我将不胜感激。