-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v8: Reading IPublishedContent in a background task fails #4442
Comments
UmbracoContext is a web based lifetime, it is created based on an HttpContext, just like v7. It is not a singleton but there is a new singleton accessor. If you are using that, and you are not in a web context, you will get a null because it cannot exist there. Just like v7. As for accessing the content cache outside of a web based lifetime, I'm sure that's possible but AFAIK it's simply down to current time constraints that it's not available. I could be wrong but i think that is the case. In the meantime you might have to resort to v7 tactics and fake a context, etc... @zpqrtbnk will know more on the subject and i'll chat to him later to see what the status is about accessing the content cache outside of a web request. That said, the umbraco context accessor will never return an umbraco context that's not in a web request because that is it's lifetime. |
Thanks for the very swift reply Shannon. It makes sense there is no UmbracoContext outside of a web request indeed, I can totally understand that. I'd rather use a more focused dependency like |
I'm closing the issue for now. |
So... Outside of a request, you have to manage a snapshot by yourself.
Here, Making sense? |
Excellent, thank you Stephan (@zpqrtbnk) for your reply. Will look into your suggested approach. Something like this is what I was hoping for would be possible in v8. |
For future readers, core team is discussing a similar issue and possible solutions in #4572 |
Also for future readers: the code in the referenced issue works more reliably and is as follows: // _umbracoContextFactory is an injected IUmbracoContextFactory
using (var reference = _umbracoContextFactory.EnsureUmbracoContext())
{
var content = reference.UmbracoContext.ContentCache.GetById(...)
} |
Hi,
I am trying to figure out some v8 stuff, including how to execute background jobs that want to read some Umbraco data as IPublishedContent. I was happy to see v8 provides an
IUmbracoContextAccessor
and aIPublishedSnapshotAccessor
, either of which would probably give me access to IPublishedContent.However, it seems that both of those will not work properly when used on application boot in an IComponent and I was wondering if this is a bug or expected behavior. If this is expected behavior and I should not be using an IComponent like that, then please guide me in how I should be doing this.
I was checking Umbraco.Web.Scheduling.SchedulerComponent for inspiration but it uses the
IContentService
instead which is not what I would prefer. Surely the PublishedContentCache should also be available outside of a request in one way or another?Especially since Umbraco now contains the
HybridUmbracoContextAccessor
class which is referenced in the WebRuntimeComposer with the explicit comment mentioning situations without HttpContext I was hoping it would "just" work now:Bug summary
UmbracoContext
/IPublishedSnapshot
are bothnull
in an implementation ofIComponent
at itsInitialize()
method.Steps to reproduce
Tested in version
8.0.0-alpha.58.2091
.Execute this code. If I should not be using Composer / Component in this way please forgive me and tell me how it should be done instead :)
Expected result
_contextAccessor.UmbracoContext
and_snapshotAccessor.PublishedSnapshot
are both available and grant access toIPublishedContent
. No exception is thrown.Actual result
_contextAccessor.UmbracoContext
and_snapshotAccessor.PublishedSnapshot
are bothnull
, exception is thrown.It would be great if there would be a single unified way to obtain IPublishedContent, with or without HttpContext / active request / etc. In v7 this was always painful and hacky to make work (EnsureContext shenanigans), I hope v8 can streamline this.
The text was updated successfully, but these errors were encountered: