简体   繁体   中英

Cannot bind from CreateMessageOptions to TwilioSms Azure Functions

The following code:

using Microsoft.Azure.WebJobs;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

namespace FunctionApp2
{
    public static class Function1
    {
        [FunctionName("Function1")]
        [return: TwilioSms(AccountSidSetting = "AccountSidSetting", AuthTokenSetting = "AuthTokenSetting", From = "From")]
        public static CreateMessageOptions Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer)
        {

            var message = new CreateMessageOptions(new PhoneNumber("XXXXX"))
            {
                Body = $"Hello thanks for your order!"
            };

            return message;
        }
    }
}

gives the error:

"Microsoft.Azure.WebJobs.Host: Error indexing method 'Funct
ion1.Run'. Microsoft.Azure.WebJobs.Host: Can't bind TwilioSms to type 'Twilio.Re
st.Api.V2010.Account.CreateMessageOptions&'.

[2018-12-02 18:03:44] Error indexing method 'Function1.Run'
[2018-12-02 18:03:44] Microsoft.Azure.WebJobs.Host: Error indexing method 'Funct
ion1.Run'. Microsoft.Azure.WebJobs.Host: Can't bind TwilioSms to type 'Twilio.Re
st.Api.V2010.Account.CreateMessageOptions&'.
[2018-12-02 18:03:44] Function 'Function1.Run' failed indexing and will be disab
led.
[2018-12-02 18:03:44] No job functions found. Try making your job classes and me
thods public. If you're using binding extensions (e.g. ServiceBus, Timers, etc.)
 make sure you've called the registration method for the extension(s) in your st
artup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.).
[2018-12-02 18:03:44] Host initialized (715ms)
[2018-12-02 18:03:44] Host started (743ms)
[2018-12-02 18:03:44] Job host started
[2018-12-02 18:03:44] The following 1 functions are in error:
[2018-12-02 18:03:44] Run: Microsoft.Azure.WebJobs.Host: Error indexing method '
Function1.Run'. Microsoft.Azure.WebJobs.Host: Can't bind TwilioSms to type 'Twil
io.Rest.Api.V2010.Account.CreateMessageOptions&'."

Here are the dependencies:

依赖img

How can I solve it? It is a problem with dependencies or just with the code?

The example comes from this docs :

https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-twilio#example---functions-2x

You have created a v1 Function project while referred to a v2 example. v1 function uses SMSMessage while v2 uses CreateMessageOptions due to the difference of Twilio SDK. So just check v1 example and modify the code.

    [FunctionName("Function1")]
    [return: TwilioSms(AccountSidSetting = "TwilioAccountSid", AuthTokenSetting = "TwilioAuthToken", From = "xxx")]
    public static SMSMessage Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer)
    {
        var message = new SMSMessage()
        {
            Body = "Hello thanks for your order!",
            To = "xxx"
        };

        return message;
    }

And project only needs two dependencies Microsoft.Azure.WebJobs.Extensions.Twilio and Microsoft.NET.Sdk.Functions .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM