From 2f6295b18d18e530cae02844264388ad38896d34 Mon Sep 17 00:00:00 2001 From: PascalSenn Date: Mon, 3 Jun 2024 21:40:08 +0200 Subject: [PATCH] Add IdenttiyServer.Messaging.Azure --- .../Messaging.Azure/EventHubSender.cs | 12 ++++++++---- .../AzureServiceBusIdOpsBuilderExtensions.cs | 18 ------------------ .../Messaging.Azure/Options/EventHubOptions.cs | 2 -- .../Options/EventStorageHubOptions.cs | 8 -------- .../samples/Server/IdentityServer.csproj | 3 +-- src/IdentityServer/samples/Server/Startup.cs | 10 ++++------ .../IIdOpsIdentityServerBuilder.cs | 2 +- 7 files changed, 14 insertions(+), 41 deletions(-) delete mode 100644 src/IdentityServer/Messaging.Azure/Options/EventStorageHubOptions.cs diff --git a/src/IdentityServer/Messaging.Azure/EventHubSender.cs b/src/IdentityServer/Messaging.Azure/EventHubSender.cs index 4c3b6f3..32e00f9 100644 --- a/src/IdentityServer/Messaging.Azure/EventHubSender.cs +++ b/src/IdentityServer/Messaging.Azure/EventHubSender.cs @@ -5,28 +5,32 @@ using IdOps.IdentityServer.Abstractions; using IdOps.Messages; using MassTransit; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace IdOps.IdentityServer.Azure; public sealed class EventHubSender : BackgroundService, IEventSenderWorker { + private readonly IServiceProvider _serviceProvider; private readonly ChannelReader _channelReader; - private readonly IEventHubProducerProvider _producerProvider; public EventHubSender( - IEventHubProducerProvider provider, + IServiceProvider serviceProvider, ChannelReader channelReader) { - _producerProvider = provider; + _serviceProvider = serviceProvider; _channelReader = channelReader; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await Task.Yield(); + await using AsyncServiceScope scope = _serviceProvider.CreateAsyncScope(); + IEventHubProducerProvider provider = + scope.ServiceProvider.GetRequiredService(); - IEventHubProducer producer = await _producerProvider.GetProducer("identity-events"); + IEventHubProducer producer = await provider.GetProducer("identity-events"); // we reuse the buffer to avoid allocations var buffer = new IdentityEventMessage[50]; diff --git a/src/IdentityServer/Messaging.Azure/Extensions/AzureServiceBusIdOpsBuilderExtensions.cs b/src/IdentityServer/Messaging.Azure/Extensions/AzureServiceBusIdOpsBuilderExtensions.cs index ee24f37..118f12e 100644 --- a/src/IdentityServer/Messaging.Azure/Extensions/AzureServiceBusIdOpsBuilderExtensions.cs +++ b/src/IdentityServer/Messaging.Azure/Extensions/AzureServiceBusIdOpsBuilderExtensions.cs @@ -1,7 +1,6 @@ using System; using Azure.Identity; using IdOps.IdentityServer.Abstractions; -using IdOps.IdentityServer.Azure; using MassTransit; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -75,23 +74,6 @@ private static void RegisterEventHub( 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."); - } - } }) ); } diff --git a/src/IdentityServer/Messaging.Azure/Options/EventHubOptions.cs b/src/IdentityServer/Messaging.Azure/Options/EventHubOptions.cs index 6e33a6c..c9a0df3 100644 --- a/src/IdentityServer/Messaging.Azure/Options/EventHubOptions.cs +++ b/src/IdentityServer/Messaging.Azure/Options/EventHubOptions.cs @@ -5,6 +5,4 @@ public class EventHubOptions public string? ConnectionString { get; set; } public string? Namespace { get; set; } - - public EventStorageHubOptions? Storage { get; set; } } diff --git a/src/IdentityServer/Messaging.Azure/Options/EventStorageHubOptions.cs b/src/IdentityServer/Messaging.Azure/Options/EventStorageHubOptions.cs deleted file mode 100644 index 50eb1ed..0000000 --- a/src/IdentityServer/Messaging.Azure/Options/EventStorageHubOptions.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace IdOps.IdentityServer.Azure; - -public sealed class EventStorageHubOptions -{ - public string? ConnectionString { get; set; } - - public string? Url { get; set; } -} diff --git a/src/IdentityServer/samples/Server/IdentityServer.csproj b/src/IdentityServer/samples/Server/IdentityServer.csproj index 0213daf..917da20 100644 --- a/src/IdentityServer/samples/Server/IdentityServer.csproj +++ b/src/IdentityServer/samples/Server/IdentityServer.csproj @@ -11,8 +11,7 @@ - - + diff --git a/src/IdentityServer/samples/Server/Startup.cs b/src/IdentityServer/samples/Server/Startup.cs index 7b1f264..c2bc923 100644 --- a/src/IdentityServer/samples/Server/Startup.cs +++ b/src/IdentityServer/samples/Server/Startup.cs @@ -1,6 +1,5 @@ using System; -using IdOps.IdentityServer.AzureEventHub; -using IdOps.IdentityServer.AzureServiceBus; +using IdOps.IdentityServer.Azure; using IdOps.IdentityServer.Events; using IdOps.IdentityServer.RabbitMQ; using IdOps.IdentityServer.Samples.DataSeeding; @@ -50,12 +49,11 @@ public void ConfigureServices(IServiceCollection services) case MessagingTransport.RabbitMq: busBuilder.UseRabbitMq(); break; - case MessagingTransport.AzureServiceBus: - busBuilder.UseAzureServiceBus(); + case MessagingTransport.Azure: + busBuilder.UseAzure(); break; } - - busBuilder.UseEventHub(); + }).AddProfileService(); services.AddSingleton(); diff --git a/src/IdentityServer/src/Abstractions/IIdOpsIdentityServerBuilder.cs b/src/IdentityServer/src/Abstractions/IIdOpsIdentityServerBuilder.cs index 52f4cf0..c8dd3db 100644 --- a/src/IdentityServer/src/Abstractions/IIdOpsIdentityServerBuilder.cs +++ b/src/IdentityServer/src/Abstractions/IIdOpsIdentityServerBuilder.cs @@ -32,6 +32,6 @@ public enum MessagingTransport { Memory, RabbitMq, - AzureServiceBus + Azure } }