我目前正在编写 Azure Functions 代码以通过 Twilio 发送短信。
using System;
using System.Threading.Tasks;
using Twilio;
using Twilio.Types;
using Twilio.Rest.Api.V2010.Account;
public static void Run(string myQueueItem, ILogger log)
{
log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
string accountSid = "My_SID";
string authToken = "MY_TOKEN";
TwilioClient.Init(accountSid, authToken);
var _to = new PhoneNumber("+821073884889");
var _from = new PhoneNumber("+15672805833");
var message = MessageResource.Create(to: _to, from: _from, body: "Hello world");
}
当我运行此代码时,我收到以下错误消息
2018-11-09T14:06:47 Welcome, you are now connected to log-streaming service.
2018-11-09T14:07:28.742 [Information] Executing 'Functions.sendSMSFunction2' (Reason='This function was programmatically called via the host APIs.', Id=c21afe21-8af0-4dde-a730-236377f790b9)
2018-11-09T14:07:28.772 [Error] Executed 'Functions.sendSMSFunction2' (Failed, Id=c21afe21-8af0-4dde-a730-236377f790b9)
Method not found: 'Twilio.Rest.Api.V2010.Account.MessageResource Twilio.Rest.Api.V2010.Account.MessageResource.Create(Twilio.Types.PhoneNumber, System.String, Twilio.Types.PhoneNumber, System.String, System.String, System.Collections.Generic.List`1<System.Uri>, System.Uri, System.String, System.Nullable`1<System.Decimal>, System.Nullable`1<Boolean>, System.Nullable`1<Int32>, System.String, System.Nullable`1<Boolean>, System.String, ContentRetentionEnum, AddressRetentionEnum, System.Nullable`1<Boolean>, Twilio.Clients.ITwilioRestClient)'.
2018-11-09T14:08:47 No new trace in the past 1 min(s).
2018-11-09T14:09:48 No new trace in the past 2 min(s).
在粘贴到此处之前我删除了 SID 和 TOKEN :)
所以问题看起来像 MessageResource.Create 部分(如上面提到的第 4 行)
当我在 VS2017 上运行上面的代码时,它可以很好地发送短信,但在 Azure Functions 上,却卡住了:(
函数.json
{
"bindings": [
{
"name": "myQueueItem",
"type": "serviceBusTrigger",
"direction": "in",
"queueName": "myqueue",
"connection": "servicebus8_RootManageSharedAccessKey_SERVICEBUS"
}
]
}
函数.proj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Twilio" Version="5.21.0"/>
</ItemGroup>
</Project>
请问有什么帮助吗?
最佳答案
我发现一些代码无法在 Azure Function v2 上运行。
然后我将版本更改为 1,它起作用了:)
如果像我这样的人只是从 Visual Studio 中复制并粘贴代码行,那么请记住“找不到方法”意味着降级版本。
我希望这能防止其他人浪费宝贵的时间!!
关于azure - Azure Functions 上的 Twilio(2.0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53227436/