WCF 服务无法仅在发布版本中找到 System.Net.Http

WCF Service is unable to locate System.Net.Http in release build only

我正在处理在最近从 .Net Framework 4.5.2 更新到 4.7 的 WinForms 应用程序中遇到的错误。该问题仅在部署应用程序 之后出现。该错误源自我正在使用的 WCF 服务 (.svc) 内部。我得到的具体错误文本是:

Could not load file or assembly 'System.Net.Http, Version=4.1.1.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

为了解决这个问题,我将 App.Config 文件调整为 bindingRedirect 从 System.Net.Http version 4.1.1.14.3.2(实际上并不存在)。

解决了 服务问题;但是,当我调用 System.Net.Http 下载字符串时,调用失败了。据推测,这是因为我指示 System.Net.Http 使用实际上不存在的依赖项 (4.3.2)。

这意味着目前我有一个选择:要么服务工作,要么直接调用 System.Net.Http 工作,但不能同时工作。有人知道如何使服务能够找到 System.Net.Http 4.1.1.1 吗?它是通过 NuGet 安装的...只是似乎无法识别,除非: 1) 我发出绑定重定向到实际上并不存在的 System.Net.Http 版本。 要么 2) 我 运行 Visual Studio debug/release 模式下的程序(已部署的版本不起作用)。

经过几天的麻烦,我找到了问题所在。 System.Net.Http 没有按预期发布。这是一个已知问题(理论上已解决),可在此处找到:

https://blogs.msdn.microsoft.com/bclteam/p/httpclient/

Issue 8

Symptom

ClickOnce applications targeting .NET Framework 4.0 that reference the Microsoft.Net.Http package may experience a TypeLoadException or other errors after being installed.

Resolution

This occurs because ClickOnce fails to deploy certain required assemblies. As a workaround, do the following:
1.Right-click on the project and choose Add Existing Item
2.Browse to the HttpClient net40 package folder
3.In the File name text box enter *.*
4.Holding CTRL, select System.Net.Http.dll and System.Net.Http.Primitives.dll
5.Click the down-arrow next to the Add button and choose Add as Link
6.In Solution Explorer, holding CTRL select System.Net.Http.dll and System.Net.Http.WebRequest.dll
7.Right-click the selection, choose Properties and change Copy to Output Directory to Copy always
8.Republish