GlobalConfiguration - 尝试访问字段'<> 9__CachedAnonymousMethodDelegate2'失败

时间:2014-04-17 21:03:34

标签: asp.net-web-api

设置依赖性解析程序时出现此错误。

代码:

  

GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);

错误:

  

尝试通过方法'System.Web.Http.GlobalConfiguration..cctor()'访问字段'System.Web.Http.GlobalConfiguration.CS $<> 9__CachedAnonymousMethodDelegate2'失败。

我在这里缺少什么?

所有发布的解决方案都涉及安装最新版本的Microsoft.AspNet.WebApi。

  

安装包Microsoft.AspNet.WebApi -IncludePrerelease

但是,我已经拥有了所有必需程序集的最新版本,而且我仍然收到此错误。

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31BF3856AD364E35" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
        </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
  </dependentAssembly>
  <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.WebPages" 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="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
  </dependentAssembly>
</assemblyBinding>

3 个答案:

答案 0 :(得分:6)

已安装软件包,但显然有些引用是从GAC而不是NuGet软件包文件夹中提取的。

这些是手动删除的,我在packages文件夹中添加了引用,但它确实有效。

(应该有一个NuGet功能来“清理”引用,以便它们从packages文件夹而不是其他地方获取。)

答案 1 :(得分:4)

我通过安装Microsoft ASP.NET Web API 2.2解决了这个问题

答案 2 :(得分:0)

解决方案链接:https://www.niceonecode.com/Question/20222/The-type-initializer-for-System.Web.Http.-GlobalConfiguration-threw-an-exceptio

我有同样的问题,可以通过解决办法解决。

PM>安装软件包Microsoft.AspNet.WebApi -Pre 它将包含一个预发行版本。