From 641929a8fe39cbdf84402f61cdcaa36d34a2cbe8 Mon Sep 17 00:00:00 2001 From: Tigran TIKSN Torosyan Date: Fri, 30 Jan 2026 19:35:35 -0600 Subject: [PATCH] Add ServiceIdentityProvider and update dependency injection setup --- Directory.Packages.props | 1 + .../Messaging/ServiceCollectionExtensions.cs | 9 +++++- Source/Messaging/ServiceIdentityProvider.cs | 29 +++++++++++++++++++ Tests/Messaging.Test/Messaging.Test.csproj | 1 + .../ServiceIdentityProviderTests.cs | 18 +++--------- 5 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 Source/Messaging/ServiceIdentityProvider.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index 914a744..4b4e8a6 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -8,6 +8,7 @@ + diff --git a/Source/Messaging/ServiceCollectionExtensions.cs b/Source/Messaging/ServiceCollectionExtensions.cs index a30226b..706dbe3 100644 --- a/Source/Messaging/ServiceCollectionExtensions.cs +++ b/Source/Messaging/ServiceCollectionExtensions.cs @@ -1,6 +1,7 @@ namespace Fossa.Messaging; using CloudNative.CloudEvents; +using IdGen; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -14,15 +15,21 @@ public static class ServiceCollectionExtensions /// /// Input Service Collection. /// Configuration. + /// The application name. + /// The component names. /// Populated Service Collection. public static IServiceCollection AddMessaging( this IServiceCollection services, - IConfiguration configuration) + IConfiguration configuration, + string applicationName, + Seq componentNames) { _ = services.AddSingleton(); _ = services.AddSingleton(); _ = services.AddSingleton(); _ = services.AddSingleton(); + _ = services.AddSingleton(sp => + new ServiceIdentityProvider(sp.GetRequiredService(), applicationName, componentNames)); _ = services.Configure(configuration.GetRequiredSection("Messaging")); diff --git a/Source/Messaging/ServiceIdentityProvider.cs b/Source/Messaging/ServiceIdentityProvider.cs new file mode 100644 index 0000000..ade37df --- /dev/null +++ b/Source/Messaging/ServiceIdentityProvider.cs @@ -0,0 +1,29 @@ +namespace Fossa.Messaging; + +using IdGen; +using TIKSN.Identity; + +/// +/// Service Identity Provider. +/// +public class ServiceIdentityProvider : IServiceIdentityProvider +{ + private readonly ServiceIdentity serviceIdentity; + + /// + /// Initializes a new instance of the class. + /// + /// The id generator. + /// The application name. + /// The component names. + public ServiceIdentityProvider(IdGenerator idGenerator, string applicationName, Seq componentNames) + { + ArgumentNullException.ThrowIfNull(idGenerator); + ArgumentNullException.ThrowIfNull(applicationName); + + this.serviceIdentity = new ServiceIdentity(applicationName, componentNames, ServiceInstanceId.Create(idGenerator.Id)); + } + + /// + public ServiceIdentity GetIdentity() => this.serviceIdentity; +} diff --git a/Tests/Messaging.Test/Messaging.Test.csproj b/Tests/Messaging.Test/Messaging.Test.csproj index 7b427e5..b574345 100644 --- a/Tests/Messaging.Test/Messaging.Test.csproj +++ b/Tests/Messaging.Test/Messaging.Test.csproj @@ -9,6 +9,7 @@ + diff --git a/Tests/Messaging.Test/ServiceIdentityProviderTests.cs b/Tests/Messaging.Test/ServiceIdentityProviderTests.cs index 0c6c859..d835253 100644 --- a/Tests/Messaging.Test/ServiceIdentityProviderTests.cs +++ b/Tests/Messaging.Test/ServiceIdentityProviderTests.cs @@ -2,12 +2,11 @@ namespace Fossa.Messaging.Test; using Autofac; using Autofac.Extensions.DependencyInjection; +using IdGen.DependencyInjection; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Time.Testing; -using NSubstitute; using TIKSN.DependencyInjection; -using TIKSN.Identity; using Xunit; public class ServiceIdentityProviderTests @@ -50,23 +49,14 @@ public ServiceIdentityProviderTests() }) .Build(); var services = new ServiceCollection(); - _ = services.AddMessaging(configuration); - _ = services.AddFrameworkCore(); + _ = services.AddMessaging(configuration, "Fossa", Seq("Messaging", "Test")); + _ = services.AddFrameworkCore(); + _ = services.AddIdGen(9); var fakeTimeProvider = new FakeTimeProvider( new DateTimeOffset(2022, 9, 24, 0, 0, 0, TimeSpan.Zero)); _ = services.AddSingleton(fakeTimeProvider); - var serviceIdentityProvider = Substitute.For(); - _ = serviceIdentityProvider - .GetIdentity().Returns( - new ServiceIdentity( - applicationName: "Fossa", - componentNames: Seq("Messaging", "Test"), - instanceId: ServiceInstanceId.Create(Ulid.NewUlid()))); - - _ = services.AddSingleton(serviceIdentityProvider); - ContainerBuilder containerBuilder = new(); _ = containerBuilder.RegisterModule(); containerBuilder.Populate(services);