【问题标题】:Azure Functions triggered by servicebus complains about connection string 'AzureWebJobsServiceBus' is missing or empty由 servicebus 触发的 Azure Functions 抱怨连接字符串“AzureWebJobsServiceBus”丢失或为空
【发布时间】:2020-08-25 19:08:09
【问题描述】:

当我在 local.settings.json 中有一个有效的 AzureWebJobsServiceBus 时,我有一个 .netcore Azure Functions 项目在 Visual Studio 2019 中运行良好,但如果它缺失或为空,则无法编译。我正在使用 AD 向服务总线验证我的功能,而不是通过连接字符串。 AzureWebJobsServiceBus 在我的项目中的任何地方都没有使用。 这是我的 project.cs

我使用 Azure.Identity 包并按照this post 使用我的凭据登录到 Azure,这是我正在运行的函数:

    [FunctionName("ProcessNewMessage")]
    public async Task ProcessPaymentMessage([ServiceBusTrigger("topic", "subscription")] Message message, ILogger log) {
        var tokenProvider = TokenProvider.CreateManagedIdentityTokenProvider();
        QueueClient queueClient = new QueueClient($"sb://{Environment.GetEnvironmentVariable("ServiceBusEndPoint")}", Environment.GetEnvironmentVariable("GenericAuditQueueName"), tokenProvider);

        await queueClient.SendAsync(message);
    }

在使用 AD 进行身份验证之前,我使用的是连接字符串,这也可以,但建议使用 AD。

总而言之,当提供了连接字符串但我的代码未使用时,我的 Azure 函数可与服务总线触发器一起使用。如何在没有连接字符串的情况下使我的函数与 AD 一起工作?

非常感谢

【问题讨论】:

    标签: azure-active-directory azure-functions azureservicebus


    【解决方案1】:

    您无法从 Azure 函数应用服务总线触发器中删除服务总线连接字符串,因为内部 SDK 使用它来建立连接。

    使用 key Vault 在 Function App 中保存 Service bus 的连接字符串: Key Vault 引用功能使您的应用程序可以像使用应用程序设置一样工作,这意味着不需要更改代码。您可以从我们的Key Vault reference documentation 获得所有详细信息,但我将在此处概述基础知识。

    此功能需要为您的应用提供system-assigned managed identity。在这篇文章的后面,我将讨论用户分配的身份,但我们暂时将这些预览分开。

    然后,您需要在 Key Vault 上配置访问策略,为您的应用程序授予对机密的 GET 权限。了解如何configure an access policy

    最后,将任何应用程序设置的值设置为以下格式的引用:

    @Microsoft.KeyVault(SecretUri=secret_uri_with_version)
    

    其中 secret_uri_with_version 是 Key Vault 中机密的完整 URI。例如,这将类似于:https://myvault.vault.azure.net/secrets/azurewebjobsservicebussecret/ec96f02080254f109c51a1f14cdb1931

    您可以将 MSI 用于 Azure Function App 和服务总线:

    函数应用的 MSI: https://docs.microsoft.com/en-us/azure/app-service/overview-managed-identity?tabs=dotnet#add-a-system-assigned-identity

    服务总线的 MSI: https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-managed-service-identity#use-service-bus-with-managed-identities-for-azure-resources

    【讨论】:

    • 感谢您指出在应用设置中使用@Microsoft.KeyVault 的方法,这比将连接字符串放在应用设置中要好。几天后我会试一试。如果我们可以使用纯 AD 进行身份验证,那就太好了。
    • 这是我能得到的最接近的答案。希望下个 SDK 版本只能使用 AD 进行身份验证。
    • 如果这对您有用,您可以将其标记为答案以帮助其他社区人员。
    【解决方案2】:

    如果要在 Azure 函数中使用 Azure AD auth 配置 Azure 服务总线触发器,请将服务总线连接字符串设置为Endpoint=sb://<service-bus-resource>.servicebus.windows.net;Authentication=Managed Identity;。更多详情请参考here

    例如

    1. Enable MSI fro Azure function

    2. Assign Azure RABC role (Azure Service Bus Data Owner) for the MSI

    az role assignment create \
        --role $service_bus_role \
        --assignee $assignee_id \
        --scope /subscriptions/$subscription_id/resourceGroups/$resource_group/providers/Microsoft.ServiceBus/namespaces/$service_bus_namespace
    
    1. 在 Azure 函数应用程序设置中添加服务总线连接字符串。

    2. 代码

     public static void Run([ServiceBusTrigger("test", "test", Connection = "myQueueConn")]string mySbMsg, ILogger log)
            {
                log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
            }
    

    【讨论】:

    • 谢谢吉姆。我已经尝试了上述方法,但它仍然抱怨缺少连接字符串。这不是抱怨我没有通过身份验证,而是缺少连接字符串。
    • @JustinCurious 您是否尝试将代码ProcessPaymentMessage([ServiceBusTrigger("topic", "subscription")] 更新为ProcessPaymentMessage([ServiceBusTrigger("topic", "subscription",Connection ="<connection string>")]
    • 感谢您的建议,它在使用连接字符串时对我有用,我已经说过了。目的是不使用连接字符串,而仅使用 AD。
    • @JustinCurious 当我们将连接字符串更新为Endpoint=sb://<service-bus-resource>.servicebus.windows.net;Authentication=Managed Identity; 时,它将告诉 azure 函数使用 MSI 访问服务总线。我认为它符合您的目的。此外,当我们使用服务总线触发器时,我们需要提供一个连接字符串。否则函数无法成功运行。
    • 是的,当我有连接字符串时,我什至不需要 Authentication=Managed Identity。当我看到这个建议时,我立刻觉得它会解决我的问题。不幸的是,当我删除连接字符串时,它有编译错误。正如上面@HarshitaSingh-MSFT所说,连接字符串是SDK内部使用的,那么我不明白使用AD的目的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2018-02-02
    • 1970-01-01
    相关资源
    最近更新 更多