diff --git a/CHANGELOG.md b/CHANGELOG.md index 77c9538..27f75f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - X10D: Added `TextWriter.WriteLineNoAlloc(long[, ReadOnlySpan[, IFormatProvider]])`. - X10D: Added `TextWriter.WriteLineNoAlloc(ulong[, ReadOnlySpan[, IFormatProvider]])`. - X10D: Added `string.ConcatIf`. +- X10D.Hosting: Added support for service/implementation registration with `AddHostedSingleton`. - X10D.Unity: Added `RaycastHit.GetComponent` and `RaycastHit.TryGetComponent`. - X10D.Unity: Added `DebugUtility.DrawFunction`, and `DebugUtility.DrawUnjoinedPolyhedron` on which it relies. diff --git a/X10D.Hosting/src/DependencyInjection/ServiceCollectionExtensions.cs b/X10D.Hosting/src/DependencyInjection/ServiceCollectionExtensions.cs index 9a0bafe..8df216c 100644 --- a/X10D.Hosting/src/DependencyInjection/ServiceCollectionExtensions.cs +++ b/X10D.Hosting/src/DependencyInjection/ServiceCollectionExtensions.cs @@ -21,6 +21,21 @@ public static class ServiceCollectionExtensions return services.AddSingleton(provider => provider.GetRequiredService()); } + /// + /// Adds an registration for the given type, while simultaneously adding it as a singleton. + /// + /// The to add the service to. + /// The type of the service to add. + /// The type of the implementation to use. + /// A reference to this instance after the operation has completed. + public static IServiceCollection AddHostedSingleton(this IServiceCollection services) + where TService : class, IHostedService + where TImplementation : class, TService + { + services.AddSingleton(); + return services.AddSingleton(provider => provider.GetRequiredService()); + } + /// /// Adds an registration for the given type, while simultaneously adding it as a singleton. /// @@ -32,4 +47,19 @@ public static class ServiceCollectionExtensions services.AddSingleton(type); return services.AddSingleton(provider => (IHostedService)provider.GetRequiredService(type)); } + + /// + /// Adds an registration for the given type, while simultaneously adding it as a singleton. + /// + /// The to add the service to. + /// The type of the service to register. + /// The type of the implementation to use. + /// A reference to this instance after the operation has completed. + public static IServiceCollection AddHostedSingleton(this IServiceCollection services, + Type serviceType, + Type implementationType) + { + services.AddSingleton(serviceType, implementationType); + return services.AddSingleton(provider => (IHostedService)provider.GetRequiredService(serviceType)); + } }