私のC#ASP.NET MVC WebアプリケーションでTwilio REST APIヘルパーライブラリv 5.0.1を使用しています。私は、次のヘルパークラスを作成し、テキストメッセージを送信するために機能:私はそれがMessageResource.Create()呼び出しの後だろうと予想通りTwilio Rest API Helperライブラリ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;
}
}
}
は残念ながら、私のコードは動作していません。つまり、テキストメッセージが正しく送信され、私は電話でSMSを受信します。しかし、私は、コールが私のmsgResponse変数に制御を返すことを期待し、私は
if (msgResponse.ErrorCode == null) ...
行目以降の実行を期待するが、それは起きていません。私は、var msgResponse行にブレークポイントを置くことができますが、それはうまく動作しますが、それ以降はコード行を実行しません。あなたはtry/catchで呼び出しがあることがわかります。私は例外が発生していると思っていましたが、それは私のキャッチブロックにも行かないのでそうは思われません!テキストメッセージが正常に送信されています!私がしたいのは、私が正しくログに記録し、その情報をこの関数を呼び出すルーチンに送り返すことができるように、承認を取り戻すことだけです。
ご協力いただければ幸いです!
try/catchを削除した場合の動作は同じですか?私はあなたの基本的なコードのセットアップを取って、複製しようとしましたが、できませんでした。 VSでデバッガの出力に入るものは何ですか? –
ok..nm。私はそれを複製することができたと思う –
あなたはライブラリのバグを見つけました、私はそれのためのPRを提出しました:https://github.com/twilio/twilio-csharp/pull/333 - 新しいバージョンが公開されます。その間、CreateAsyncメソッドを使用してコントローラアクションを非同期にすることもできます。そうすれば、リクエストを行う際にプライマリスレッドをブロックしているわけではありません。 – dprothero