从MVC控制器调用异步方法

时间:2021-12-21 20:34:40

I'm trying to send an SMS asynchronously from an MVC5 controller.

我正在尝试从MVC5控制器异步发送SMS。

In the controller I call the following method, which is in another class. I don't need any response from the method and don't care if it fails.

在控制器中,我调用以下方法,该方法在另一个类中。我不需要该方法的任何响应,也不关心它是否失败。

    public async void SendSMS(string phoneNumber, string message)
    {
        await Task.Run(() =>
            {
                TwilioRestClient twilio = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
                twilio.SendSmsMessage(TWILIO_PHONE_NUMBER, phoneNumber, message);
            }
        );
    }

This isn't working for me, as it seems to be running synchronously. Any tips on how to improve the code and get it into working order are appreciated.

这对我来说不起作用,因为它似乎同步运行。任何有关如何改进代码并使其进入正常工作状态的提示都值得赞赏。

1 个解决方案

#1


6  

When you await a Task (or any other awaitable), you're telling the compiler "Im executing this task and yielding control back to you". What happends is that control is returned to the caller until the Task finishes execution, and the compiler generates a state machine that will return to the await once it completes.

当你等待一个任务(或任何其他等待的)时,你告诉编译器“我正在执行这个任务并让你回到你身边”。发生的事情是控件返回给调用者,直到Task完成执行,并且编译器生成一个状态机,一旦完成就会返回到await。

In your method, you dont need any of that.

在你的方法中,你不需要任何这些。

I will state that im not a fan of fire and forget methods and I think you should add a continuation to handle the Tasks failure even if you dont care if it succeeds. If you're using .NET 4.0 not handling a Task exception will cause your process to terminate when the finializer disposes the Task.

我将声明我不是火的粉丝而忘记了方法,我认为你应该添加一个延续来处理任务失败,即使你不在乎它是否成功。如果您使用.NET 4.0而不处理任务异常将导致您的进程在终结器处理任务时终止。

You can remove the async from your method signature since you dont need to await and return a Task instead:

您可以从方法签名中删除异步,因为您不需要等待并返回任务:

public Task SendSMS(string phoneNumber, string message)
{
    return Task.Run(() =>
    {
        TwilioRestClient twilio = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
        twilio.SendSmsMessage(TWILIO_PHONE_NUMBER, phoneNumber, message);
    });
}

#1


6  

When you await a Task (or any other awaitable), you're telling the compiler "Im executing this task and yielding control back to you". What happends is that control is returned to the caller until the Task finishes execution, and the compiler generates a state machine that will return to the await once it completes.

当你等待一个任务(或任何其他等待的)时,你告诉编译器“我正在执行这个任务并让你回到你身边”。发生的事情是控件返回给调用者,直到Task完成执行,并且编译器生成一个状态机,一旦完成就会返回到await。

In your method, you dont need any of that.

在你的方法中,你不需要任何这些。

I will state that im not a fan of fire and forget methods and I think you should add a continuation to handle the Tasks failure even if you dont care if it succeeds. If you're using .NET 4.0 not handling a Task exception will cause your process to terminate when the finializer disposes the Task.

我将声明我不是火的粉丝而忘记了方法,我认为你应该添加一个延续来处理任务失败,即使你不在乎它是否成功。如果您使用.NET 4.0而不处理任务异常将导致您的进程在终结器处理任务时终止。

You can remove the async from your method signature since you dont need to await and return a Task instead:

您可以从方法签名中删除异步,因为您不需要等待并返回任务:

public Task SendSMS(string phoneNumber, string message)
{
    return Task.Run(() =>
    {
        TwilioRestClient twilio = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
        twilio.SendSmsMessage(TWILIO_PHONE_NUMBER, phoneNumber, message);
    });
}