diff --git a/All.sln b/All.sln index 4304170..7ee27d9 100644 --- a/All.sln +++ b/All.sln @@ -7,7 +7,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{17CDA34C-429 EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "IdentityServer", "IdentityServer", "{D264CAEC-2137-46D8-B637-FCE7CBE3B390}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Messaging.AzureServiceBus", "src\IdentityServer\Messaging.AzureServiceBus\Messaging.AzureServiceBus.csproj", "{C8F188CA-2671-4364-B53C-43832627C0DA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Messaging.Azure", "src\IdentityServer\Messaging.Azure\Messaging.Azure.csproj", "{C8F188CA-2671-4364-B53C-43832627C0DA}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Messaging.RabbitMQ", "src\IdentityServer\Messaging.RabbitMQ\Messaging.RabbitMQ.csproj", "{9C1A424A-64A5-4BAA-BCE5-529F2148497F}" EndProject diff --git a/src/IdentityServer/Messaging.AzureEventHub/EventHubSender.cs b/src/IdentityServer/Messaging.Azure/EventHubSender.cs similarity index 90% rename from src/IdentityServer/Messaging.AzureEventHub/EventHubSender.cs rename to src/IdentityServer/Messaging.Azure/EventHubSender.cs index 6d8267a..4c3b6f3 100644 --- a/src/IdentityServer/Messaging.AzureEventHub/EventHubSender.cs +++ b/src/IdentityServer/Messaging.Azure/EventHubSender.cs @@ -7,7 +7,7 @@ using MassTransit; using Microsoft.Extensions.Hosting; -namespace IdOps.IdentityServer.AzureEventHub; +namespace IdOps.IdentityServer.Azure; public sealed class EventHubSender : BackgroundService, IEventSenderWorker { @@ -26,7 +26,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await Task.Yield(); - var producer = await _producerProvider.GetProducer("identity-events"); + IEventHubProducer producer = await _producerProvider.GetProducer("identity-events"); // we reuse the buffer to avoid allocations var buffer = new IdentityEventMessage[50]; @@ -34,7 +34,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (await _channelReader.WaitToReadAsync(stoppingToken)) { - // we read as many messages as we can + // we read as many messages as we can for (var i = 0; i < buffer.Length; i++) { if (!_channelReader.TryRead(out IdentityEventMessage? entity)) diff --git a/src/IdentityServer/Messaging.Azure/Extensions/AzureServiceBusIdOpsBuilderExtensions.cs b/src/IdentityServer/Messaging.Azure/Extensions/AzureServiceBusIdOpsBuilderExtensions.cs new file mode 100644 index 0000000..ee24f37 --- /dev/null +++ b/src/IdentityServer/Messaging.Azure/Extensions/AzureServiceBusIdOpsBuilderExtensions.cs @@ -0,0 +1,119 @@ +using System; +using Azure.Identity; +using IdOps.IdentityServer.Abstractions; +using IdOps.IdentityServer.Azure; +using MassTransit; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using static IdOps.IdentityServer.Wellknown.ConfigSections; + +namespace IdOps.IdentityServer.Azure +{ + public static class AzureServiceBusIdOpsBuilderExtensions + { + public static IIdOpsIdentityServerBuilder UseAzure(this BusBuilder builder) + { + AzureOptions? options = builder + .IdOpsBuilder.Configuration?.GetSection($"{Messaging}:Azure") + .Get(); + + if (options == null) + { + throw new ApplicationException( + "Could not get AzureOptions configuration from " + + $"{Messaging}:Azure." + + "Please check you configuration"); + } + + return builder.UseAzure(options); + } + + private static IIdOpsIdentityServerBuilder UseAzure( + this BusBuilder builder, + AzureOptions options) + { + if (options.EventHub is not null) + { + builder.IdOpsBuilder.Services.AddSingleton(); + } + + builder.IdOpsBuilder.Services.AddMassTransit(s => + { + builder.BusSetup?.Invoke(s); + + if (options.ServiceBus is { }) + { + s.RegisterServiceBus(options.ServiceBus, builder); + } + + if (options.EventHub is { } eventHub) + { + s.RegisterEventHub(eventHub); + } + }); + + return builder.IdOpsBuilder; + } + + private static void RegisterEventHub( + this IBusRegistrationConfigurator configurator, + EventHubOptions eventHub) + { + configurator.AddRider(x => + x.UsingEventHub((_, k) => + { + if (eventHub.Namespace is { } @namespace) + { + k.Host(@namespace, new DefaultAzureCredential()); + } + else if (eventHub.ConnectionString is not null) + { + k.Host(eventHub.ConnectionString); + } + else + { + throw new ApplicationException( + "EventHub configuration is missing. Please check your settings."); + } + + if (eventHub.Storage is { } storageOption) + { + if (storageOption.Url is { } url) + { + k.Storage(new Uri(url), new DefaultAzureCredential()); + } + else if (storageOption.ConnectionString is { } connectionString) + { + k.Storage(connectionString); + } + else + { + throw new ApplicationException( + "EventHub storage configuration is missing. Please check your settings."); + } + } + }) + ); + } + + private static void RegisterServiceBus( + this IBusRegistrationConfigurator configurator, + AzureServiceBusOptions options, + BusBuilder builder) + { + configurator.UsingAzureServiceBus((provider, cfg) => + { + var serverGroup = builder.IdOpsBuilder.Options!.ServerGroup.ToLower(); + var environmentName = builder.IdOpsBuilder.Options!.EnvironmentName.ToLower(); + cfg.Host(options.ConnectionString); + cfg.ReceiveEndpoint( + $"id-{serverGroup}-{environmentName}", + e => + { + e.ConfigureConsumers(provider); + e.PrefetchCount = options.PrefetchCount; + }); + }); + } + } +} diff --git a/src/IdentityServer/Messaging.AzureServiceBus/Messaging.AzureServiceBus.csproj b/src/IdentityServer/Messaging.Azure/Messaging.Azure.csproj similarity index 91% rename from src/IdentityServer/Messaging.AzureServiceBus/Messaging.AzureServiceBus.csproj rename to src/IdentityServer/Messaging.Azure/Messaging.Azure.csproj index 37efdb6..4819acf 100644 --- a/src/IdentityServer/Messaging.AzureServiceBus/Messaging.AzureServiceBus.csproj +++ b/src/IdentityServer/Messaging.Azure/Messaging.Azure.csproj @@ -10,6 +10,7 @@ + diff --git a/src/IdentityServer/Messaging.Azure/Options/AzureOptions.cs b/src/IdentityServer/Messaging.Azure/Options/AzureOptions.cs new file mode 100644 index 0000000..40658c5 --- /dev/null +++ b/src/IdentityServer/Messaging.Azure/Options/AzureOptions.cs @@ -0,0 +1,8 @@ +namespace IdOps.IdentityServer.Azure; + +public sealed class AzureOptions +{ + public AzureServiceBusOptions? ServiceBus { get; set; } = default!; + public EventHubOptions? EventHub { get; set; } = default!; +} + diff --git a/src/IdentityServer/Messaging.Azure/Options/AzureServiceBusOptions.cs b/src/IdentityServer/Messaging.Azure/Options/AzureServiceBusOptions.cs new file mode 100644 index 0000000..4d0eb0d --- /dev/null +++ b/src/IdentityServer/Messaging.Azure/Options/AzureServiceBusOptions.cs @@ -0,0 +1,9 @@ +namespace IdOps.IdentityServer.Azure; + +public class AzureServiceBusOptions +{ + public string ConnectionString { get; set; } = default!; + + public int PrefetchCount { get; set; } = 10; +} + diff --git a/src/IdentityServer/Messaging.AzureEventHub/EventHubOptions.cs b/src/IdentityServer/Messaging.Azure/Options/EventHubOptions.cs similarity index 79% rename from src/IdentityServer/Messaging.AzureEventHub/EventHubOptions.cs rename to src/IdentityServer/Messaging.Azure/Options/EventHubOptions.cs index e237c3f..6e33a6c 100644 --- a/src/IdentityServer/Messaging.AzureEventHub/EventHubOptions.cs +++ b/src/IdentityServer/Messaging.Azure/Options/EventHubOptions.cs @@ -1,4 +1,4 @@ -namespace IdOps.IdentityServer.AzureEventHub; +namespace IdOps.IdentityServer.Azure; public class EventHubOptions { @@ -7,4 +7,4 @@ public class EventHubOptions public string? Namespace { get; set; } public EventStorageHubOptions? Storage { get; set; } -} \ No newline at end of file +} diff --git a/src/IdentityServer/Messaging.AzureEventHub/EventStorageHubOptions.cs b/src/IdentityServer/Messaging.Azure/Options/EventStorageHubOptions.cs similarity index 74% rename from src/IdentityServer/Messaging.AzureEventHub/EventStorageHubOptions.cs rename to src/IdentityServer/Messaging.Azure/Options/EventStorageHubOptions.cs index 51eb47a..50eb1ed 100644 --- a/src/IdentityServer/Messaging.AzureEventHub/EventStorageHubOptions.cs +++ b/src/IdentityServer/Messaging.Azure/Options/EventStorageHubOptions.cs @@ -1,4 +1,4 @@ -namespace IdOps.IdentityServer.AzureEventHub; +namespace IdOps.IdentityServer.Azure; public sealed class EventStorageHubOptions { diff --git a/src/IdentityServer/Messaging.AzureEventHub/EventHubIdOpsBuilderExtensions.cs b/src/IdentityServer/Messaging.AzureEventHub/EventHubIdOpsBuilderExtensions.cs deleted file mode 100644 index cf721d6..0000000 --- a/src/IdentityServer/Messaging.AzureEventHub/EventHubIdOpsBuilderExtensions.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System; -using Azure.Identity; -using IdOps.IdentityServer.Abstractions; -using MassTransit; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; - -namespace IdOps.IdentityServer.AzureEventHub; - -public static class EventHubIdOpsBuilderExtensions -{ - public static IIdOpsIdentityServerBuilder UseEventHub(this BusBuilder builder) - { - EventHubOptions? options = builder.IdOpsBuilder.Configuration? - .GetSection($"{Wellknown.ConfigSections.Messaging}:EventHub") - .Get(); - - if (options == null) - { - throw new ApplicationException( - "Could not get EventHub configuration from " + - $"{Wellknown.ConfigSections.Messaging}:EventHub." + - "Please check you configuration"); - } - - return builder.UseEventHub(options); - } - - public static IIdOpsIdentityServerBuilder UseEventHub( - this BusBuilder builder, - EventHubOptions options) - { - builder.IdOpsBuilder.Services.AddSingleton(); - builder.IdOpsBuilder.Services.AddMassTransit(s => - { - builder.BusSetup?.Invoke(s); - - s.AddRider(x => x.UsingEventHub((_, k) => - { - if (options.Namespace is { } @namespace) - { - k.Host(@namespace, new DefaultAzureCredential()); - } - else if (options.ConnectionString is not null) - { - k.Host(options.ConnectionString); - } - else - { - throw new ApplicationException( - "EventHub configuration is missing. Please check your settings."); - } - - if (options.Storage is { } storageOption) - { - if (storageOption.Url is { } url) - { - k.Storage(new Uri(url), new DefaultAzureCredential()); - } - else if (storageOption.ConnectionString is { } connectionString) - { - k.Storage(connectionString); - } - else - { - throw new ApplicationException( - "EventHub storage configuration is missing. Please check your settings."); - } - } - })); - }); - - return builder.IdOpsBuilder; - } - - public static IIdOpsIdentityServerBuilder UseEventHub( - this BusBuilder builder, - Action setupAction) - { - var options = new EventHubOptions(); - setupAction.Invoke(options); - - return builder.UseEventHub(options); - } -} diff --git a/src/IdentityServer/Messaging.AzureEventHub/Messaging.AzureEventHub.csproj b/src/IdentityServer/Messaging.AzureEventHub/Messaging.AzureEventHub.csproj deleted file mode 100644 index 86843e6..0000000 --- a/src/IdentityServer/Messaging.AzureEventHub/Messaging.AzureEventHub.csproj +++ /dev/null @@ -1,19 +0,0 @@ - - - - IdOps.IdentityServer.AzureEventHub - IdOps.IdentityServer.AzureEventHub - IdOps.IdentityServer.Messaging.AzureEventHub - true - - - - - - - - - - - - diff --git a/src/IdentityServer/Messaging.AzureServiceBus/AzureServiceBusIdOpsBuilderExtensions.cs b/src/IdentityServer/Messaging.AzureServiceBus/AzureServiceBusIdOpsBuilderExtensions.cs deleted file mode 100644 index 95884d6..0000000 --- a/src/IdentityServer/Messaging.AzureServiceBus/AzureServiceBusIdOpsBuilderExtensions.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System; -using MassTransit; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; - -namespace IdOps.IdentityServer.AzureServiceBus -{ - public static class AzureServiceBusIdOpsBuilderExtensions - { - public static IIdOpsIdentityServerBuilder UseAzureServiceBus(this BusBuilder builder) - { - AzureServiceBusOptions? options = builder.IdOpsBuilder.Configuration? - .GetSection($"{Wellknown.ConfigSections.Messaging}:AzureServiceBus") - .Get(); - - if (options == null) - { - throw new ApplicationException( - "Could not get AzureServiceBus configuration from " + - $"{Wellknown.ConfigSections.Messaging}:AzureServiceBus." + - "Please check you configuration"); - } - - return builder.UseAzureServiceBus(options); - } - - public static IIdOpsIdentityServerBuilder UseAzureServiceBus( - this BusBuilder builder, - AzureServiceBusOptions options) - { - builder.IdOpsBuilder.Services.AddMassTransit(s => - { - builder.BusSetup?.Invoke(s); - - s.UsingAzureServiceBus((provider, cfg) => - { - cfg.Host(options.ConnectionString); - cfg.ReceiveEndpoint($"id-" + - $"{builder.IdOpsBuilder.Options!.ServerGroup.ToLower()}-" + - $"{builder.IdOpsBuilder.Options!.EnvironmentName.ToLower()}", - e => - { - e.ConfigureConsumers(provider); - e.PrefetchCount = options.PrefetchCount; - }); - }); - }); - - return builder.IdOpsBuilder; - } - - public static IIdOpsIdentityServerBuilder UseAzureServiceBus( - this BusBuilder builder, - Action setupAction) - { - var options = new AzureServiceBusOptions(); - setupAction.Invoke(options); - - return builder.UseAzureServiceBus(options); - } - } -} diff --git a/src/IdentityServer/Messaging.AzureServiceBus/AzureServiceBusOptions.cs b/src/IdentityServer/Messaging.AzureServiceBus/AzureServiceBusOptions.cs deleted file mode 100644 index cd96fb9..0000000 --- a/src/IdentityServer/Messaging.AzureServiceBus/AzureServiceBusOptions.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace IdOps.IdentityServer.AzureServiceBus -{ - public class AzureServiceBusOptions - { - public string ConnectionString { get; set; } = default!; - - public int PrefetchCount { get; set; } = 10; - } -} diff --git a/src/IdentityServer/src/Abstractions/IIdOpsIdentityServerBuilder.cs b/src/IdentityServer/src/Abstractions/IIdOpsIdentityServerBuilder.cs index b3a4308..52f4cf0 100644 --- a/src/IdentityServer/src/Abstractions/IIdOpsIdentityServerBuilder.cs +++ b/src/IdentityServer/src/Abstractions/IIdOpsIdentityServerBuilder.cs @@ -18,18 +18,11 @@ public class IdOpsOptions public MessagingOptions Messaging { get; set; } = default!; - public EventHubOptions? EventHub { get; set; } = default!; - public string ServerGroup { get; set; } = default!; public bool EnableDataConnectors { get; set; } } - public class EventHubOptions - { - public string ConnectionString { get; set; } = default!; - } - public class MessagingOptions { public MessagingTransport Transport { get; set; } = MessagingTransport.Memory; diff --git a/src/Server/src/Core/Configuration/IdOpsServerBuilderExtensions.cs b/src/Server/src/Core/Configuration/IdOpsServerBuilderExtensions.cs index d2b44ed..b904b8c 100644 --- a/src/Server/src/Core/Configuration/IdOpsServerBuilderExtensions.cs +++ b/src/Server/src/Core/Configuration/IdOpsServerBuilderExtensions.cs @@ -6,7 +6,6 @@ using IdOps.IdentityServer.Hashing; using IdOps.Model; using MassTransit; -using Microsoft.Extensions.Azure; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection;