Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Heather Floyd 610 posts 1033 karma points MVP 6x c-trib
    Jul 08, 2023 @ 04:24
    Heather Floyd
    1

    Dependency Injection - when one service relies on another and blows up the boot

    I'm struggling with a weird issue that is causing the dreaded "HTTP Error 500.30 - ASP.NET Core app failed to start" message. Checking the Windows Event Viewer - the specific error is "System.InvalidOperationException: Wasn't able to get an UmbracoContext"

    Description: The process was terminated due to an unhandled exception.
    Exception Info: System.InvalidOperationException: Wasn't able to get an UmbracoContext
       at Umbraco.Extensions.UmbracoContextAccessorExtensions.GetRequiredUmbracoContext(IUmbracoContextAccessor umbracoContextAccessor)
       at Umbraco.Cms.Infrastructure.DependencyInjection.UmbracoBuilderExtensions.<>c.<AddCoreInitialServices>b__0_11(IServiceProvider factory)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitIEnumerable(IEnumerableCallSite enumerableCallSite, RuntimeResolverContext context)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
       at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
       at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
       at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
       at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetServices[T](IServiceProvider provider)
       at Umbraco.Cms.Core.Events.NotificationHandlerWrapperImpl`1.Handle(INotification notification, ServiceFactory serviceFactory, Action`2 publish)
       at Umbraco.Cms.Core.Events.EventAggregator.PublishNotification(INotification notification)
       at Umbraco.Cms.Core.Events.EventAggregator.PublishAsync[TNotification](TNotification notification, CancellationToken cancellationToken)
       at Umbraco.Cms.Infrastructure.Runtime.CoreRuntime.StartAsync(CancellationToken cancellationToken, Boolean isRestarting)
       at Umbraco.Cms.Infrastructure.Runtime.CoreRuntime.StartAsync(CancellationToken cancellationToken)
       at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
       at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
       at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
       at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
       at UmbracoProject.Program.Main(String[] args) in C:\Users\Heather\WEBS\Clients\LOYD\American Regent\sourcecode-10\src\UmbracoProject\Program.cs:line 6
    

    What seems to be causing the error is a DI service I am adding via the Startup.cs which relies on another injected service.

    Here is a simplified example of what I mean:

    Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
            ...
            services.AddScoped<SiteHelperService>();
            ...
            services.AddScoped<IPageRenderer, BaseModelPageRenderer>();
            ...
    }
    

    BaseModelPageRenderer relies upon SiteHelperService:

    BaseModelPageRenderer.cs

    public BaseModelPageRenderer(
                ILogger<BaseModelPageRenderer> logger,
                IUmbracoContextAccessor umbracoContextAccessor,
                IVariationContextAccessor variationContextAccessor,
                IHttpContextAccessor httpContextAccessor,
                SiteHelperService siteHelperService)
            {...}
    

    SiteHelperService.cs

    public SiteHelperService(
                ILogger<SiteHelperService> logger,
                IConfiguration AppSettingsConfig,
                IHttpContextAccessor ContextAccessor,
                IUmbracoContextAccessor UmbracoContextAccessor,
                IFileService fileService,
                IContentTypeService ContentTypeService)
            {...}
    

    If I comment-out the line in 'Startup':

    //services.AddScoped<IPageRenderer, BaseModelPageRenderer>();
    

    Then the site will load just fine.

    OR if I leave that in, and instead comment out this bit in 'BaseModelPageRenderer':

    public BaseModelPageRenderer(
                ILogger<BaseModelPageRenderer> logger,
                IUmbracoContextAccessor umbracoContextAccessor,
                IVariationContextAccessor variationContextAccessor,
                IHttpContextAccessor httpContextAccessor
                //SiteHelperService siteHelperService)
            {...}
    

    Then the site will load just fine.

    Since loading the SiteHelperService via the Startup file works fine, I just don't understand why it causes an issue when loaded into the other service.

  • Anders Bjerner 487 posts 2996 karma points MVP 8x admin c-trib
    Jul 08, 2023 @ 17:31
    Anders Bjerner
    0

    Hi Heather

    Generally this should be a problem. I tried setting up similar services, and have no problem booting up Umbraco.

    Couldn't it be something that you're doing in one of the constructors?

  • Heather Floyd 610 posts 1033 karma points MVP 6x c-trib
    Jul 14, 2023 @ 20:03
    Heather Floyd
    0

    Hi Anders!

    My constructors are pretty straightforward - basically storing those injected services to local fields...

    What I did notice, is that if I remove the interface IPageRenderer from BaseModelPageRenderer, the startup error doesn't occur.

    ex:

    public class BaseModelPageRenderer : IPageRenderer

    to:

    public class BaseModelPageRenderer

    so it seems to be something having to do with the IPageRenderer...

    This is all used in conjunction with a Full Text Search Indexer, and the new class is supposed to provide custom page rendering for the indexer to use, rather than an HTTP request. I will likely need to dig into that codebase and figure out where/when that is getting loaded, and perhaps causing the missing Context issue.

    Thanks for testing and replying!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies