I'm trying to use Umbraco context methods in my service, which doesn't have any context at the moment, I have some DI working, but would like access to Umbraco.Content and Umbraco.Media, both of which I don't have access to as I don't have any Umbraco context as the services don't inherit any Umbraco magic.
At the moment my Service looks something like this:
But essentially you can use the IUmbracoContextFactory you've injecting in your example to get a reference to the UmbracoContext by calling it's EnsureUmbacoContext() method:
eg
using (UmbracoContextReference umbracoContextReference = _context.EnsureUmbracoContext()){
//the UmbracoContextReference provides access to the UmbracoContext eg:
// umbracoContextReference.UmbracoContext
//and therefore also the ContentCache
IPublishedContentCache contentCache = umbracoContextReference.UmbracoContext.ContentCache;
}
Inherit UmbracoContext in service
Hi all,
I'm trying to use Umbraco context methods in my service, which doesn't have any context at the moment, I have some DI working, but would like access to Umbraco.Content and Umbraco.Media, both of which I don't have access to as I don't have any Umbraco context as the services don't inherit any Umbraco magic.
At the moment my Service looks something like this:
Is there anyway i can still inherit the interface, but use umbraco context in this service?
Thanks, Lewis
(I'm sure this is just General C# stuff, not you never know!)
Hi Lewis
Yes, it is possible; have a read of this work in progress documentation.. there are some good examples that outline the options:
https://github.com/umbraco/UmbracoDocs/blob/887038c595e1fb492104f3e50db3749b807d923b/Implementation/Services/index.md
But essentially you can use the IUmbracoContextFactory you've injecting in your example to get a reference to the UmbracoContext by calling it's EnsureUmbacoContext() method:
eg
regards
Marc
is working on a reply...