diff --git a/Directory.Packages.props b/Directory.Packages.props index 30d84aaa50..05229f9594 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -17,6 +17,7 @@ + @@ -88,7 +89,6 @@ - @@ -190,5 +190,6 @@ + - + \ No newline at end of file diff --git a/docs/en/framework/infrastructure/event-bus/distributed/azure.md b/docs/en/framework/infrastructure/event-bus/distributed/azure.md index 8e1bff3e63..92a961230d 100644 --- a/docs/en/framework/infrastructure/event-bus/distributed/azure.md +++ b/docs/en/framework/infrastructure/event-bus/distributed/azure.md @@ -137,4 +137,14 @@ Configure(options => }); ```` +Use `TokenCredential` instead of `ConnectionString` if you want to use custom credential. + +````csharp +Configure(options => +{ + options.Connections.Default.FullyQualifiedNamespace = "sb-my-app.servicebus.windows.net"; + options.Connections.Default.TokenCredential = new DefaultAzureCredential(); +}); +```` + Using these options classes can be combined with the `appsettings.json` way. Configuring an option property in the code overrides the value in the configuration file. diff --git a/framework/src/Volo.Abp.AzureServiceBus/Volo.Abp.AzureServiceBus.csproj b/framework/src/Volo.Abp.AzureServiceBus/Volo.Abp.AzureServiceBus.csproj index 4a360e8389..fe6a6a9708 100644 --- a/framework/src/Volo.Abp.AzureServiceBus/Volo.Abp.AzureServiceBus.csproj +++ b/framework/src/Volo.Abp.AzureServiceBus/Volo.Abp.AzureServiceBus.csproj @@ -17,6 +17,7 @@ + diff --git a/framework/src/Volo.Abp.AzureServiceBus/Volo/Abp/AzureServiceBus/ClientConfig.cs b/framework/src/Volo.Abp.AzureServiceBus/Volo/Abp/AzureServiceBus/ClientConfig.cs index 19db1beef7..2328018b74 100644 --- a/framework/src/Volo.Abp.AzureServiceBus/Volo/Abp/AzureServiceBus/ClientConfig.cs +++ b/framework/src/Volo.Abp.AzureServiceBus/Volo/Abp/AzureServiceBus/ClientConfig.cs @@ -1,3 +1,4 @@ +using Azure.Core; using Azure.Messaging.ServiceBus; using Azure.Messaging.ServiceBus.Administration; @@ -5,7 +6,7 @@ namespace Volo.Abp.AzureServiceBus; public class ClientConfig { - public string ConnectionString { get; set; } = default!; + public string? ConnectionString { get; set; } public ServiceBusAdministrationClientOptions Admin { get; set; } = new(); @@ -15,4 +16,8 @@ public class ClientConfig { AutoCompleteMessages = false }; + + public TokenCredential? TokenCredential { get; set; } + + public string? FullyQualifiedNamespace { get; set; } } diff --git a/framework/src/Volo.Abp.AzureServiceBus/Volo/Abp/AzureServiceBus/ConnectionPool.cs b/framework/src/Volo.Abp.AzureServiceBus/Volo/Abp/AzureServiceBus/ConnectionPool.cs index df830cde7a..ee4bfdf011 100644 --- a/framework/src/Volo.Abp.AzureServiceBus/Volo/Abp/AzureServiceBus/ConnectionPool.cs +++ b/framework/src/Volo.Abp.AzureServiceBus/Volo/Abp/AzureServiceBus/ConnectionPool.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Concurrent; using System.Linq; using System.Threading.Tasks; +using Azure.Identity; using Azure.Messaging.ServiceBus; using Azure.Messaging.ServiceBus.Administration; using Microsoft.Extensions.Logging; @@ -35,7 +36,17 @@ public class ConnectionPool : IConnectionPool, ISingletonDependency connectionName, new Lazy(() => { var config = _options.Connections.GetOrDefault(connectionName); - return new ServiceBusClient(config.ConnectionString, config.Client); + if (!config.ConnectionString.IsNullOrWhiteSpace()) + { + return new ServiceBusClient(config.ConnectionString, config.Client); + } + + if (!config.FullyQualifiedNamespace.IsNullOrWhiteSpace() && config.TokenCredential != null) + { + return new ServiceBusClient(config.FullyQualifiedNamespace, config.TokenCredential, config.Client); + } + + throw new InvalidOperationException($"{connectionName} does not have a valid Service Bus connection configuration."); }) ).Value; } @@ -47,7 +58,17 @@ public class ConnectionPool : IConnectionPool, ISingletonDependency connectionName, new Lazy(() => { var config = _options.Connections.GetOrDefault(connectionName); - return new ServiceBusAdministrationClient(config.ConnectionString, config.Admin); + if (!config.ConnectionString.IsNullOrWhiteSpace()) + { + return new ServiceBusAdministrationClient(config.ConnectionString, config.Admin); + } + + if (!config.FullyQualifiedNamespace.IsNullOrWhiteSpace() && config.TokenCredential != null) + { + return new ServiceBusAdministrationClient(config.FullyQualifiedNamespace, config.TokenCredential, config.Admin); + } + + throw new InvalidOperationException($"{connectionName} does not have a valid Service Bus connection configuration."); }) ).Value; }