2

So, I have an ASP.NET MVC/WebAPI 2 project targeting .NET framework 4.5.2 and I am trying to use SimpleInjector (DI) with the same container for the MVC controllers and the WebAPI controllers and I have done this before without issues.

At runtime, when I try to instantiate my SimpleInjectorWebApiDependencyResolver I get an exception:

public static void Initialize()
{
    var container = new Container();

    InitializeContainer(container);
    container.Verify();
    DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
    GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container); //Exception thrown here
}

The exception:

An exception of type 'System.TypeAccessException' occurred in MyAssembly.dll but was not handled in user code

Additional information: Attempt by security transparent method 'System.Web.Http.GlobalConfiguration.get_Configuration()' to access security critical type 'System.Web.Http.HttpConfiguration' failed.

My packages.config:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Antlr" version="3.4.1.9004" targetFramework="net452" />
  <package id="bootstrap" version="3.0.0" targetFramework="net452" />
  <package id="jQuery" version="1.10.2" targetFramework="net452" />
  <package id="jQuery.Validation" version="1.11.1" targetFramework="net452" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.OData" version="5.7.0" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Tracing" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net452" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.Data.Edm" version="5.6.2" targetFramework="net452" />
  <package id="Microsoft.Data.OData" version="5.6.2" targetFramework="net452" />
  <package id="Microsoft.Data.Services.Client" version="5.6.2" targetFramework="net452" />
  <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.19.208020213" targetFramework="net452" />
  <package id="Microsoft.IdentityModel.Protocol.Extensions" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net452" />
  <package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" />
  <package id="Microsoft.Owin" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Owin.Security.OpenIdConnect" version="3.0.1" targetFramework="net452" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net452" />
  <package id="Microsoft.WindowsAzure.ConfigurationManager" version="2.0.3" targetFramework="net452" />
  <package id="Modernizr" version="2.6.2" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
  <package id="Owin" version="1.0" targetFramework="net452" />
  <package id="Respond" version="1.2.0" targetFramework="net452" />
  <package id="SimpleInjector" version="3.1.1" targetFramework="net452" />
  <package id="SimpleInjector.Extensions.ExecutionContextScoping" version="3.1.1" targetFramework="net452" />
  <package id="SimpleInjector.Integration.Web" version="3.1.1" targetFramework="net452" />
  <package id="SimpleInjector.Integration.Web.Mvc" version="3.1.1" targetFramework="net452" />
  <package id="SimpleInjector.Integration.WebApi" version="3.1.1" targetFramework="net452" />
  <package id="System.IdentityModel.Tokens.Jwt" version="4.0.0" targetFramework="net452" />
  <package id="System.IdentityModel.Tokens.ValidatingIssuerNameRegistry" version="4.5.1" targetFramework="net452" />
  <package id="System.Spatial" version="5.6.2" targetFramework="net452" />
  <package id="WebGrease" version="1.5.2" targetFramework="net452" />
  <package id="WindowsAzure.Storage" version="4.3.0" targetFramework="net452" />
</packages>

I am completely stuck, as far as I can see I am referencing the correct versions, the version numbers are correct in the DependentAssembly elements in my web.config, System.Web.Http references v5.2.3.0 and I have tried removing all packages, bin, obj and rebuilt.

I have read and tried WebAPI OData 5.0 Beta - Accessing GlobalConfiguration throws Security Error, and How do I resolve the error "Attempt by security transparent method 'System.Web.Http.GlobalConfiguration.get_Configuration() to no avail.

What causes this error and how can I fix it?

Community
  • 1
  • 1
Marcus
  • 7,013
  • 6
  • 50
  • 79
  • Are you trying to run your application in partial / medium trust? – Steven Dec 02 '15 at 08:27
  • I haven´t explicitly set any `securityPolicy` but default is **full**, is it not? – Marcus Dec 02 '15 at 08:29
  • Yes, default is full, but an 'Attempt by security transparent method to access security critical type' exception makes me think you're running in partial trust (which is [not supported anymore](https://stackoverflow.com/questions/16849801/is-trying-to-develop-for-medium-trust-a-lost-cause) by the web stack btw). I can't remember ever seeing such exception while running in full trust (but I might be wrong). – Steven Dec 02 '15 at 08:33
  • Do you know how I might have set the `securityPolicy` to partial by mistake other than adding the element in the web.config? – Marcus Dec 02 '15 at 08:44
  • If it's not in the web.config, the usual place is the machine config. – Steven Dec 02 '15 at 08:50
  • 5
    I solved it, but still I have no idea what the original problem was. The solution was to force a reinstall of `Microsoft.AspNet.WebApi.WebHost` as explained here: http://stackoverflow.com/a/26002407/1376691 But I would still like to know why this happened. – Marcus Dec 02 '15 at 08:51
  • For me it was a version mismatch of the Microsoft.AspNet.WebApi.* packages as explained here: https://stackoverflow.com/a/26047141/5413117 – S. Robijns Apr 18 '18 at 14:17

0 Answers0