umbraco / Umbraco-CMS

Umbraco is a free and open source .NET content management system helping you deliver delightful digital experiences.
https://umbraco.com
MIT License
4.36k stars 2.64k forks source link

Fix unguarded calls to ServiceDescriptor.ImplementationType for keyed services #16604

Closed xdjoshuaaz closed 1 week ago

xdjoshuaaz commented 1 week ago

Prerequisites

No known existing issue that I could find.

Description

This PR updates the calls to ServiceDescriptor.ImplementationType by checking IsKeyedService first.

Currently, if you register a keyed service before UmbracoBuilder does its work, you'll get an exception like below (running the ComponentRuntimeTests.Start_And_Stop_Umbraco_With_Components_Enabled test):

System.InvalidOperationException : This service descriptor is keyed. Your service provider may not support keyed services.
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
   at Microsoft.Extensions.DependencyInjection.ServiceDescriptor.ThrowKeyedDescriptor()
   at Microsoft.Extensions.DependencyInjection.ServiceDescriptor.get_ImplementationType()
   at Umbraco.Cms.Api.Common.DependencyInjection.UmbracoBuilderAuthExtensions.<>c.<AddUmbracoOpenIddict>b__0_0(ServiceDescriptor x) in F:\repos\umbraco\Umbraco-CMS\src\Umbraco.Cms.Api.Common\DependencyInjection\UmbracoBuilderAuthExtensions.cs:line 20
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at Umbraco.Cms.Api.Common.DependencyInjection.UmbracoBuilderAuthExtensions.AddUmbracoOpenIddict(IUmbracoBuilder builder) in F:\repos\umbraco\Umbraco-CMS\src\Umbraco.Cms.Api.Common\DependencyInjection\UmbracoBuilderAuthExtensions.cs:line 20
   at Umbraco.Cms.Api.Management.DependencyInjection.BackOfficeAuthBuilderExtensions.AddBackOfficeAuthentication(IUmbracoBuilder builder) in F:\repos\umbraco\Umbraco-CMS\src\Umbraco.Cms.Api.Management\DependencyInjection\BackOfficeAuthBuilderExtensions.cs:line 20
   at Umbraco.Cms.Tests.Integration.Testing.UmbracoIntegrationTest.ConfigureServices(IServiceCollection services) in F:\repos\umbraco\Umbraco-CMS\tests\Umbraco.Tests.Integration\Testing\UmbracoIntegrationTest.cs:line 156
   at Umbraco.Cms.Tests.Integration.Testing.UmbracoIntegrationTest.<CreateHostBuilder>b__27_1(HostBuilderContext _, IServiceCollection services) in F:\repos\umbraco\Umbraco-CMS\tests\Umbraco.Tests.Integration\Testing\UmbracoIntegrationTest.cs:line 112
   at Microsoft.Extensions.Hosting.HostBuilder.InitializeServiceProvider()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at Umbraco.Cms.Web.Common.Hosting.UmbracoHostBuilderDecorator.Build() in F:\repos\umbraco\Umbraco-CMS\src\Umbraco.Web.Common\Hosting\UmbracoHostBuilderDecorator.cs:line 63
   at Umbraco.Cms.Tests.Integration.Testing.UmbracoIntegrationTest.Setup() in F:\repos\umbraco\Umbraco-CMS\tests\Umbraco.Tests.Integration\Testing\UmbracoIntegrationTest.cs:line 76
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
--TearDown
   at Umbraco.Cms.Tests.Integration.Testing.UmbracoIntegrationTest.TearDownAsync() in F:\repos\umbraco\Umbraco-CMS\tests\Umbraco.Tests.Integration\Testing\UmbracoIntegrationTest.cs:line 87
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

To test & verify against this, I've registered a keyed service in the UmbracoIntegrationTest base class before UmbracoBuilder is created & used.

github-actions[bot] commented 1 week ago

Hi there @xdjoshuaaz, thank you for this contribution! πŸ‘

While we wait for one of the Core Collaborators team to have a look at your work, we wanted to let you know about that we have a checklist for some of the things we will consider during review:

Don't worry if you got something wrong. We like to think of a pull request as the start of a conversation, we're happy to provide guidance on improving your contribution.

If you realize that you might want to make some changes then you can do that by adding new commits to the branch you created for this work and pushing new commits. They should then automatically show up as updates to this pull request.

Thanks, from your friendly Umbraco GitHub bot πŸ€– πŸ™‚

georgebid commented 1 week ago

Hey @xdjoshuaaz, thanks for your PR πŸ˜„ someone on the core collaborators team will review this soon!

Matthew-Wise commented 1 week ago

Hi @xdjoshuaaz, thanks for this PR it works great :) Nice to have some extra guards in place