AzureClientServiceCollectionExtensions.AddAzureClients可以被多次调用吗?

huangapple go评论68阅读模式
英文:

Can AzureClientServiceCollectionExtensions.AddAzureClients be called multiple times?

问题

AzureClientServiceCollectionExtensions.AddAzureClients 可以被多次调用吗,而不会出现任何问题,即使在多个方法调用之间分散开来,所有的客户端都会被追加/添加到同一个构建器中吗?

为了背景信息:我已经将 Azure 存储账户和 Azure 服务总线的一般设置和配置拆分到不同的文件中。我希望从每个文件中调用 AddAzureClients,以便分别添加 ServiceBusClientBlobServiceClient

英文:

Can AzureClientServiceCollectionExtensions.AddAzureClients be called multiple times without any problems, i.e. will all clients be appended/added to the same builder, even when spread across multiple method invocations?

For context: I have split up the general setup and configuration for Azure Storage Account and for Azure Service Bus in separate files. From each file I would like to call AddAzureClients in order to add ServiceBusClient and BlobServiceClient respectively.

答案1

得分: 2

简短回答,很可能可以。

根据 GitHub 上的实现 https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/extensions/Microsoft.Extensions.Azure/src/AzureClientServiceCollectionExtensions.cs 来看,多次调用 AddAzureClients 应该是可以的。它的实现使用 TryAddSingleton 方法注册所需的类型,只有在这些类型尚未注册时才会注册。至于调用 AddAzureClients 是否会出现问题,该方法是 IServiceCollection 类型的扩展方法,只要每次调用都在同一个服务集合上,应该是可以的。

英文:

Short answer, most likely.

Long answer, judging by the implementation in GitHub https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/extensions/Microsoft.Extensions.Azure/src/AzureClientServiceCollectionExtensions.cs multiple calls to the AddAzureClients should be fine. It's implementation makes registration calls for it's required types using the TryAddSingleton method which will only register types if they haven't already been registered. As for whether all the calls to the AddAzureClients will be fine as that method is an extension method on the IServiceCollection type so long as each of your subsequent calls is on the same service collection you should be fine.

huangapple
  • 本文由 发表于 2023年3月15日 18:25:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743420.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定