Skip to content

Commit 64eecb5

Browse files
authored
Add info about IPublishedContentQuery in background task
1 parent 29db9eb commit 64eecb5

File tree

1 file changed

+13
-0
lines changed
  • 15/umbraco-cms/implementation/services

1 file changed

+13
-0
lines changed

15/umbraco-cms/implementation/services/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,19 @@ public class HandleUnPublishingHandler : INotificationHandler<ContentUnpublished
223223
}
224224
```
225225

226+
#### Accessing the Published Content Cache via IPublishedContentQuery
227+
{% hint style="warning" %}
228+
Sometimes you may want to fetch multiple content items from ids, where `UmbracoContext.Content` only allow to fetch a single content item by id.
229+
However in a background task accessing content cache from injected `IPublishedContentQuery` or `IPublishedContentQueryAccessor` won't work as these rely on `HttpContext`.
230+
Innstead the following can be used.
231+
{% endhint %}
232+
233+
```csharp
234+
using UmbracoContextReference _ = _umbracoContextFactory.EnsureUmbracoContext();
235+
using IServiceScope serviceScope = _serviceProvider.CreateScope();
236+
IPublishedContentQuery query = serviceScope.ServiceProvider.GetRequiredService<IPublishedContentQuery>();
237+
```
238+
226239
#### Accessing the Published Content Cache from a Content Finder / UrlProvider
227240

228241
Inside a ContentFinder access to the content cache is possible by injecting `IUmbracoContextAccessor` into the constructor and provided via the PublishedRequest object:

0 commit comments

Comments
 (0)