Azure功能未将消息发布到服务总线

时间:2019-08-27 08:12:53

标签: c# azure .net-core azure-functions azureservicebus

我创建了一个通过Http触发的功能应用程序。功能应用程序应将消息发布到Azure Service Bus主题。由于某些原因,我没有在该主题中发布任何消息。我已成功触发功能应用。我不确定我在做什么错。下面是我的代码。

     [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";
            }

但是,下面的控制台应用程序已成功将消息发布到服务总线主题。这是代码

    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="

}

有什么主意吗?

-艾伦-

1 个答案:

答案 0 :(得分:3)

Connection属性应该收到一个设置键,而不是实际的连接字符串。

如果要在本地尝试,则必须更改local.settings.json文件,并将连接字符串添加到特定键:

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

然后,按如下所示在您的函数中使用它:

[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";
}

应该可以。
让我知道是否。 :)