【问题标题】:Upgrading to Web API 2 breaks application升级到 Web API 2 会破坏应用程序
【发布时间】:2014-02-18 21:06:09
【问题描述】:

我有一个 Umbraco 7 应用程序,它使用 V4.0 程序集来处理诸如 System.Web.WebHost、System.Web.Http 和相关程序集之类的东西。我正在尝试让 Web API2 dll 与 Umbraco 应用程序一起使用,但我遇到了困难。

如果我使用程序集重定向默认为 V5.0 程序集,则应用程序会在 Umbraco 加载并尝试使用控制器时中断。这里的问题是,在 System.Web.Http 的 V5.0 中,ApiController 现在在设置 Request 时有一个检查。此检查总是失败并抛出 InvalidOperationException:

set
{
    if (value == null)
    {
        throw Error.PropertyNull();
    }
    HttpRequestContext requestContext = value.GetRequestContext();
    HttpRequestContext httpRequestContext = this.RequestContext;
    if (requestContext != null && requestContext != httpRequestContext)
    {
        throw new InvalidOperationException(SRResources.RequestContextConflict);
    }
    this.ControllerContext.Request = value;
    value.SetRequestContext(httpRequestContext);
    RequestBackedHttpRequestContext requestBackedHttpRequestContext = httpRequestContext as RequestBackedHttpRequestContext;
    if (requestBackedHttpRequestContext != null)
    {
        requestBackedHttpRequestContext.Request = value;
    }
}

我正在尝试的另一件事是正确加载和使用两个 DLL。在这种情况下,我遇到了一个错误,但不知道我是否做得正确。我所做的只是添加文件夹以保留相同 dll 的不同版本,并使用以下方式将它们连接到 web.config 中:

<probing privatePath="webapi2_dlls;" />

错误: CS1705:程序集 'WebApi, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 使用 'System.Web.Http, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35',它的版本高于引用的程序集'System.Web.Http,版本=4.0.0.0,文化=中性,PublicKeyToken=31bf3856ad364e35'

还有希望吗?

【问题讨论】:

    标签: c# .net asp.net-web-api2 umbraco7


    【解决方案1】:

    看起来 Umbraco 是使用旧版本 (4.0) 的 Web API 构建的。您可以在将以下绑定重定向添加到配置后重试:

    <system.webServer>
        <runtime>
          <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
              <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" />
              <bindingRedirect oldVersion="1.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
            </dependentAssembly>
            <dependentAssembly>
              <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" />
              <bindingRedirect oldVersion="1.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
            </dependentAssembly>
    

    【讨论】:

    • 当我使用 V5.0 时,Umbraco 有时会即时构建一个控制器,并在其上设置 Request 成员。在 V5.0 中,有一些检查如果不满足会抛出异常。查看我的问题
    • 我想我错过了那部分......你也在使用5.15.0的Web API吗?
    • 我实际上使用的是 5.1,但看起来我遇到的问题是在 5.0 中引入的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 2012-04-01
    • 1970-01-01
    相关资源
    最近更新 更多