0

Is it possible to connect to a Service Bus Topic using App Service Function App's MSI? Any documentations or examples to perform this via ARM Template or .NET SDK (C#) would be helpful.

user989988
  • 3,006
  • 7
  • 44
  • 91

1 Answers1

1

See example below:

namespaces:

using Microsoft.Azure.ServiceBus;

using Microsoft.Azure.ServiceBus.Primitives;

Adding services to the container:

services.Configure<ServiceBusConfiguration>(Configuration.GetSection("ServiceBus"));

Main implementation:

public class ServiceBusConfiguration
{
    public string Namespace { get; set; }
    public string Topic { get; set; }
}

public class HomeController : Controller
{
    public ServiceBusConfiguration Config { get; }

    public HomeController(IOptions<ServiceBusConfiguration> serviceBusConfig)
    {
       Config = serviceBusConfig.Value;
    }

    [HttpPost]
    public async Task<ActionResult> Send(ServiceBusMessageData messageInfo)
    {
      if (string.IsNullOrEmpty(messageInfo.MessageToSend))
      {
        return RedirectToAction("Index");
      }

      var tokenProvider = TokenProvider.CreateManagedServiceIdentityTokenProvider();
      var sendClient = new TopicClient($"sb://{Config.Namespace}.servicebus.windows.net/", Config.Topic, tokenProvider);
      await sendClient.SendAsync(new Message(Encoding.UTF8.GetBytes(messageInfo.MessageToSend)));
      await sendClient.CloseAsync();

      return RedirectToAction("Index");
   }
}

To get more details please see Use Service Bus from App Service with Managed Service Identity

Eugene D
  • 347
  • 1
  • 3
  • 17