Reputation: 2674
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
Reputation: 35124
Your code looks fine.
Put your QueueConnectionString
under Values
not ConnectionStrings
.
Upvotes: 2