2017-02-24 34 views
0

我使用Twilio REST API助手庫,V在我的C#ASP.NET MVC Web應用程序5.0.1。我創建了下面的輔助類和函數來發送短信:Twilio的REST API助手庫,V 5.0.1,C# - MessageResource.Create函數調用不返回正確

using MyApplication.Web.Helpers; 
    using System; 
    using System.Configuration; 
    using Twilio; 
    using Twilio.Exceptions; 
    using Twilio.Rest.Api.V2010.Account; 
    using Twilio.Types; 

    namespace MyApplication.Web.Services 
    { 
     public class TwilioSmsSender : ISmsSender 
     { 
      public string AccountSid { get; set; } 
      public string AuthToken { get; set; } 
      public string FromPhoneNumber { get; set; } 
      private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 
      public string SmsPrefix { get; set; } 
      public string SmsSuffix { get; set; } 

      public TwilioSmsSender() 
      { 
       //get our Twilio account info from the config file 
       AccountSid = ConfigurationManager.AppSettings["TwilioAccountSid"]; 
       AuthToken = ConfigurationManager.AppSettings["TwilioAuthToken"]; 
       FromPhoneNumber = ConfigurationManager.AppSettings["SmsService.FromPhoneNumber"]; 
       SmsPrefix = ConfigurationManager.AppSettings["SmsPrefix"]; 
       SmsSuffix = ConfigurationManager.AppSettings["SmsSuffix"]; 

       if (FromPhoneNumber.Length == 10) 
       { 
        FromPhoneNumber = $"+1{FromPhoneNumber}"; 
       } 

       TwilioClient.Init(AccountSid, AuthToken); 
      } 

      public INotificationResponse SendTextMessage(string phoneNumber, string message, bool useFormatting = true) 
      { 
       var resp = new TwilioSmsSenderResponse(); 
       resp.Succeeded = false; 
       resp.AttemptDateTimeUtc = DateTime.UtcNow; 

       if (useFormatting) 
       { 
        message = $"{SmsPrefix}{message}{SmsSuffix}"; 
       } 

       try 
       { 
        var msgResponse = MessageResource.Create(
         to: new PhoneNumber($"+1{phoneNumber}"), 
         from: new PhoneNumber($"{FromPhoneNumber}"), 
         body: message); 

        //Previous line works (i.e, I get the text message that I'm sending out successfully). 
        //However, none of the following lines are running... 
        //As you see, this is in a try/catch block... and it doesn't go to the catch block either! 

        if (msgResponse.ErrorCode == null) 
        { 
         //successfully queued 
         resp.Succeeded = true; 
         resp.ReferenceId = msgResponse.Sid; 
         resp.AttemptDateTimeUtc = DateTime.UtcNow; 
        } 
        else 
        { 
         //Twilio sent an error back 
         log.Info($"Twilio sent an error back: {msgResponse}"); 
         resp.Succeeded = false; 
         resp.Notes = $"ErrorCode: {msgResponse.ErrorCode}, ErrorMessage: {msgResponse.ErrorMessage}"; 
         resp.AttemptDateTimeUtc = DateTime.UtcNow; 
        } 
       } 
       catch (Exception e) 
       { 
        resp.Succeeded = false; 
        resp.Notes = ExceptionsHelper.GetExceptionDetailsAsString(e); 
        resp.AttemptDateTimeUtc = DateTime.UtcNow; 

        log.Error($"Twilio Error: {resp.Notes}, to: {phoneNumber}, message: {message}"); 
       } 

       return resp; 
      } 
     } 
    } 

不幸的是,我的代碼是不是表現爲我估計它會在MessageResource.Create()調用之後。也就是說,短信正確發送出去,我收到手機上的短信。不過,我希望呼叫控制回到我msgResponse變量,我想到

if (msgResponse.ErrorCode == null) ... 

線和後續行運行但沒有發生。我可以在var msgResponse行上放置一個斷點,它會運行得很好,但之後它不會運行任何代碼行。你會看到我在try/catch中打電話。我懷疑是發生了一個異常,但似乎並不是這樣,因爲它也沒有進入我的catch塊。短信正在發送成功!我想要做的就是獲取確認信息,以便我可以正確記錄它並將該信息發送回調用此函數的例程。

任何幫助將不勝感激!

+0

是行爲,如果你刪除的try/catch一樣嗎?我把你的基本代碼設置,並試圖複製,但無法。任何進入調試器輸出的VS? –

+0

ok..nm。認爲我能夠複製它 –

+0

你確實在庫中發現了一個錯誤,我提交了一個PR:https://github.com/twilio/twilio-csharp/pull/333 - 我們會告訴你什麼時候新版本發佈。同時,您可以使用CreateAsync方法並使控制器的操作異步。這樣你在提出請求時就不會阻塞主線程。 – dprothero

回答

0

版本5.0.2固定這對我來說只是twilio到5.0.2更新。他們只是添加了.ConfigureAwait(false);與CreateAsync