前景:我正在调查 ASP.NET Core 项目(针对 .NET 4.6.1,使用 .Net Standard 1.4 中的程序集)中的一个错误,该错误引发了 MissingMethodException,尽管这绝不可能。隔离故障方法,我将代码的核心取出来进行单独的单元测试,该单元测试也针对 .NET 4.6.1。
我从单元测试中收到的异常 ThreadAbortException
或多或少是这样的(有时会有所不同):
at System.Net.Http.WinHttpHandler.SetRequestHandleDecompressionOptions(SafeWinHttpHandle requestHandle)
at System.Net.Http.WinHttpHandler.SetRequestHandleOptions(WinHttpRequestState state)
at System.Net.Http.WinHttpHandler.<StartRequest>d__103.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Hub.Test.UnitTest1.<FetchUrlAsync>d__2.MoveNext()
上述异常的相关代码:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
//FetchUrl(); // works
var task = FetchUrlAsync(); // does not work
}
private void FetchUrl()
{
var handler = new HttpClientHandler();
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (var client = new HttpClient(handler, true))
{
var response = client.SendAsync(new HttpRequestMessage(HttpMethod.Get, new Uri("https://github.com/dotnet/core"))).Result;
Trace.WriteLine("FetchUrl:" + response.StatusCode);
}
}
private async Task FetchUrlAsync()
{
try
{
var handler = new HttpClientHandler();
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (var client = new HttpClient(handler, true))
{
var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, new Uri("https://github.com/dotnet/core")));
Trace.WriteLine("FetchUrlAsync: " + response.StatusCode);
}
}
catch (Exception e)
{
Trace.WriteLine(e);
throw;
}
}
}
请注意,如果我调用 FetchUrl(),代码就会工作。是的,我知道这在我调用结果的那一刻就变成了同步方法,但它仅用于演示目的。
但是,错误方法 FetchUrlAsync() 不断抛出 ThreadAbortException。
SendAsync 的原因是,原始实现是一个通用实现,对所有 Http* 工作都使用这种方法。使用 GetAsync 等可以触发相同的错误。
我不知道我在这里做错了什么,但我怀疑这个错误是 .NET CLR 中某处的错误。
更新
虽然单元测试问题已解决(谢谢),但我在 ASP.NET Core web-api 中仍然遇到问题。据我了解,.NET Standard 1.4 和 .NET Framework 4.6.1 应该是 1-1 兼容的。这就是为什么我认为框架中存在一个罕见的错误,尽管这似乎不太可能。
共同点是System.Net.Http;这与 .NET Standard 1.4 和 .NET Framework 4.6.1 按位不同;我倾向于找出问题所在,然后将答案发布给可能遇到类似问题的其他人。
最佳答案
好的,解决最初的问题,即由其中一个 Controller 报告的 MissingMethodException
。
此错误仅在使用 async/await 组合时触发,这是有道理的,因为编译器会在后台生成代码。
事实证明,其中一个 Nuget 程序集正在使用 .NET Standard Library 1.6.1
中的 System.Net.Http
并且此程序集包针对 .NET Standard 1.4 (从理论上讲,它与 .NET Framework 4.6.1
1-1 兼容)。
然后我与解决方案混在一起,并且能够生成 FileNotFoundException: Could not load file or assembly 'System.Net.Http, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 或一个它的依赖项。
到目前为止一切顺利。
当查看 bin/debug 文件夹时,问题变得很明显。找不到 System.Net.Http.dll,因为它使用的是内置框架程序集。
啊哈。
Visual Studio 2015
查看解决方案,仅在project.lock.json中引用了内置Framework程序集:
"frameworkAssemblies": [
"System.Net.Http"
]
此引用来自 Microsoft.AspNetCore.Authentication/1.1.0
和 Microsoft.IdentityModel.Protocols/2.1.0
。
删除这两个 frameworkAssemblies
条目使解决方案起作用,因为“真正的”System.Net.Http.dll 现在已复制到 bin/debug 文件夹。
Visual Studio 2017
几乎相同的问题;但由于微软已经弃用了 project.json,这里的解决方案是修改(或添加)一个 app.config 文件,内容如下:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
对于这篇长文章,我很抱歉;但是有一个错误 - 它可能不在框架中.. 证据可能指向 msbuild 和/或不同的 IDE。我很高兴我发现了错误并能够更正它。
我希望这篇相当长的文章可以帮助其他有类似问题的人。
编码愉快!
关于c# - 在 .NET Core(.NET Standard 1.4 和 .NET Framework 4.6.1)中对 System.Net.Http 使用 await/async 时出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41700869/