1

I'm updating an older project to use .net 471 instead of 462 and the most recent version of all nuget packages.

I've had a lot of versioning issues to plow through, but I finally got a project that builds.

But, when I execute the programming I get this exception:

System.TypeAccessException: 'Attempt by security transparent method 'Autofac.Integration.WebApi.RegistrationExtensions.RegisterApiControllers(Autofac.ContainerBuilder, System.Reflection.Assembly[])' to access security critical type 'Autofac.Builder.IRegistrationBuilder`3' failed.

Followed by

Assembly 'Autofac.Integration.WebApi, 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.'

That happens when calling:

builder.RegisterApiControllers(ThisAssembly);

Where builder is an Autofac ContainerBuilder.

Question

Like the subject says, it must be some kind of conflict between SecurityCritical and AllowPartiallyTrustedCallersAttribute but I don't know how I've introduced it and what the best action is, so

What's going on and how can I fix this?

Update

Going further on JLe's suggestion, I removed the webapi reference and used webapi2 instead. I got a related error on my logging setup, but since that's not "core functionality" I skipped it for now (I will need to get back to that) to see what I'd get. I get to the browser screen, which gives me this:

Method 'my.namespace.MyController+d__6.MoveNext()' is security transparent, but is a member of a security critical type.

I tried putting [SecurityCritical] on the controller class, but apparently you're not allowed to do async calls in a class with that attribute:

Error CS4031 Async methods are not allowed in an Interface, Class, or Structure which has the 'SecurityCritical' or 'SecuritySafeCritical' attribute.

But I can't fix that, because I'm doing async calls to the backend (NServicebus ESB).

Spikee
  • 3,967
  • 7
  • 35
  • 68
  • 1
    Are you using https://www.nuget.org/packages/Autofac.WebApi2 or https://www.nuget.org/packages/Autofac.WebApi? – JLe Nov 26 '18 at 13:29
  • 1
    I'm just guessing, but the Autofac.WebApi2 package is for WebApi2, so I though maybe the old package created some version conflict which lead to your exception. Could you try remove the old one? – JLe Nov 26 '18 at 13:34
  • I just tried, but webapi is needed for that `RegisterApiControllers` and `AutofacWebApiDependencyResolver`. – Spikee Nov 26 '18 at 13:35
  • Have you tried cleaning your solution? The WebApi2 package should contain those as well, version 4.2.0. The actual namespace of it is the same, Autofac.Integration.WebApi even though it's version 2. – JLe Nov 26 '18 at 13:46
  • @JLe: See update. – Spikee Nov 26 '18 at 14:45
  • You should probably describe what .NET Framework you're on, which other packages and versions (e.g., Web API, etc.) you're using, and so on. Also, this might help: https://stackoverflow.com/questions/5055632/net-4-allowpartiallytrustedcallers-attribute-and-security-markings-like-secur/5073044#5073044 – Travis Illig Nov 26 '18 at 21:13
  • 1
    @JLe: Your suggestion about cleaning up the packages ultimately fixed it (by making sure I was using webapi2). Could you write an answer so I can accept it? – Spikee Nov 28 '18 at 06:38

1 Answers1

4

Make sure you're using nuget.org/packages/Autofac.WebApi2 as that one if for WebApi2, and not the older one called just Autofac.WebApi (the namespaces are the same though, Autofac.Integration.WebApi).

JLe
  • 2,835
  • 3
  • 17
  • 30