在 Visual Studio 的 Azure Functions 中使用 ServiceBus

Use ServiceBus in Azure Functions from Visual Studio

我想从 Azure Function 写入服务总线队列。但是 ServiceBus 配置不起作用。

我有以下依赖项:

谁能帮我连接到服务总线?

using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using Microsoft.Azure.WebJobs.Host;

namespace HttpTrigger
{
    public static class RegisterDevice
    {
        [FunctionName("RegisterDevice")]
        public static IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            [ServiceBus("deviceupdates", Connection = "ServiceBusConnection")] ICollector<string> outputSbMsg,
            TraceWriter log)
        {

        log.Info("C# HTTP trigger function processed a request.");

        string name = req.Query["name"];

        string requestBody = new StreamReader(req.Body).ReadToEnd();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        name = name ?? data?.name;

        outputSbMsg.Add(name);

        return name != null
            ? (ActionResult)new OkObjectResult($"Hello, {name}")
             : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
    }
}

}

local.settings.json(更新)

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true",
    "ServiceBusConnection":"SecretConnectionString"
  }
}

您的代码看起来不错。

将您的 QueueConnectionString 放在 Values 而不是 ConnectionStrings 下。