Skip to content

TryAddSingleton custom AuthorizationPolicyProvider #36014

Closed
@janek9971

Description

@janek9971

Describe the bug

Exception at startup webApi class is thrown, because of unexcpected behaviour when use TryAddSingletion custom AuthorizationPolicyProvider.

To Reproduce

Steps to reproduce the behavior:

  1. Using newest version of .net core 2.2 of package Microsoft.Extensions.DependencyInjection,
    class ServiceCollectionDescriptorExtensions
  2. Running
services.TryAddSingleton<IAuthorizationPolicyProvider, AdGroupsAuthorizationPolicyProvider>();

where AdGroupsAuthorizationPolicyProvider derive from IAuthorizationPolicyProvider,
so it is custom AuthorizationPolicyProvider.

  1. This give me an error in Startup.cs, when calling
app.UseMvc();

System.InvalidOperationException: 'The AuthorizationPolicy named: 'AdGroupsic-asset-validator,ic-asset-editor' was not found.'

Expected behavior

I copied that code below from ServiceCollectionDescriptorExtensions, to my static class and debugged

  public static void TryAddSingleton<TService, TImplementation>(this IServiceCollection collection)
            where TService : class
            where TImplementation : class, TService
        {
            if (collection == null)
                throw new ArgumentNullException(nameof(collection));
            collection.TryAddSingleton(typeof(TService), typeof(TImplementation));
        }
        public static void TryAddSingleton(
            this IServiceCollection collection,
            Type service,
            Type implementationType)
        {
            if (collection == null)
                throw new ArgumentNullException(nameof(collection));
            if (service == (Type)null)
                throw new ArgumentNullException(nameof(service));
            if (implementationType == (Type)null)
                throw new ArgumentNullException(nameof(implementationType));
            ServiceDescriptor descriptor = ServiceDescriptor.Singleton(service, implementationType);
            collection.TryAdd(descriptor);
        }
        public static void TryAdd(this IServiceCollection collection, ServiceDescriptor descriptor)
        {
            if (collection == null)
                throw new ArgumentNullException(nameof(collection));
            if (descriptor == null)
                throw new ArgumentNullException(nameof(descriptor));
            if (collection.Any<ServiceDescriptor>((Func<ServiceDescriptor, bool>)(d => d.ServiceType == descriptor.ServiceType)))
                return;
            collection.Add(descriptor);
        }

and the reason why error is throw is beacuse, when program reach line

            if (collection.Any<ServiceDescriptor>((Func<ServiceDescriptor, bool>)(d => d.ServiceType == descriptor.ServiceType)))

it goes to return and not add the service, cause probably the IAuthorizationPolicy default is added earlier and I can't add again the same serviceType with difrrent implementation.
For me it should inform me about that, not silently just return and then I have no idea at app.UseMvc(), why exception is throwed.

Screenshots

image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions