Console App在调用Debug中的异步方法时突然退出

huangapple go评论97阅读模式
英文:

Console App exits abruptly when calling async methods in Debug

问题

以下是翻译好的部分:

在控制台和Windows服务应用中发生,未在Windows Forms或WPF上测试。在代码中逐步执行时,每当它遇到带有await的async调用时,调试会突然退出,无法继续。作为一种解决方法,我正在使用Task.Run(),但在调试期间将所有async调用更改为使用Task.Run()语法,然后在部署时将其改回await语法是一种麻烦。

在下面的这个方法中,在调试期间,以下行是问题,在部署时它可以正常工作。

var response = await httpClient.SendAsync(request);

在调试时的解决方法是将上述行更改为:

//var response = await httpClient.SendAsync(request);
var task = Task.Run(() => httpClient.SendAsync(request));
task.Wait();
var response = task.Result;

代码:

private static async Task<string> SendRequest()
{
    string result = "";
    string url = "http://localhost:5119/WeatherForecast";
    var URL = new Uri(url);
    var method = new HttpMethod("GET");
    using (var request = new HttpRequestMessage(method, URL))
    {
        var response = await httpClient.SendAsync(request);

        //var task = Task.Run(() => httpClient.SendAsync(request));
        //task.Wait();
        //var response = task.Result;
        result = await response.Content.ReadAsStringAsync();
    }

    return result;
}
英文:

It happens on Console and Windows Service apps, haven't tested on Windows Forms or WPF. When stepping thru the code whenever it hits an async call with await, the debug session exits abruptly, can't go further. As a workaround I'm using Task.Run() but changing all the async calls to use Task.Run() syntax during debug, and put it back to await syntax for deployment is a hassle.

Here in this method below, the following line is the issue during Debug, it works as expected when deployed.

var response = await httpClient.SendAsync(request);

Work around while debugging is to change the above line to this:

//var response = await httpClient.SendAsync(request);
var task = Task.Run(() =&gt; httpClient.SendAsync(request));
task.Wait();
var response = task.Result;

Code:

private static async Task&lt;string&gt; SendRequest()
{
    string result = &quot;&quot;;
    string url = &quot;http://localhost:5119/WeatherForecast&quot;;
    var URL = new Uri(url);
    var method = new HttpMethod(&quot;GET&quot;);
    using (var request = new HttpRequestMessage(method, URL))
    {
        var response = await httpClient.SendAsync(request);

        //var task = Task.Run(() =&gt; httpClient.SendAsync(request));
        //task.Wait();
        //var response = task.Result;
        result = await response.Content.ReadAsStringAsync();
    }

    return result;
}

答案1

得分: 2

以下是您要翻译的内容:

我在.NET 7控制台应用程序中遇到了类似的问题。

我已经将我的App.Run方法更改为异步,但我没有等待它。 我通过在Program.cs中的这一行上添加“await”来解决了我的问题:

await services.GetRequiredService&lt;App&gt;().Run(args);

如果我当时留意警告,我本可以更早地发现这个问题。 对于任何与类似问题抗争的人,只需检查警告并确保在整个调用堆栈中都使用了异步/等待。

英文:

I ran into a similar issue in a .NET 7 Console application.

I had already changed my App.Run method to be async, but I was not awaiting it. My issue was resolved by adding the "await" on this line in Program.cs:

await services.GetRequiredService&lt;App&gt;().Run(args);

I would have caught this earlier if I was paying attention to my warnings. For anyone wrestling with a similar issue just check the warnings and make sure your using async/await all the way up and down the call stack.

huangapple
  • 本文由 发表于 2023年2月16日 02:13:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75463895.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定