我遇到一个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)
即使我确实在builder.Register(context => new SessionStateProvider()).As<IStateProvider>();
中注册了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堆栈跟踪的深度),我很感激任何帮助。
答案 0 :(得分:0)
嗨我不在VS 2010测试,因为我没有。但在我的VS 2013中工作正常。
public static class DependencyInjection
{
public static void Init()
{
// Create the container builder.
var builder = new ContainerBuilder();
//Register controllers
builder.RegisterControllers(Assembly.GetExecutingAssembly());
// Register other dependencies.
builder.RegisterType<SessionStateProvider>().As<IStateProvider>().InstancePerHttpRequest();
// Build the container.
var container = builder.Build();
//Configure ASP.NET MVC the dependency resolver
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
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);
}
}
public interface IStateProvider
{
object this[string key] { get; set; }
void Remove(string key);
}
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
DependencyInjection.Init();
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
public abstract class BaseController : Controller
{
protected IStateProvider SessionStateProvider { get; private set; }
public BaseController()
{
SessionStateProvider = DependencyResolver.Current.GetService<IStateProvider>();
}
}
public class HomeController : BaseController
{
public ActionResult Index()
{
SessionStateProvider["1"] = 2;
return View();
}
}
Autofac - &gt; https://www.nuget.org/packages/Autofac.Mvc4/