【问题标题】:Twilio Rest API Helper Library, v 5.0.1, C# - MessageResource.Create function call not returning properlyTwilio Rest API 帮助程序库,v 5.0.1,C# - MessageResource.Create 函数调用未正确返回
【发布时间】:2017-02-24 16:30:07
【问题描述】:

我在我的 C# ASP.NET MVC Web 应用程序中使用 Twilio REST API 帮助程序库 v 5.0.1。我创建了以下帮助类和函数来发送短信:

    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) ...

line 和后续行运行,但没有发生。我可以在 var msgResponse 行上放一个断点,它会运行得很好,但之后它不会运行任何代码行。你会看到我在 try/catch 中有调用。我怀疑发生了异常,但似乎不是这样,因为它也没有进入我的 catch 块!短信发送成功!我要做的就是得到一个确认,以便我可以正确记录它并将该信息发送回调用此函数的例程。

任何帮助将不胜感激!

【问题讨论】:

  • 如果删除 try/catch,行为是否相同?我采用了您的基本代码设置并尝试复制但无法复制。 VS 中的调试器输出有什么东西吗?
  • 好的..nm。认为我能够复制它
  • 您确实在库中发现了一个错误,我为此提交了 PR:github.com/twilio/twilio-csharp/pull/333 -- 我们会在新版本发布时通知您。同时,您可以考虑使用 CreateAsync 方法并使您的控制器操作也异步。这样你在发出请求时就不会阻塞主线程。
  • @tom-vaidyan 5.0.2 版包含对此的修复,现在在 Nuget 中。感谢您帮助查找问题。我们可以寄给您一件 Twilio T 恤作为感谢吗?给 twilio dot com 上的 devin 发送一封电子邮件,让你的方向前进。
  • 我很高兴能够揭露这一点!如果我遇到任何问题,我会检查新版本并报告!

标签: c# asp.net asp.net-mvc rest twilio


【解决方案1】:

版本 5.0.2 为我修复了这个问题,只需将 twilio 更新到 5.0.2。他们刚刚添加了 .ConfigureAwait(false);使用 CreateAsync

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    相关资源
    最近更新 更多