使用 Function App 的 MSI 连接到主题

Connect to Topic using Function App's MSI

是否可以使用 App Service Function App 的 MSI 连接到服务总线主题?通过 ARM 模板或 .NET SDK (C#) 执行此操作的任何文档或示例都会有所帮助。

参见下面的示例:

命名空间:

using Microsoft.Azure.ServiceBus;

using Microsoft.Azure.ServiceBus.Primitives;

正在向容器添加服务:

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

主要实现:

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

要了解更多详情,请参阅Use Service Bus from App Service with Managed Service Identity