Issue with Scheduler in Umbraco
# help-with-umbraco
d
Created a scheduler as per the documentation, as it's the implementation of Ihosted sevice we can't consume scoped service from the scheduler as DI, so using IServiceProvider to create the scope and try to consume , but the issue is within the required service , we are using Umbraco .Core services and throwing an error from the lib, as below. System.InvalidOperationException: 'Wasn't able to get an UmbracoContext' at Umbraco.Extensions.UmbracoContextAccessorExtensions.GetRequiredUmbracoContext(IUmbracoContextAccessor umbracoContextAccessor) at Umbraco.Cms.Infrastructure.DependencyInjection.UmbracoBuilderExtensions.<>c.b__0_11(IServiceProvider factory) at Did anyone faced this issue before, or any guidance to resolve this would be appreciated.
j
When you need to access an Umbraco Context oustide of a HTTP Request, you need to use the IUmbracoContextFactory to get it. Take a look at this example: https://docs.umbraco.com/umbraco-cms/implementation/services#accessing-published-content-outside-of-a-http-request
The key bit is:
Copy code
csharp
private readonly IUmbracoContextFactory _umbracoContextFactory;

public HandleUnPublishingHandler(IUmbracoContextFactory umbracoContextFactory)
{
    _umbracoContextFactory = umbracoContextFactory;
}
followed by
Copy code
csharp
using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext())
  {
    // your code that needs a context here
  }