-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check if service is keyed before accessing ImplementationType
- Loading branch information
1 parent
29ee873
commit 66ea497
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...AspNetCoreTests/Bugs/Bug_395_keyed_service_closed_generic_interface_registration_check.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Collections.Generic; | ||
using Lamar.Microsoft.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace Lamar.AspNetCoreTests.Bugs; | ||
|
||
public class Bug_395_keyed_service_closed_generic_interface_registration_check | ||
{ | ||
class ClassA {} | ||
|
||
interface IGenericService<T> {} | ||
|
||
class ServiceA : IGenericService<ClassA> {} | ||
|
||
[Fact] | ||
public void do_not_blow_up() | ||
{ | ||
var serviceName = "MyServiceName"; | ||
|
||
var builder = new HostBuilder() | ||
.ConfigureServices((context, services) => | ||
{ | ||
// This is needed because of https://github.com/aspnet/Logging/issues/691 | ||
services.AddSingleton<ILoggerFactory, LoggerFactory>(sp => | ||
new LoggerFactory( | ||
sp.GetRequiredService<IEnumerable<ILoggerProvider>>(), | ||
sp.GetRequiredService<IOptionsMonitor<LoggerFilterOptions>>() | ||
) | ||
); | ||
services.AddKeyedTransient<IGenericService<ClassA>, ServiceA>(serviceName); | ||
}) | ||
.UseLamar(); | ||
|
||
using (var host = builder.Start()) | ||
{ | ||
var container = host.Services.ShouldBeOfType<Container>(); | ||
|
||
container.GetInstance<IGenericService<ClassA>>() | ||
.ShouldNotBeNull(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters