How to fix "You cannot use the HttpContextLifecycle outside of a web request. Try the HybridLifecycle instead"?

Vote:
 

I have a hard time figuring out the reason for the following StructureMap error. 

    MyComputerName 2022-10-18 19:00:00,854 [119] ERROR EPiServer.DataAbstraction.ScheduledJob: Failed to create job instance for 'Generate search engine sitemaps' with jobId = '90c6cae4-9939-495c-ae61-40d9768e98b5'
    EPiServer.ServiceLocation.ActivationException: Activation error occurred while trying to get instance of type SitemapXmlGeneratorFactory, key "" ---> StructureMap.StructureMapException: You cannot use the HttpContextLifecycle outside of a web request. Try the HybridLifecycle instead.
    1.) new UrlLocalizationService(*Default of ICustomerNameLocalizationService*, *Default of ISiteDefinitionResolver*, *Default of IPageRouteHelper*, *Default of ICurrentCultureResolver*, *Default of IContentUrlResolver*, *Default of IUrlResolver*, *Default of IContentLanguageSettingsHandler*)
    2.) CustomerName.EPiServer.Core.Services.UrlLocalizationService.UrlLocalizationService
    3.) Instance of CustomerName.EPiServer.Core.Services.UrlLocalizationService.IUrlLocalizationService (CustomerName.EPiServer.Core.Services.UrlLocalizationService.UrlLocalizationService)
    4.) new CustomerNameSitemapXmlGenerator(*Default of ISitemapRepository*, *Default of IContentRepository*, *Default of UrlResolver*, *Default of ISiteDefinitionRepository*, *Default of ILanguageBranchRepository*, *Default of IContentFilter*, *Default of IUrlLocalizationService*)
    5.) CustomerName.EPiServer.Web.Helpers.Sitemap.CustomerNameSitemapXmlGenerator
    6.) Instance of CustomerName.EPiServer.Web.Helpers.Sitemap.ICustomerNameSitemapXmlGenerator (CustomerName.EPiServer.Web.Helpers.Sitemap.CustomerNameSitemapXmlGenerator)
    7.) All registered children for IEnumerable<ICustomerNameSitemapXmlGenerator>
    8.) Instance of IEnumerable<ICustomerNameSitemapXmlGenerator>
    9.) new CustomerNameSitemapXmlGeneratorFactory(*Default of IEnumerable<ICustomerNameSitemapXmlGenerator>*)
    10.) CustomerName.EPiServer.Web.Helpers.Sitemap.CustomerNameSitemapXmlGeneratorFactory
    11.) Instance of Geta.SEO.Sitemaps.Utils.SitemapXmlGeneratorFactory (CustomerName.EPiServer.Web.Helpers.Sitemap.CustomerNameSitemapXmlGeneratorFactory)
    12.) Container.GetInstance(Geta.SEO.Sitemaps.Utils.SitemapXmlGeneratorFactory)
    
       bei lambda_method(Closure , IBuildSession , IContext )
       bei StructureMap.Building.BuildPlan.Build(IBuildSession session, IContext context)
       bei StructureMap.Pipeline.LazyLifecycleObject`1.CreateValue()
       bei StructureMap.SessionCache.GetObject(Type pluginType, Instance instance, ILifecycle lifecycle)
       bei StructureMap.SessionCache.GetDefault(Type pluginType, IPipelineGraph pipelineGraph)
       bei StructureMap.Container.GetInstance(Type pluginType)
       bei EPiServer.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
       --- Ende der internen Ausnahmestapelüberwachung ---
       bei EPiServer.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
       bei EPiServer.ServiceLocation.ServiceLocatorImplBase.GetInstance[TService]()
       bei Geta.SEO.Sitemaps.SitemapCreateJob..ctor()

Some background:

We're using Episerver/Optimizely 11 and the geta-sitemap plugin to generate the sitemaps. I needed to provide a custom SitemapXmlGenerator to provide a custom logic for the x-default url. Everything is working if i trigger sitemaps from CMS (so from web via Admin/ Generate search engine sitemaps). If the generation is triggered from geta scheduled job, StructureMap cannot create instances of the used types.

I can provide more context(classes, DependencyInjectionRegistry, InitializationModule) if necessary. What can be the reason of this error, how to tackle it down?

 

#290246
Edited, Oct 18, 2022 18:03
Vote:
 

Scheduled jobs runs without an http context, just like the error message says. This is however not true when you run the job manually. To solve this, you cannot rely on an http context and therefor not have scoped services in a scheduled job.

#290248
Oct 18, 2022 19:04
Tim Schmelter - Oct 18, 2022 21:10
Thanks. So does it mean that i could use HybridHttpOrThreadLocalScoped instead of HttpContextScoped for the types that i need in both?
Johan Petersson - Oct 18, 2022 21:15
I would give it a try. I don't know StructureMap by heart, but the error message says that hybrid should work.
Tim Schmelter - Oct 19, 2022 7:16
Great, it worked to use HybridHttpOrThreadLocalScoped. Those classes don't need HttpContext, at least not the code that is also used by the job. But maybe this shows that they are doing too much. Thanks again.
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.