TypeLoadException when using Autofac.Mvc5 (3.2.0) Integration

1.2k Views Asked by At

Getting the TypeLoadException when using some of the registration extension methods.

builder.RegisterModelBinders(Assembly);
builder.RegisterModelBinderProvider();
builder.RegisterFilterProvider();

The following respective exceptions are thrown, however builder.RegisterControllers(Assembly) works fine.

TypeLoadException: Inheritance security rules violated while overriding member: 'Autofac.Integration.Mvc.AutofacModelBinderProvider.GetBinder(System.Type)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.
    at Autofac.Integration.Mvc.RegistrationExtensions.RegisterModelBinders(ContainerBuilder builder, Assembly[] modelBinderAssemblies)

TypeLoadException: Inheritance security rules violated while overriding member: 'Autofac.Integration.Mvc.AutofacModelBinderProvider.GetBinder(System.Type)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.
    at Autofac.Integration.Mvc.RegistrationExtensions.RegisterModelBinderProvider(ContainerBuilder builder)

TypeLoadException: Inheritance security rules violated by type: 'Autofac.Integration.Mvc.AutofacFilterProvider'. Derived types must either match the security accessibility of the base type or be less accessible.
    at Autofac.Integration.Mvc.RegistrationExtensions.RegisterFilterProvider(ContainerBuilder builder)

Here are what the relevant packages packages.config look like:

<package id="Autofac" version="3.3.0" targetFramework="net45" />
<package id="Autofac.Mvc5" version="3.2.0" targetFramework="net45" />
<package id="Autofac.Wcf" version="3.0.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc" version="5.1.0" targetFramework="net45" />

If it's any consolation, the previous Autofac.Mvc5 package was throwing the same error.

These are the binding redirects for Autofac:

<dependentAssembly>
    <assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
2

There are 2 best solutions below

0
On

I rebuilt the test project's .csproj and the test passes now, I have not isolated the exact issue. If I find it, I will post it here.

0
On

I had this error after upgrading from MVC 4 to MVC 5 , it didn't update the nuget package for autofac mvc integration which was stil on mvc 4 , so uninstall/reinstall under nuget of all autofac libraries fixed this error for me.