Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issues with caching in the wrong slot #52168

Merged
merged 2 commits into from
May 3, 2021
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 @@ -481,15 +481,15 @@ public int GetSlot(ServiceDescriptor descriptor)
{
if (descriptor == _item)
{
return 0;
return Count - 1;
}

if (_items != null)
{
int index = _items.IndexOf(descriptor);
if (index != -1)
{
return index + 1;
return Count - index + 1;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ namespace Microsoft.Extensions.DependencyInjection.Tests
{
internal static class ServiceCollectionContainerBuilderTestExtensions
{
public static ServiceProvider BuildServiceProvider(this IServiceCollection services, ServiceProviderMode mode)
public static ServiceProvider BuildServiceProvider(this IServiceCollection services, ServiceProviderMode mode, ServiceProviderOptions options = null)
{
options ??= ServiceProviderOptions.Default;

if (mode == ServiceProviderMode.Default)
{
return services.BuildServiceProvider();
return services.BuildServiceProvider(options);
}

IServiceProviderEngine engine = mode switch
Expand All @@ -24,7 +26,7 @@ public static ServiceProvider BuildServiceProvider(this IServiceCollection servi
_ => throw new NotSupportedException()
};

return new ServiceProvider(services, engine, ServiceProviderOptions.Default);
return new ServiceProvider(services, engine, options);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.Extensions.DependencyInjection.Specification;

namespace Microsoft.Extensions.DependencyInjection.Tests
{
public class ServiceProviderDefaultContainerTestsWithOptions : DependencyInjectionSpecificationTests
{
protected override IServiceProvider CreateServiceProvider(IServiceCollection collection)
{
try
{
return collection.BuildServiceProvider(ServiceProviderMode.Default, new ServiceProviderOptions
{
ValidateOnBuild = true,
// Too many tests fail because they try to resolve scoped services from the root
// provider
// ValidateScopes = true
});
}
catch (AggregateException)
{
// This is how we "skip" tests that fail on BuildServiceProvider (broken object graphs).
// We care mainly about exercising the non-throwing code path so we fallback to the default BuildServiceProvider
return collection.BuildServiceProvider();
}
}
}
}