使用Task.Run异步调用方法

时间:2014-01-28 17:35:03

标签: c# asp.net-mvc-4 asynchronous asp.net-web-api .net-4.5

有人可以帮助我如何调用方法SendSms(textMessageItems)异步?什么是最好的方法/做法?我假设Task.Run Async-Await可以在这里使用,因为我使用.Net4.5使用MVC4 WebApi。但我想听听专家的意见,因为我是新手。我在我的Web服务器中使用此代码,该服务器位于IIS7中,此方法可能需要一段时间才能处理,因此需要异步处理它,以便响应可以立即返回给调用者。 此外,因为我在for循环中调用SendSms会导致任何问题吗?你认为我应该将它作为集合列表传递然后处理吗?请指教。

 using Twilio.Mvc;
 using Twilio.TwiML.Mvc;
 using Twilio.TwiML;


public class SmsController : ApiController
{
    [HttpPost]
    public HttpResponseMessage Post([FromBody]SmsRequest smsReq)
    {
            var response = new Twilio.TwiML.TwilioResponse();
            //validation checks..

            try
            {


                if ((txtMessageResponse != null) && (txtMessageResponse.SmsMessageInfo.Count > 0))
                {
                    _smsStagingList = txtMessageResponse.SmsMessageInfo;
                    foreach (TextMessageStaging prepareTextMessageResponse in _smsStagingList)
                    {
                        smsDTO textMessageItems = new smsDTO();
                        textMessageItems.PhoneNumber = prepareTextMessageResponse.PhoneNumber;
                        textMessageItems.SmsMessage = prepareTextMessageResponse.SmsMessageBody;

                        isTxtMessageSent = SendSms(textMessageItems);

                        //If the messages were sent then no need to set the flag to be updated 
                        if (isTxtMessageSent)
                        {
                            txtMessageStatusToBeUpdated = false;
                        }
                    }
                    return Request.CreateResponse(HttpStatusCode.OK, twilioResponse.Element);
                }
                else
                {
                    //send error response
                }
            catch (Exception msgProcessingError)
            {
              //send error response again as processing error
            }
            finally
            {
             //set the outbound flag in the table
            }
     }


    private bool SendSms(smsDTO textMessageItems)
    {
        bool isTxtMessageSent = false;
        PushMessageRequest txtMessageRequest = new PushMessageRequest();
        PushMessageResponse txtMessageResponse = null;
        txtMessageRequest.SmsMessageInfo = new SendTextMessage(); //instantiate the dto

        txtMessageRequest.SmsMessageInfo.ToPhone = textMessageItems.PhoneNumber;
        txtMessageRequest.SmsMessageInfo.TextMessage = textMessageItems.SmsMessage;
        try
        {
            using (ITextService textService = ObjectFactory.SendSmsMessage())
            {
                txtMessageResponse = textService.SendSmsMessage(txtMessageRequest);
            }

            isTxtMessageSent = txtMessageResponse.IsSuccessful;
        }
        catch (Exception ex)
        {
            isTxtMessageSent = false;
        }      
        return isTxtMessageSent;
    }          

1 个答案:

答案 0 :(得分:1)

我建议您不要使用Task.Run。 AFAIK,Twilio没有async API,所以你应该问他们。你当然可以自己编写,例如,基于HttpClient

  

希望异步处理它,以便响应可以立即返回给调用者

请注意async does not change the HTTP protocol,如我的博客所述。它是possible, but extremely dangerous,在请求尚未完成处理时返回ASP.NET的响应(也是我博客的链接)。