kk-dev11
kk-dev11

Reputation: 2674

Use ServiceBus in Azure Functions from Visual Studio

I would like to write to a service bus queue from Azure Function. But the ServiceBus configuration doesn't work.

I have the following dependencies:

Could anyone please help me how to connect to the service bus?

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 (updated)

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

Upvotes: 3

Views: 2192

Answers (1)

Mikhail Shilkov
Mikhail Shilkov

Reputation: 35124

Your code looks fine.

Put your QueueConnectionString under Values not ConnectionStrings.

Upvotes: 2

Related Questions