尝试将项目从MVC3升级到MVC5, 3> 4部分进展顺利,我可以运行该项目。
但是,更新可能 mvc5的所有内容后, 我得到了一个例外
类型违反的继承安全规则:'Autofac.Integration.Web.Mvc.AutofacControllerFactory'。派生类型必须与基类型的安全可访问性匹配,或者不易访问。
在Application_Start()的这一行
InitContainerProvider();
被调用的方法:
protected void InitContainerProvider()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<PeriodsProxy>().As<IPeriods>().AsWcfProxy();
_containerProvider = new ContainerProvider(builder.Build());
var factory = new AutofacControllerFactory(_containerProvider);
ControllerBuilder.Current.SetControllerFactory(factory);
}
答案 0 :(得分:1)
尝试以下
protected void InitContainerProvider()
{
var builder = new ContainerBuilder();
builder.RegisterModule<AutofacWebTypesModule>();
builder.RegisterType<PeriodsProxy>().As<IPeriods>().AsWcfProxy();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
_containerProvider = new ContainerProvider(builder.Build());
DependencyResolver.SetResolver(new AutofacDependencyResolver(_containerProvider.ApplicationContainer.BeginLifetimeScope()));
}
此示例使用包NuGet: Autofac.Mvc5