WebAPI OData 5.0 Beta - 訪問 GlobalConfiguration 引發安全錯誤
我最近安裝了 WebApi OData 5.0 框架的預發布版本,以使用新的 $expand 支持。一切正常,但我在 App_Start 上遇到了一個奇怪的異常。
Attempt by security transparent method 'System.Web.Http.GlobalConfiguration.get_Configuration()' to access security critical type 'System.Web.Http.HttpConfiguration' failed.源錯誤:
Line 12: protected void Application_Start() Line 13: { Line 14: WebApiConfig.Register(GlobalConfiguration.Configuration); // <-- Line 15: }堆棧跟踪:
[TypeAccessException: Attempt by security transparent method 'System.Web.Http.GlobalConfiguration.get_Configuration()' to access security critical type 'System.Web.Http.HttpConfiguration' failed.] System.Web.Http.GlobalConfiguration.get_Configuration() +0 API.WebApiApplication.Application_Start() in Global.asax.cs:14 [HttpException (0x80004005): Attempt by security transparent method 'System.Web.Http.GlobalConfiguration.get_Configuration()' to access security critical type 'System.Web.Http.HttpConfiguration' failed.] System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +12863325 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +175 System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +304 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +404 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +475 [HttpException (0x80004005): Attempt by security transparent method 'System.Web.Http.GlobalConfiguration.get_Configuration()' to access security critical type 'System.Web.Http.HttpConfiguration' failed.] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +12880068 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +159 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +12721257Google透露的很少。
按給定順序在包管理器控制台中執行以下命令:
Uninstall-Package Microsoft.AspNet.Mvc.FixedDisplayModes Update-Package Microsoft.AspNet.Mvc -Pre Update-Package Microsoft.AspNet.WebApi -Pre Update-Package Microsoft.AspNet.WebApi.Tracing現在,對 web.config 應用以下更改:
- 在項目的 Web.config 中,將應用設置網頁的值更新為 3.0.0.0
- 在 web.config 中的執行時部分下,檢查配置的每個程序集的版本並將其更新為添加到項目中的程序集版本。以下是我的 web.config 中的更新配置:
<runtime>
<assemblyBinding xmlns=“urn:schemas-microsoft-com:asm.v1”>
<dependentAssembly>
<assemblyIdentity name=“System.Web.Helpers” publicKeyToken=“31bf3856ad364e35” />
<bindingRedirect oldVersion=“1.0.0.0 -3.0.0.0” newVersion=“3.0.0.0” />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name=“System.Web.Mvc” publicKeyToken=“31bf3856ad364e35” />
<bindingRedirect oldVersion=“1.0.0.0-5.0. 0.0” newVersion=“5.0.0.0” />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name=“System.Web.WebPages” publicKeyToken=“31bf3856ad364e35” />
<bindingRedirect oldVersion=“1.0.0.0-3.0.0.0” newVersion=“3.0.0.0” />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name=“EntityFramework” publicKeyToken=“b77a5c561934e089” />
<bindingRedirect oldVersion=“0.0.0.0-5.0.0.0” newVersion=“5.0.0.0” />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name=“WebGrease” publicKeyToken=“31bf3856ad364e35” />
<bindingRedirect oldVersion=“0.0.0.0-1.3.0.0” newVersion=“1.3.0.0” />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name=“System. Web.Http"publicKeyToken=“31bf3856ad364e35"culture=“中性” />
<bindingRedirect oldVersion=“0.0.0.0-5.0.0.0” newVersion=“5.0.0.0” />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name=“System.Net.Http.Formatting” publicKeyToken=“31bf3856ad364e35"culture=“neutral” />
<bindingRedirect oldVersion=“0.0.0.0-5.0.0.0” newVersion=“5.0.0.0” />
</dependentAssembly>
</assemblyBinding>
</runtime>從 Views 文件夾中打開 web.config。這裡有三點需要更新:
- 在 configSections 下,將 Razor 程序集的版本更新為 3.0.0.0。
- 更新 system.web.webPages.razor 部分下的主機版本,將 System.Web.Mvc.MvcWebRazorHostFactory 的版本更新為 3.0.0.0。
- System.web 的頁面部分下提到了一些版本號。將它們全部更新到版本 5.0.0.0
如果您安裝了 Web API 幫助頁面,請在那裡檢查上述程序集配置。