简体   繁体   中英

Azure Function Not Publishing Message to Service Bus

I created a Function App which is triggered via Http. The function app should publish a message to Azure Service Bus Topic. I am not getting any message published in the topic for some reasons. I have the function app successfully triggered. I am not sure what I am doing wrong here. Below is my code.

     [FunctionName("MessageProcessorFunction")]
            [return: ServiceBus("mytopic", Connection = "Endpoint=sb://abcsb.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=wBCZ4ssssssohbg1ZUYKw4q8cpKaoZLIG9NR28ZoUDhBG8=")]
            public async Task<string> Run(
               [HttpTrigger(AuthorizationLevel.Function, "post", Route = "sms")] HttpRequest req,
               ILogger log)
            {
                log.LogInformation("HTTP trigger function processed a request.");

                return "hello World";
            }

However, the console app below successfully published a message to service bus topic. Here is the code

    TopicClient _topicClient = new TopicClient("Endpoint=sb://abcsb.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=wBCZ4ssssssohbg1ZUYKw4q8cpKaoZLIG9NR28ZoUDhBG8=", "whispir");
                string data = JsonConvert.SerializeObject("Hello world");
                Message message = new Message(Encoding.UTF8.GetBytes(data));

                try
                {
                    await _topicClient.SendAsync(message);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

local.settings.json

"IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
        "ServiceBusConnectionString": "Endpoint=sb://abcsb.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=wBCZ4okoohbg1ZUYKw4q8cpKaoZLIG9NR28ZoUDhBG8="

}

Any idea?

-Alan-

The Connection property should receive a settings key, not the actual connection string.

If you want to try it locally, you'll have to change the local.settings.json file and add the connection string to a specific key:

{
  "Values": {
    "ServiceBusConnectionString": "Endpoint=sb://abcsb.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=wBCZ4ssssssohbg1ZUYKw4q8cpKaoZLIG9NR28ZoUDhBG8="
  }
}

Then, use it in your function like this:

[FunctionName("MessageProcessorFunction")]
[return: ServiceBus("mytopic", Connection = "ServiceBusConnectionString")]
public async Task<string> Run(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = "sms")] HttpRequest req,
    ILogger log)
{
    log.LogInformation("HTTP trigger function processed a request.");

    return "hello World";
}

Should be working.
Let me know if not. :)

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