简体   繁体   English

来自控制器的HttpClient PostAsync没有结果

[英]HttpClient PostAsync from controller without result

I'd like to find out what exactly happens in the scenario where i don't need the result of PostAsync call in a scenario when it's called from controller. 我想找出在从控制器调用方案的情况下不需要PostAsync调用的结果的情况下到底发生了什么。
For example, i have some action 例如,我有一些动作

[HttpPost]
public ActionResult DoSomething(SomeModel model)
{
    ...
    PostDataFireAndForget(model);
    ...
    return new EmptyResult();
}

private void PostDataFireAndForget(SomeModel model)
{
    try
    {
        using (var client = new HttpClient())
        {
            string targetUrl="SomeUrl";
            client.PostAsync(targetUrl, new FormUrlEncodedContent(model.Content));
        }
    }

    catch (Exception ex)
    {
        //we didn't care about the response, no additional requirements here
        //ignore
    }
}

So i'd like to know how exactly it will proceed and why in all possible scenarios. 所以我想知道它将如何进行,以及为什么在所有可能的情况下。 The most interesting one is the scenario where PostAsync takes longer than all the other action code after it so the action completes before PostAsync is finished. 最有趣的情况是PostAsync花费比所有其他动作代码更长的时间,因此动作在PostAsync完成之前完成。 Will it get terminated/blocked/finished? 它会被终止/阻止/完成吗? Is there any better approach if i want to perform some kind of async stuff in action and don't want to wait for the result? 如果我想执行某种异步操作并且不想等待结果,有什么更好的方法?

Make the fire and forget method async all the way so that the exception can be caught and ignored. 使fire and忘记方法一直保持异步,以便可以捕获和忽略异常。 In its current design it can cause the main action request's thread to fall over. 在其当前设计中,它可能导致主操作请求的线程掉线。

private async Task PostDataFireAndForgetAsync(SomeModel model) 
    try {
        using (var client = new HttpClient()) {
            var targetUrl="SomeUrl";
            await client.PostAsync(targetUrl, new FormUrlEncodedContent(model.Content));
        }
    } catch (Exception ex) {
        //we didn't care about the response, no additional requirements here
        //ignore
    }
}

By making it async the method can handle (or not handle in this case) the exception and let the other threads continue. 通过使其异步,该方法可以处理(或在这种情况下不能处理)异常,并让其他线程继续。

[HttpPost]
public ActionResult DoSomething(SomeModel model) {
    //... fire and forget
    PostDataFireAndForgetAsync(model);
    //...
    return new EmptyResult();
}

A function/method is always called to process an input and present an output, irrelevant if you need the results or not. 始终调用函数/方法来处理输入并显示输出,无论是否需要结果都无关紧要。 If you do not want to wait for the result you will have to at least wait for the process to finish, even if you wish to ignore the result. 如果您不想等待结果,则即使您希望忽略结果,也必须至少等待过程完成。

In your scenario, you are making a request and leaving without any outcome, though all UI process might complete and exit but the application will stay in memory until the async completes. 在您的方案中,您正在发出请求,并且没有任何结果,尽管所有UI进程可能已完成并退出,但应用程序将保留在内存中,直到异步完成。 Preferably wait for the async to finish before you completely end all routines. 在完全结束所有例程之前,最好等待异步完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM