Merge branch 'main' into release/4.0.0

This commit is contained in:
Oliver Booth 2023-08-21 17:41:05 +01:00
commit a14fe4ca64
Signed by: oliverbooth
GPG Key ID: B89D139977693FED
3 changed files with 64 additions and 5 deletions

View File

@ -35,6 +35,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- X10D: Removed `IEnumerable<T>.ConcatOne` - this functionality already exists with `Append`.
## [3.3.1] - 2023-08-21
### Fixed
- X10D.Hosting: Fixed `AddHostedSingleton` not accepting an interface as the service type.
## [3.3.0] - 2023-08-21
### Added
@ -613,7 +619,8 @@ please [open an issue](https://github.com/oliverbooth/X10D/issues)!
Earlier versions of this package are undocumented and unlisted from package results.
[unreleased]: https://github.com/oliverbooth/X10D/compare/v3.3.0...main
[unreleased]: https://github.com/oliverbooth/X10D/compare/v3.3.1...main
[3.3.1]: https://github.com/oliverbooth/X10D/releases/tag/v3.3.1
[3.3.0]: https://github.com/oliverbooth/X10D/releases/tag/v3.3.0
[3.2.2]: https://github.com/oliverbooth/X10D/releases/tag/v3.2.2
[3.2.0]: https://github.com/oliverbooth/X10D/releases/tag/v3.2.0

View File

@ -29,11 +29,12 @@ public static class ServiceCollectionExtensions
/// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddHostedSingleton<TService, TImplementation>(this IServiceCollection services)
where TService : class, IHostedService
where TImplementation : class, TService
where TService : class
where TImplementation : class, TService, IHostedService
{
services.AddSingleton<TService, TImplementation>();
return services.AddSingleton<IHostedService, TService>(provider => provider.GetRequiredService<TService>());
return services.AddSingleton<IHostedService, TImplementation>(provider =>
(TImplementation)provider.GetRequiredService<TService>());
}
/// <summary>

View File

@ -29,6 +29,27 @@ public class ServiceCollectionTests
});
}
[Test]
public void AddHostedSingleton_ShouldRegisterServiceAsSingletonAndAsHostedService_GivenServiceAndImplTypes()
{
var services = new ServiceCollection();
services.AddHostedSingleton<ITestService, TestService>();
var serviceProvider = services.BuildServiceProvider();
var service = serviceProvider.GetService<ITestService>();
var hostedService = serviceProvider.GetService<IHostedService>();
Assert.Multiple(() =>
{
Assert.That(service, Is.Not.Null);
Assert.That(hostedService, Is.Not.Null);
Assert.IsAssignableFrom<TestService>(service);
Assert.IsAssignableFrom<TestService>(hostedService);
Assert.That(hostedService, Is.SameAs(service));
});
}
[Test]
public void AddHostedSingleton_ShouldRegisterServiceTypeAsSingletonAndAsHostedService()
{
@ -50,8 +71,38 @@ public class ServiceCollectionTests
});
}
private sealed class TestService : IHostedService
[Test]
public void AddHostedSingleton_ShouldRegisterServiceTypeAsSingletonAndAsHostedService_GivenServiceAndImplTypes()
{
var services = new ServiceCollection();
services.AddHostedSingleton(typeof(ITestService), typeof(TestService));
var serviceProvider = services.BuildServiceProvider();
var service = serviceProvider.GetService<ITestService>();
var hostedService = serviceProvider.GetService<IHostedService>();
Assert.Multiple(() =>
{
Assert.That(service, Is.Not.Null);
Assert.That(hostedService, Is.Not.Null);
Assert.IsAssignableFrom<TestService>(service);
Assert.IsAssignableFrom<TestService>(hostedService);
Assert.That(hostedService, Is.SameAs(service));
});
}
private interface ITestService
{
void Foo();
}
private sealed class TestService : ITestService, IHostedService
{
public void Foo()
{
}
public Task StartAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;