DI Issues with DocumentEventAuthorizer

We have Umbraco v15.1.2 running fine for a large project I am working on. When I try to upgrade Umbraco to v15.2+ or v15.3, I get the exception below, running the project.

The issue seems to be that it cannot construct DocumentEventAuthorizer as a singleton, because IAuthorizationService (which DocumentEventAuthorizer’s base class injects) is scoped and not a singleton.

I have tried running a fresh Umbraco v15.3 install, and it doesn’t throw this exception, despite DocumentEventAuthorizer being a singleton and IAuthorizationService being scoped in that implementation as well.

System.AggregateException
  HResult=0x80131500
  Message=Some services are not able to be constructed
  Source=Microsoft.Extensions.DependencyInjection
  StackTrace:
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
   at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build()
   at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
   at ClientProject.Program.<Main>d__0.MoveNext() in C:\projects\Clients\ClientProject.Web\Program.cs:line 23
   at ClientProject.Web.Program.<Main>(String[] args)

  This exception was originally thrown at this call stack:
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.VisitCallSite(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.CallSiteValidatorState)
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.VisitConstructor(Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.CallSiteValidatorState)
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSite(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite, TArgument)
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.VisitCallSite(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceCallSite, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.CallSiteValidatorState)
    Microsoft.Extensions.DependencyInjection.ServiceProvider.ValidateService(Microsoft.Extensions.DependencyInjection.ServiceDescriptor)

Inner Exception 1:
InvalidOperationException: Error while validating the service descriptor 'ServiceType: Umbraco.Cms.Api.Management.ServerEvents.Authorizers.DocumentEventAuthorizer Lifetime: Singleton ImplementationType: Umbraco.Cms.Api.Management.ServerEvents.Authorizers.DocumentEventAuthorizer': Cannot consume scoped service 'Microsoft.AspNetCore.Authorization.IAuthorizationService' from singleton 'Umbraco.Cms.Api.Management.ServerEvents.Authorizers.DocumentEventAuthorizer'.

Inner Exception 2:
InvalidOperationException: Cannot consume scoped service 'Microsoft.AspNetCore.Authorization.IAuthorizationService' from singleton 'Umbraco.Cms.Api.Management.ServerEvents.Authorizers.DocumentEventAuthorizer'.

Steps to reproduce:
Modify the Startup.cs, adding the following:

 var builder = WebApplication.CreateBuilder(args);
 builder.Host.UseDefaultServiceProvider(
     (context, options) =>
     {
         options.ValidateOnBuild = true;
         options.ValidateScopes = true;
     });

Alternatively, set "ASPNETCORE_ENVIRONMENT": "Development" in the launchSettings.json.

Since there has been no response to this, I have created the following Issue on Github: DI Issues with DocumentEventAuthorizer · Issue #19181 · umbraco/Umbraco-CMS · GitHub

1 Like