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);
}
}
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.
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.
So all the above code gets ripped out and replaced with:
public class UmbracoReactStartupComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.SetDefaultRenderMvcController(typeof(ReactRoutesController));
}
}
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?
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
Hi Nik, I still can't see based on the docs what the equivalent to OnApplicationStarted is?
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
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
Thanks Nik that helped a lot :)
Trying to get umbraco and react to work together man it is a nightmare
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:
is working on a reply...