20

I am trying to build an asp.net web api 2.0 application using VS 2012 and asp net web tools for vs 2012 on .Net 4.5.2 framework; when i try to run the application, Inheritance security rules violated while overriding member: 'Autofac.Integration.WebApi.AutofacWebApiDependencyResolver.BeginScope()'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.

I tried updating all the packages in the solution, adding [assembly: SecurityRules(SecurityRuleSet.Level1)] or [assembly: SecurityRules(SecurityRuleSet.Level2)] but none of them seems to work.

Please help me in resolving this issue I tried all the previous threads but none of them is working.

Thanks in advance.

Edit Below are the list of nugget packages that are referenced in my web api project

  1. Autofac.3.5.2
  2. Autofac.Configuration.3.3.0
  3. Autofac.Extras.DynamicProxy2.3.0.7
  4. Autofac.WebApi.3.1.0
  5. AutoMapper.4.2.1
  6. Castle.Core.3.3.3
  7. Microsoft.AspNet.Mvc.5.2.3
  8. Microsoft.AspNet.Razor.3.2.3
  9. Microsoft.AspNet.Web.Optimization.1.1.3
  10. Microsoft.AspNet.WebApi.5.2.3
  11. Microsoft.AspNet.WebApi.Client.5.2.3
  12. Microsoft.AspNet.WebApi.Core.5.2.3
  13. Microsoft.AspNet.WebApi.WebHost.5.2.3
  14. Microsoft.AspNet.WebPages.3.2.3
  15. Microsoft.Web.Infrastructure.1.0.0.0
  16. Moq.4.5.8
  17. Newtonsoft.Json.8.0.3
  18. WebGrease.1.6.0
Amigo
  • 323
  • 1
  • 3
  • 7
  • Why are you running your website in partial trust? Microsoft dropped partial trust support for everything in ASP.NET since .NET 4.0, so you don't have to expect support from maintainers of libraries that integrate with ASP.NET. – Steven May 31 '16 at 20:43
  • Hi Steven, Thanks for your reply, how can i run the application in full trust mode ? I don't see any setting in web.config as well. – Amigo May 31 '16 at 21:17
  • Are you trying to use Autofac 4 beta with classic ASP.NET? [Possibly a similar issue to this known issue.](https://github.com/autofac/Autofac.Mvc/issues/10). We didn't have the classic integration libraries updated for Autofac 4 yet. – Travis Illig Jun 01 '16 at 04:34
  • @TravisIllig No i am using Autofac.3.5.2 with Asp.net Web API 2.0, below are the nugget packages that i am using Autofac.3.5.2 Autofac.Configuration.3.3.0 Autofac.Extras.DynamicProxy2.3.0.7 Autofac.Mvc5.3.3.4 Autofac.WebApi.3.1.0 – Amigo Jun 01 '16 at 14:10
  • Might be good to update your question with the list of packages in a nice, readable format. Also would be good to mention you've got Web API and MVC in the same app. – Travis Illig Jun 01 '16 at 14:26
  • @TravisIllig My solution has only Web API project it doesn't have MVC, I have updated the question with list of packages that are installed in my solution. – Amigo Jun 01 '16 at 19:25
  • You do have MVC even if you're not using it. Note the package reference to Microsoft.AspNet.Mvc in your packages. – Travis Illig Jun 01 '16 at 19:59

1 Answers1

47

You need to use the Autofac.WebApi2 package with the latest classic Web API as outlined in the docs. The current release version of Autofac.WebApi2 is 3.4.0.

Travis Illig
  • 23,195
  • 2
  • 62
  • 85
  • 1
    Thanks @TravisIIIig I removed all the AutoFac nuget packages and added only AutoFac and AutoFac.Integration.WebAPI nuget packages only – Amigo Jun 02 '16 at 19:22
  • 7
    First `uninstall-package Autofac.WebApi -force` then `install-package Autofac.WebApi2` :) – Jalal Jul 07 '17 at 07:23
  • I am having the same issue with Autofac.WebApi v3.1.0 and Autofac.WebApi2 4.2.1 being installed on the same project. I am using the `RegisterApiControllers` method in the namespace `Autofac.Integration.WebApi`. That namespace seem to require both NuGet packages. – Michael Kniskern Apr 10 '19 at 16:43
  • 1
    You can't use both at the same time. – Travis Illig Apr 10 '19 at 16:46
  • So how do you use utilize the `RegsiterApiControllers` method to register API controllers for DI in Autofac.WebApi2? – Michael Kniskern Apr 10 '19 at 17:07
  • I removed NuGet package `Autofac.WebApi` and dropped the `Autofac.WepApi2` reference to v4.2.0 and it resolved the issue. But in v4.2.1 of WebApi2 the `Autofac.Integration.WebApi` namespace is not available anymore. How do you use the `RegisterApiController` method to set up DI in v4.2.1? – Michael Kniskern Apr 10 '19 at 17:34
  • 1
    It's there, check out [this example showing it in action](https://github.com/autofac/Examples/blob/master/src/WebApiExample.OwinSelfHost/Startup.cs). I would recommend cleaning your temp files - all obj, bin, .vs folders, any .user files, etc. Lots gets cached, switching like this can hose things up. – Travis Illig Apr 10 '19 at 18:04
  • Yes, I found out it happens if you have both Autofac.webapi2 and Autofac.webapi installed together. Remove the Autofac.webapi nuget. – Ronen Festinger Jul 24 '19 at 11:40
  • Severity Code Description Project File Line Suppression State Error Could not install package 'Autofac.WebApi2 6.0.1'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6.1', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author. – VJOY Apr 07 '21 at 06:41