Skip to content

Commit

Permalink
Merge branch 'hotfix/3.3.1' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Aug 21, 2023
2 parents 70cb1b7 + 42cb9ac commit 71a9c78
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 9 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [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
Expand Down Expand Up @@ -583,7 +589,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
Expand Down
2 changes: 1 addition & 1 deletion X10D.DSharpPlus/X10D.DSharpPlus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageTags>dotnet extension-methods</PackageTags>
<PackageReleaseNotes>$([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/../CHANGELOG.md"))</PackageReleaseNotes>
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionPrefix>3.3.1</VersionPrefix>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
2 changes: 1 addition & 1 deletion X10D.Hosting/X10D.Hosting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageTags>dotnet extension-methods</PackageTags>
<PackageReleaseNotes>$([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/../CHANGELOG.md"))</PackageReleaseNotes>
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionPrefix>3.3.1</VersionPrefix>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ public static IServiceCollection AddHostedSingleton<TService>(this IServiceColle
/// <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>
Expand Down
53 changes: 52 additions & 1 deletion X10D.Tests/src/Hosting/ServiceCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ public void AddHostedSingleton_ShouldRegisterServiceAsSingletonAndAsHostedServic
});
}

[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()
{
Expand All @@ -50,8 +71,38 @@ public void AddHostedSingleton_ShouldRegisterServiceTypeAsSingletonAndAsHostedSe
});
}

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;
Expand Down
2 changes: 1 addition & 1 deletion X10D.Unity/X10D.Unity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageTags>dotnet extension-methods</PackageTags>
<PackageReleaseNotes>$([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/../CHANGELOG.md"))</PackageReleaseNotes>
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionPrefix>3.3.1</VersionPrefix>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
2 changes: 1 addition & 1 deletion X10D/X10D.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>$([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/../CHANGELOG.md"))</PackageReleaseNotes>
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
<VersionPrefix>3.3.0</VersionPrefix>
<VersionPrefix>3.3.1</VersionPrefix>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down

0 comments on commit 71a9c78

Please sign in to comment.