Copied to clipboard

Flag this post as spam?

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


  • Aleksander 45 posts 205 karma points
    Mar 06, 2019 @ 14:39
    Aleksander
    0

    Content services during boot / composing

    Hi.

    I'm trying to extend Umbraco 8 and i need to access the Services, Contentype Service, memberservice ETC during startup.

    Usually i would use the old OnApplicationStarted and Application context like so:

        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ApplicationContext.Current.Services.ContentService.GetById(someId);
        }
    

    Trying to do the same in umbraco 8 i've hit a wall though. I've come up witht the following:

    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class Startup : IUserComposer
    {
    
        void IComposer.Compose(Composition composition)
        {
    
            var x = Current.Services.ContentTypeService.GetAllContentTypeAliases();
    

    The issue is that any attempts to get any services results in the error that Factory is not set.

    Does anyone know a way around this ? Am i doing it completely wrong?

    Thanks alot

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Mar 06, 2019 @ 14:56
    Sebastiaan Janssen
    0

    Here's some documentation that shows you how to do it: https://our.umbraco.com/Documentation/Implementation/Composing/#example---creating-a-component-to-listen-for-contentservicesaving-events

    You're currently doing it in the place where you should be registering your component.

    Your component's Initialize() method is responsible for hooking into the event, you can't do it in the IUserComposer :)

  • Aleksander 45 posts 205 karma points
    Mar 06, 2019 @ 15:07
    Aleksander
    0

    Thank you. It seems like you linked to this article ?

    Anyway after writing this post i changed my code to Subscribe to UmbracoApplicationBase.ApplicationInit.

    In this method i have access to the services. Only issue is it seems this event is called multiple times, so i cannot use it to map a new route in the route table.

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Mar 06, 2019 @ 15:14
    Sebastiaan Janssen
    0

    Ah how silly, I've updated the link to: https://our.umbraco.com/Documentation/Implementation/Composing/#example---creating-a-component-to-listen-for-contentservicesaving-events

    Subscribe to UmbracoApplicationBase.ApplicationInit.

    Nope, please don't. :-)

Please Sign in or register to post replies

Write your reply to:

Draft