所以,我有一个针对.NET framework 4.5.2的ASP.NET MVC / WebAPI 2项目,我试图将SimpleInjector
(DI)用于MVC控制器和WebAPI控制器的相同容器我之前没有遇到任何问题。
在运行时,当我尝试实例化SimpleInjectorWebApiDependencyResolver
时,我得到一个异常:
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
}
例外:
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.
我的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>
我完全被卡住,据我所知,我正在引用正确的版本,我的web.config中的DependentAssembly
元素中的版本号是正确的,{{1引用v5.2.3.0并尝试删除所有包,bin,obj和rebuilt。
我已阅读并尝试WebAPI OData 5.0 Beta - Accessing GlobalConfiguration throws Security Error,How do I resolve the error "Attempt by security transparent method 'System.Web.Http.GlobalConfiguration.get_Configuration()无济于事。
导致此错误的原因是什么?如何解决?