Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register mvc controllers with autofac 4 beta fails #10

Closed
omriran opened this issue May 12, 2016 · 13 comments
Closed

Register mvc controllers with autofac 4 beta fails #10

omriran opened this issue May 12, 2016 · 13 comments
Labels

Comments

@omriran
Copy link

omriran commented May 12, 2016

Hi,

I'm currently working on an application that use autofac xml/json integration in its latest form. However If I try and inject into MVC Controllers (using the latest Autofac.Integration.Mvc and the normal registration process for Mvc controllers as defined in the documentation) it doesn't work. Actually, it throws this exception:

Attempt by security transparent method 'Autofac.Integration.Mvc.RequestLifetimeScopeProvider..ctor(Autofac.ILifetimeScope)' to access security critical type 'Autofac.ILifetimeScope' failed.

Assembly 'Autofac.Integration.Mvc, Version=3.3.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.

I also use api controllers injection and that works fine.

Is it occurring because of versioning issues? Or perhaps there might be another issue?

I'm currently using VS2013 .NET 4.5. It worth to mention that the mvc controllers are located in a different assembly than the one i'm using to configure autofac. Furthermore, each assembly has it's own autofac's module to register specific component such as controllers.

@tillig
Copy link
Member

tillig commented May 12, 2016

Are you using the right version of Autofac MVC integration? If you're using the latest standard ASP.NET MVC (not the new ASP.NET Core stuff) you should be referencing Autofac.Mvc5 as noted in the docs.We usually see this if someone is using ASP.NET MVC 5 but trying to reference an older version of Autofac MVC integration, like Autofac.Mvc3. We also sometimes find folks accidentally including multiple versions of Autofac MVC integration in their project - you should only have the one version.

@tillig tillig closed this as completed May 12, 2016
@omriran
Copy link
Author

omriran commented May 12, 2016

I'm using Autofac.Mvc5 integration 3.3.4 and Autofac.Mvc5.Owin integration 3.1.0
I don't have other Autofac.Mvc versions installed. I do however have several other autofac packages installed.

@tillig tillig reopened this May 12, 2016
@tillig
Copy link
Member

tillig commented May 12, 2016

You will probably have to post a reproduction. We do have several users of the MVC integration who don't see this. For example, issue #459 had almost the same question and it turned out to be some version mismatch problems. Same thing on the Web API side: issue #3 on the Web API integration also has the same error and the same cause - version mismatch problems.

Can you post your packages.config? That may help shed light here.

@tillig
Copy link
Member

tillig commented May 12, 2016

Also, if your application has any assembly-level security attributes, can you post those?

@omriran
Copy link
Author

omriran commented May 12, 2016

No assembly-level security attributes that I'm aware off.

Here is the web.config and all the references in the assembly:

screen shot 2016-05-12 at 22 43 43

screen shot 2016-05-12 at 22 44 09

@tillig
Copy link
Member

tillig commented May 12, 2016

I really need to see the packages.config file with the list of NuGet packages. Thanks!

@omriran
Copy link
Author

omriran commented May 12, 2016

Oh... sorry, a bit too much in front of the screen

screen shot 2016-05-12 at 22 48 27

@tillig
Copy link
Member

tillig commented May 12, 2016

It appears you have a lot of interesting combinations of things that may cause challenges.

  • Autofac 4.0.0 with earlier stuff. It may or may not cause issues since we're targeting new runtimes and new interfaces with that. We do have a version limitation on the other packages so they only take < 4.0.0 of core Autofac, but NuGet sees 4.0.0-beta as still less than 4.0.0 so it's not explicitly disallowed by the tooling. I raise it as a potential issue, not a guaranteed problem. In fact, we haven't actually released a version of the classic ASP.NET MVC integration that's compatible with 4.0 yet. You can track our .NET Core progress on #594. I do know that with Autofac 4 we stopped using the legacy security model since it's not compatible with .NET Core so that could be the source of the problem.
  • Mixing ASP.NET classic with ASP.NET Core. I gather that's to get the new configuration stuff in the mix, which is cool, but I'm not sure how well the ASP.NET Core beta 8 stuff is supposed to work with ASP.NET classic.
  • Older versions of things. I see, for example, ASP.NET MVC 5.1.0. That came out in 2014. The latest is 5.2.3, which came out at the beginning of 2015. I also see the Autofac version is -beta8 and we have an -rc1 out. Same for all the ASP.NET Core bits - older, which could be a problem given how fast this stuff has been moving.

What I'm guessing right now is that it's the Autofac 4 with the older integration that hasn't been updated yet to work with Autofac 4.

What I'd try: upgrade everything in your project as far as it can go (on released/stable versions, not beta). See if you still have the issue. If you do... well, you'll have to wait until RC2 is out so we can start looking at getting Autofac core more stable and worth updating the classic integration for.

Let me know how it goes.

@tillig
Copy link
Member

tillig commented May 12, 2016

(Sorry I missed the thing about you using the new xml/JSON config in the first post. It didn't click until just now.)

@omriran
Copy link
Author

omriran commented May 12, 2016

Thanks for the suggestions, will have a go at that. I'll post here if I have any success (or not)
Thanks again for the quick help and tips!!!

@omriran
Copy link
Author

omriran commented May 16, 2016

Hello again,
I've tried to follow your suggestions as best I've could. Unfortunately it still gives me the same exception.

Looking back at my post I've now realise that I've posted the wrong package.config info so I'll add the correct one here. It's a shame but I think I'll have to give on having xml/json configuration (I really liked it!) or alternatively not implementing mvc controller injection for now.

Is there another way to read configuration in Autofac 3.5.2? e.g. from app/web.config

screen shot 2016-05-16 at 21 50 24

@tillig
Copy link
Member

tillig commented May 24, 2016

Yeah, unfortunately you may be stuck until we can get the .NET Core stuff ironed out a bit more in the Autofac main library and then pushed through to some of these other integrations.

However, if you stick to the non-beta version of Autofac.Configuration, you can still use XML configuration. Look further down the documentation page at the legacy configuration section to see how that works. It's not as pretty as JSON but it gets the job done.

I'll leave this open so we can track it against the .NET Core releases and make sure things are working together.

@tillig
Copy link
Member

tillig commented Jul 1, 2016

I pushed Autofac.Mvc5 4.0.0-rc3-219 with compatibility for Autofac 4. Note there are some breaking changes in Autofac 4 that require a minimum of Autofac 4 now, so if you have other third-party assemblies that use Autofac requirements, you may have challenges with NuGet allowing you to upgrade.

@tillig tillig closed this as completed Jul 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants