When I try to call SaveAndPublish() on the contentService, to save changes to the content node I get:
System.AggregateException: 'One or more errors occurred. (Wasn't able to get an UmbracoContext)'
I thought it might have been because I wasnt creating a "scope", so I used the scopeProvider as specified in the link above. But instead of getthing this error when i call SaveAndPublish, I get it when the scope is disposed... which makes sense.
So what is happening here, are we not able to use the content service in hosted services?
From the inner stack trace, it looks like its something to do with notification handlers:
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.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
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.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
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.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.Publish[TNotification](TNotification notification)
at Umbraco.Cms.Core.Events.ScopedNotificationPublisher.ScopeExit(Boolean completed)
at Umbraco.Cms.Infrastructure.Scoping.Scope.TryFinally(Action[] actions)
I resolved this by wrapping the code that performs any contentService calls with this:
using (UmbracoContextReference umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext())
{
// call the content service here
}
Feels like a bit of a hack to me, but I guess it makes sense.
Surprised the RecurringHostedServiceBase base class doesnt take care of this for us. Also surprised it is not mentioned in the documentation linked above, if it is the correct thing to do.
RecurringHostedService throwing exception "Wasn't able to get an UmbracoContext"
Hi All,
I am trying to create a hosted service that runs periodically. What it does is:
I followed this article very closely: https://docs.umbraco.com/umbraco-cms/10.latest/reference/scheduling
So I dont think I have any obvious mistakes
When I try to call SaveAndPublish() on the contentService, to save changes to the content node I get:
I thought it might have been because I wasnt creating a "scope", so I used the scopeProvider as specified in the link above. But instead of getthing this error when i call SaveAndPublish, I get it when the scope is disposed... which makes sense.
So what is happening here, are we not able to use the content service in hosted services?
From the inner stack trace, it looks like its something to do with notification handlers:
I resolved this by wrapping the code that performs any contentService calls with this:
Feels like a bit of a hack to me, but I guess it makes sense. Surprised the RecurringHostedServiceBase base class doesnt take care of this for us. Also surprised it is not mentioned in the documentation linked above, if it is the correct thing to do.
is working on a reply...