2014-01-22 28 views
0

我是WebApi,.Net世界的新手,对于我应该采取什么方法提供的所有信息完全混淆。我使用MVC4 WebApi创建了一个WebService,Twilio在收到文本消息时调用它。我需要回复这条短信。我正在使用当前正在同步调用的WCF方法。由于我的进程可能需要超过3-5秒才能处理对文本消息的回复,因此由于超时而与Twilio的连接断开。所以我正在寻找方法来异步调用这个WCF方法。 我的问题是调用WCF方法(我使用Object Factory调用WCF并使用) 我需要更新合同以表示异步?我对此很困惑。如何从MVC4异步调用WCF方法WebAPI

顺便说一句我的Web服务是在IIS7中,并使用.Net4.5框架和MVC4 WebApi。

我的代码有点像这样:所以我想异步调用SendSms部分。我怎么做?我可以简单地使用Task.Run异步和等待?

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 
      { 

       -- call to WCF to get the List of sms to be sent 
       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; 
    }   
+0

几个澄清问题。您是否使用ASP.NET Web API或WCF创建您的服务?另外,你有没有一种特定的方法可以在你的服务中调用,你知道它可能会长时间运行? –

+0

@DevinRader - 我正在使用ASP.NET Web API创建服务。但在我的HTTP后,我已经实现了一个驻留在WCF中的方法。 – Ditty

回答

0

Twilio evangelist here。

好的,所以你有一个Web API方法,你可以在其中调用一个可能长时间运行的WCF方法。有两个问题要解决这里:

  1. 如何调用WCF方法在不从返回响应
  2. 你怎么Twilio等到WCF方法阻止网页API方法的方法已完成

我写了blog post前一阵子,告诉您如何通过利用.NET的Task Parallel library和Twilios <Play>动词loop属性来创建在IVR无限期等待循环。

该帖子的要点是,您可以使用TPL的StartNew方法在新线程上启动长时间运行的WCF方法。这可以让ASP.NET继续并让您返回一些TwiML,因此Twilio不会结束呼叫。然后将它与continuation配对,它可让您知道WCF服务请求何时完成,并且您可以使用REST API将redirect the in-progress call发送给Twilio以发送一组新的TwiML指令。

希望有所帮助。

+0

不错的博客文章。但是博客显示了当呼叫者等待呼叫者回复时该如何处理。在我的情况下,这是一个短信。那么,我可以返回什么TwiML消息?另外请注意,我实际上不打算通过TwiML返回任何内容,但是我们将使用Twilio Rest API将消息作为回复发送出去,即传出消息。这是因为对于传入的文本,可能有超过10个短消息作为回复发送,这是TwiML API中的限制。 – Ditty

+0

基本上我觉得我需要一些可以开火和忘记的东西,除非我们有另一种告诉twilio等待得到答复的方式。我不知道我们可以使用其他什么动词。因为我相信我们不能和Sms Verb一起使用重定向。 – Ditty

+0

啊,好的。那么在这种情况下,你不可能通过纯粹的TwiML响应来实现。但是,您可以通过稍微更改帖子中的代码来完成此操作。首先,因为这是一个SMS,我们正在谈论你不需要从ASP.NET方法返回任何东西。其次,在Continuation中,不需要通过API修改调用,而需要使用REST API发送回复短信。希望有所帮助。 –