Copied to clipboard

Flag this post as spam?

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


  • Tom 711 posts 948 karma points
    Aug 06, 2019 @ 06:26
    Tom
    0

    Umbraco v8 - Composer equivalent to OnApplicationStarted

    Hi team just wondering how we'd do the equivalent in v8? I'm trying to port the react app demo to v8

    I can't seem to find in the documentation the equivalent composition or identifying where in the lifecycle certain things are. In this instance what would the equivalent of OnApplicationStarted be?

     public class UmbracoReactStartup : IApplicationEventHandler
    {
        public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
    
        public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            // Register custom default controller
            DefaultRenderMvcControllerResolver.Current.SetDefaultControllerType(typeof(ReactRoutesController));
        }
    
        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            // This project uses dependency injection
            // Read more about umbraco and dependency injection here:
            // https://glcheetham.name/2016/05/27/implementing-ioc-in-umbraco-unit-testing-like-a-boss/
            // https://glcheetham.name/2017/01/29/mocking-umbracohelper-using-dependency-injection-the-right-way/
    
    
            var umbracoContext = umbracoApplication.Context.GetUmbracoContext();
    
            var builder = new ContainerBuilder();
    
            var umbracoHelper = new Umbraco.Web.UmbracoHelper(umbracoContext);
    
            // Register our controllers from this assembly with Autofac
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            // Register controllers from the Umbraco assemblies with Autofac
            builder.RegisterControllers(typeof(UmbracoApplication).Assembly);
            builder.RegisterApiControllers(typeof(UmbracoApplication).Assembly);
            // Register the types we need to resolve with Autofac
            builder.RegisterInstance(ExamineManager.Instance).As<ExamineManager>();
            builder.RegisterInstance(umbracoHelper.ContentQuery).As<ITypedPublishedContentQuery>();
    
            // Set up MVC to use Autofac as a dependency resolver
            var container = builder.Build();
            System.Web.Mvc.DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    
            // And WebAPI
            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        }
    }
    
  • Nik 1135 posts 4701 karma points MVP 2x c-trib
    Aug 06, 2019 @ 09:22
    Nik
    1

    Hi Tom,

    The ApplicationEventHandler has gone in v8. Instead a new concept has been introduced which is composing. This is made up of a Composer and Components.

    https://our.umbraco.com/documentation/Implementation/Composing/

    The documentation is reasonable on this so I'd give this a read. It should help you update your code for v8.

    Thanks

    Nik

  • Tom 711 posts 948 karma points
    Aug 06, 2019 @ 22:40
    Tom
    0

    Hi Nik, I still can't see based on the docs what the equivalent to OnApplicationStarted is?

  • Nik 1135 posts 4701 karma points MVP 2x c-trib
    Aug 06, 2019 @ 22:58
    Nik
    0

    Hi Tom,

    I believe you'll want the initialize function of a Component and you'll want to use an IUserComposer as the base for your composer class. There are 3 types of composer and that is generally the one to use.

    Nik

  • Nik 1135 posts 4701 karma points MVP 2x c-trib
    Aug 06, 2019 @ 23:02
    Nik
    100

    https://www.zpqrtbnk.net/posts/composing-umbraco-v8-components/

    This blog post might help, there are 2 others on this blog about components and composers as well

    Hope it helps

    Nik

  • Tom 711 posts 948 karma points
    Aug 06, 2019 @ 23:14
    Tom
    0

    Thanks Nik that helped a lot :)

    Trying to get umbraco and react to work together man it is a nightmare

  • Tom 711 posts 948 karma points
    Aug 07, 2019 @ 00:08
    Tom
    0

    Actually it looks like with v8 dependency injection I shouldn't need the previous code at all :)

    https://www.perplex.nl/en/blog/2019/umbraco-v8-changes-for-developers/

    So all the above code gets ripped out and replaced with:

    public class UmbracoReactStartupComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.SetDefaultRenderMvcController(typeof(ReactRoutesController));
    
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft