升级 MVC 4 -> 5 时出现 Autofac TypeLoadException

Autofac TypeLoadException when upgrading MVC 4 -> 5

正在尝试将项目从 MVC3 升级到 MVC5, 3->4 部分进展顺利,我可以 运行 这个项目。

然而,在为 mvc5 更新可能所有内容之后, 我遇到了异常

Inheritance security rules violated by type: 'Autofac.Integration.Web.Mvc.AutofacControllerFactory'. Derived types must either match the security accessibility of the base type or be less accessible.

在 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);
        }

尝试以下方法

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