Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Lewis Smith 211 posts 620 karma points c-trib
    Aug 19, 2019 @ 08:15
    Lewis Smith
    0

    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:

    public class GeneralService : IGeneralService
    {
        private readonly ISettingsService _settingsService;
        private readonly IContentService _contentService;
        private readonly IEntityService _entityService;
        private readonly IMediaService _mediaService;
        private readonly IUmbracoContextFactory _context;
        private readonly IUrlProvider _provider;
    
        public GeneralService(IContentService contentService, ISettingsService settingsService, IEntityService entityService, IMediaService mediaService, IUmbracoContextFactory context, IUrlProvider urlProvider)
        {
            _contentService = contentService;
            _settingsService = settingsService;
            _entityService = entityService;
            _mediaService = mediaService;
            _context = context;
            _provider = urlProvider;
            }
        }
        //All methods are here
    }
    

    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!)

  • Marc Goodson 2157 posts 14434 karma points MVP 9x c-trib
    Aug 21, 2019 @ 08:49
    Marc Goodson
    1

    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

      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;
    
    }
    

    regards

    Marc

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

    Continue discussion

Please Sign in or register to post replies