Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal sealed class ConstantCallSite : ServiceCallSite
private readonly Type _serviceType;
internal object? DefaultValue => Value;

public ConstantCallSite(Type serviceType, object? defaultValue, object? serviceKey = null) : base(ResultCache.None(serviceType), serviceKey)
public ConstantCallSite(Type serviceType, object? defaultValue, object? serviceKey = null) : base(ResultCache.None(serviceType, serviceKey), serviceKey)
{
_serviceType = serviceType ?? throw new ArgumentNullException(nameof(serviceType));
if (defaultValue != null && !serviceType.IsInstanceOfType(defaultValue))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace Microsoft.Extensions.DependencyInjection.ServiceLookup
{
internal struct ResultCache
{
public static ResultCache None(Type serviceType)
public static ResultCache None(Type serviceType, object? serviceKey = null)
{
var cacheKey = new ServiceCacheKey(ServiceIdentifier.FromServiceType(serviceType), 0);
var cacheKey = new ServiceCacheKey(new ServiceIdentifier(serviceKey, serviceType), 0);
return new ResultCache(CallSiteResultCacheLocation.None, cacheKey);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,35 @@ public void ScopeValidation_ShouldBeAbleToDistingushGenericCollections_WhenGetSe
Assert.IsType<Baz2>(actual.Last());
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void BuildServiceProvider_ValidateOnBuild_DoesNotThrow_WhenKeyedSingletonInstanceAndScopedShareServiceType(bool registerScopedFirst)
{
// Arrange
var serviceCollection = new ServiceCollection();
if (registerScopedFirst)
{
serviceCollection.AddScoped<IKeyedScopedCollisionService, KeyedScopedCollisionScopedService>();
serviceCollection.AddKeyedSingleton<IKeyedScopedCollisionService>("Key", new KeyedScopedCollisionSingletonService());
}
else
{
serviceCollection.AddKeyedSingleton<IKeyedScopedCollisionService>("Key", new KeyedScopedCollisionSingletonService());
serviceCollection.AddScoped<IKeyedScopedCollisionService, KeyedScopedCollisionScopedService>();
}

serviceCollection.AddSingleton<KeyedScopedCollisionOtherSingleton>();

// Act + Assert
using var serviceProvider = serviceCollection.BuildServiceProvider(new ServiceProviderOptions
{
ValidateScopes = true,
ValidateOnBuild = true
});
Assert.NotNull(serviceProvider);
}

[Fact]
public void GetService_DoesNotThrow_WhenScopeFactoryIsInjectedIntoSingleton()
{
Expand Down Expand Up @@ -586,5 +615,27 @@ public Boo(IServiceScopeFactory scopeFactory)
{
}
}

private interface IKeyedScopedCollisionService
{
}

private class KeyedScopedCollisionSingletonService : IKeyedScopedCollisionService
{
}

private class KeyedScopedCollisionScopedService : IKeyedScopedCollisionService
{
public KeyedScopedCollisionScopedService([FromKeyedServices("Key")] IKeyedScopedCollisionService keyedService)
{
}
}

private class KeyedScopedCollisionOtherSingleton
{
public KeyedScopedCollisionOtherSingleton([FromKeyedServices("Key")] IKeyedScopedCollisionService keyedService)
{
}
}
}
}
Loading