Hooking Application_Startup in Umbraco Version 8.6.3
I am using Umbraco Version 8.6.3 and looking at trying to get my Captcha code running , one of the things the library I am using for ReCaptcha , it has a static config class instantiated with in the Global.asax.cs file App_Startup - the problem is I do not see that code running at all - and it appears that this is the case because my static config is basically null , and when I place
System.Diagnostics.Debugger.Break();
The code does not stop either and it should ..if it is being called but it does not.
In this Web Page Link I found something that seems to be the right way to gain access at startup - it points to a link that no longer exists - so I have no idea about the sample there but searched for that and
I found another link based on the info provided Version 7 Application Startup
Which led me to the Version 8 how to hook into Umbraco Start Events
Unfortunately the example there and at the BLOG listings provided do not show a good example of hooking into the application_startup .
The problem with this , is that the event fires multiple times through out the life cycle at least twice on startup (which is odd since it is an Init Method .. it also appears to fire at other times.
I am hoping that this is the right procedure to accomplish the task.
I have tried this out and it seems to work in my development code - if this is not the proper method can someone please assist or state this to be correct process (if not the best implementation I am open..)
public class ApplicationComposer : ComponentComposer<ApplicationComponent>, IUserComposer
{
public override void Compose(Composition composition)
{
// ApplicationStarting event in V7: add IContentFinders, register custom services and more here
// I was reading here
// https://our.umbraco.com/forum/developers/extending-umbraco/10564-Accessing-globalasax-events-
base.Compose(composition);
}
}
public class ApplicationComponent : IComponent
{
public void Initialize()
{
// ApplicationStarted event in V7: add your events here
WebStartInstance webStart = new WebStartInstance();
// This event fires multiple times - at least 2 times on load , and I thought on page refresh but i need to check that out.
UmbracoApplication.ApplicationInit += UmbracoApplication_ApplicationInit;
webStart.AppStartOverride();
}
private void UmbracoApplication_ApplicationInit(object sender, EventArgs e)
{
var mySecretTest = "Test" ;
var tmp = mySecretTest;
}
public void Terminate()
{ }
}
It looks to me as if you'd be fine executing your startup code directly within the Initialize method of your Component (there's no need to hook into any other events as you only need your code to execute once).
Typically they are used to hook up custom code to Umbraco events but there's nothing wrong in using one to configure your ReCaptcha on Initialize. Note that the Initialize method is executed once when Umbraco starts up.
Alternatively, you could access your static class from within your Composerand configure it there without the need for a Component (maybe someone will weigh in on their preference between the two for this use case).
Edit: Your ApplicationComposer class does not need to implement an IUserComposer if it inherits a ComponentComposer<T> too as that in itself is an implementation of IUserComposer that automatically registers your component for you :)
It can refactored to be public class ApplicationComposer : ComponentComposer<ApplicationComponent> {}
It was designed for previous .NET versions - that utilized the global.asax for startup - but what I really like is that I don't have to do a lot of explicit implementation of the reCaptcha and only need to decorate the called method of my controller with the captcha. I am also posting here in case someone else is trying to do the same thing and wants to see how I did it..
public class mySurfaceController : Umbraco.Web.Mvc.SurfaceController
{
[HttpPost]
[GoogleCaptcha]
public ActionResult controllerAction(MyFormViewModel formData)
{
}
}
I am setting the configuration parameters in the Initialize method of the Composer, just as you had stated - because I only wanted it 1 time.
I will look into the Dependency Injection Link and since I don't know alot about DI will look at the benefits and reasons for it ..as well. Thank you ..
Hooking Application_Startup in Umbraco Version 8.6.3
I am using Umbraco Version 8.6.3 and looking at trying to get my Captcha code running , one of the things the library I am using for ReCaptcha , it has a static config class instantiated with in the Global.asax.cs file App_Startup - the problem is I do not see that code running at all - and it appears that this is the case because my static config is basically null , and when I place
The code does not stop either and it should ..if it is being called but it does not.
In this Web Page Link I found something that seems to be the right way to gain access at startup - it points to a link that no longer exists - so I have no idea about the sample there but searched for that and I found another link based on the info provided
Version 7 Application Startup
Which led me to the Version 8 how to hook into Umbraco Start Events Unfortunately the example there and at the BLOG listings provided do not show a good example of hooking into the application_startup .
So I did find an event
The problem with this , is that the event fires multiple times through out the life cycle at least twice on startup (which is odd since it is an Init Method .. it also appears to fire at other times.
I am hoping that this is the right procedure to accomplish the task. I have tried this out and it seems to work in my development code - if this is not the proper method can someone please assist or state this to be correct process (if not the best implementation I am open..)
https://our.umbraco.com/documentation/reference/events/application-startup
Hi Ken,
It looks to me as if you'd be fine executing your startup code directly within the Initialize method of your
Component
(there's no need to hook into any other events as you only need your code to execute once).Here's the documentation for composing and components: https://our.umbraco.com/documentation/implementation/composing/
Typically they are used to hook up custom code to Umbraco events but there's nothing wrong in using one to configure your ReCaptcha on
Initialize
. Note that theInitialize
method is executed once when Umbraco starts up.Alternatively, you could access your static class from within your
Composer
and configure it there without the need for aComponent
(maybe someone will weigh in on their preference between the two for this use case).As a side note I'd avoid using statics where possible. What package are you using? There may be a way to set it up using Dependency Injection. More on DI: https://our.umbraco.com/documentation/reference/using-ioc/
I hope that helps - let me know how it goes! :)
Edit: Your
ApplicationComposer
class does not need to implement anIUserComposer
if it inherits aComponentComposer<T>
too as that in itself is an implementation ofIUserComposer
that automatically registers your component for you :)It can refactored to be
public class ApplicationComposer : ComponentComposer<ApplicationComponent> {}
Thank you for your knowledge and assistance - it is a big help.
I am using https://www.nuget.org/packages/reCaptcha.Google.Mvc/ https://github.com/xsoheilalizadeh/reCaptcha.Google.Mvc
It was designed for previous .NET versions - that utilized the global.asax for startup - but what I really like is that I don't have to do a lot of explicit implementation of the reCaptcha and only need to decorate the called method of my controller with the captcha. I am also posting here in case someone else is trying to do the same thing and wants to see how I did it..
I am setting the configuration parameters in the
Initialize
method of the Composer, just as you had stated - because I only wanted it 1 time.I will look into the Dependency Injection Link and since I don't know alot about DI will look at the benefits and reasons for it ..as well. Thank you ..
Thank you, James, your answer has helped me to fix my issue. Also thank you Ken for asking this question.
is working on a reply...