trquth / autofac

Automatically exported from code.google.com/p/autofac
Other
0 stars 0 forks source link

Autofac cannot load AutofacWebTypesModule in 3.1.0 through 3.1.3 #459

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Dll versions being used:

Autofac.Mvc4 3.1.0
Autofac 3.1.0 (also tried 3.1.1 and 3.1.3)

What steps will reproduce the problem?

1. Start Visual Studio 2013
2. Create an empty web site project with MVC support.
3. In Global.asax.cs add the following lines in Application_Start:

var builder = new ContainerBuilder();
builder.RegisterModule(new AutofacWebTypesModule());
var container = builder.Build();

When you try to start the project you get the following exception:

System.FieldAccessException was unhandled by user code
  HResult=-2146233081
  Message=Attempt by method 'Autofac.Integration.Mvc.AutofacWebTypesModule.Load(Autofac.ContainerBuilder)' to access field 'Autofac.Integration.Mvc.AutofacWebTypesModule.CS$<>9__CachedAnonymousMethodDelegate17' failed.
  Source=Autofac.Integration.Mvc
  StackTrace:
       at Autofac.Integration.Mvc.AutofacWebTypesModule.Load(ContainerBuilder builder)
       at Autofac.Module.Configure(IComponentRegistry componentRegistry)
       at Autofac.ContainerBuilder.Build(IComponentRegistry componentRegistry, Boolean excludeDefaultModules)
       at Autofac.ContainerBuilder.Build(ContainerBuildOptions options)
       at WebApplication1.MvcApplication.Application_Start() in d:\Temp\WebApplication1\WebApplication1\Global.asax.cs:line 21
  InnerException: 

Original issue reported on code.google.com by mc...@carr-engineering.com on 18 Oct 2013 at 5:37

GoogleCodeExporter commented 8 years ago
Some additional information on this issue that may help:

I downloaded the Autofac sources and did some digging around. Just for fun I 
tried creating an instance of UrlHelper in AutofacWebTypesModule.Load() using 
the following line:

var helper = new UrlHelper(new RequestContext());

When you try to run that, you get the following exception:

System.MethodAccessException was unhandled by user code
  HResult=-2146233072
  Message=Attempt by security transparent method 'Autofac.Integration.Mvc.AutofacWebTypesModule.Load(Autofac.ContainerBuilder)' to access security critical method 'System.Web.Mvc.UrlHelper..ctor(System.Web.Routing.RequestContext)' failed.

Assembly 'Autofac.Integration.Mvc, Version=3.0.0.0, Culture=neutral, 
PublicKeyToken=17863af14b0044da' is marked with the 
AllowPartiallyTrustedCallersAttribute, and uses the level 2 security 
transparency model.  Level 2 transparency causes all methods in 
AllowPartiallyTrustedCallers assemblies to become security transparent by 
default, which may be the cause of this exception.
  Source=Autofac.Integration.Mvc
  StackTrace:
       at Autofac.Integration.Mvc.AutofacWebTypesModule.Load(ContainerBuilder builder) in d:\Temp\WebApplication1\Autofac\Core\Source\Autofac.Integration.Mvc\AutofacWebTypesModule.cs:line 172
       at Autofac.Module.Configure(IComponentRegistry componentRegistry) in d:\Temp\WebApplication1\Autofac\Core\Source\Autofac\Module.cs:line 78
       at Autofac.ContainerBuilder.Build(IComponentRegistry componentRegistry, Boolean excludeDefaultModules) in d:\Temp\WebApplication1\Autofac\Core\Source\Autofac\ContainerBuilder.cs:line 185
       at Autofac.ContainerBuilder.Build(ContainerBuildOptions options) in d:\Temp\WebApplication1\Autofac\Core\Source\Autofac\ContainerBuilder.cs:line 94
       at WebApplication1.MvcApplication.Application_Start() in d:\Temp\WebApplication1\WebApplication1\Global.asax.cs:line 21
  InnerException: 

I believe this is causing the issue above, but I'm not sure how to fix it.

Original comment by mc...@carr-engineering.com on 18 Oct 2013 at 6:25

GoogleCodeExporter commented 8 years ago
With regard to my comment above, I discovered that if you comment out

[assembly: AllowPartiallyTrustedCallers]

in ProductAssemblyInfo.cs, the bug disappears and everything works properly.

I don't know what implications this has on real-world deployment of this 
assembly, but it looks as if Microsoft is now preventing partially trusted 
callers in MVC 5.

Original comment by mc...@carr-engineering.com on 18 Oct 2013 at 6:28

GoogleCodeExporter commented 8 years ago
It looks like you're using MVC5, but the MVC4 packages. Try updating to the 
Autofac.Mvc5 package. It's currently in beta, so you'll need to allow 
pre-release packages.

As we have several successful users of the MVC5 packages, I'm going to close 
this issue. If the new packages don't solve the problem, let us know. Please 
include alkyl the versions of everything in your project so we can give you the 
most accurate assistance.

Original comment by travis.illig on 19 Oct 2013 at 12:05