I have need to access some events in the global.asax - but I'm not seeing any particular way to do this short of recompiling umbraco. I really don't want to have a custom version of umbraco running for this website, does anyone have any ideas how I might be able to get to these events?
Right now, I specifically need ProfileOnMigrateAnonymous, but I could potentially see needing ApplicationStart, or Session_Start in the future too.
not sure if this will work but could you not drop a class into the App_code dir that inherits from System.Web.HttpApplication and implement events there? although not sure how that would tie in with events that have already been handled in global.asax?
Without the Global.asax file pointing to my class I don't think that my class would ever get instantiated. But you gave me a good idea... :)
I'm going to try to create a new class that inherits from umbraco.Global, and then point Global.asax at this new class. I think then that:
a) Umbraco will continue to get the events it needs,
b) I will not have to modify and recompile umbraco, forcing the client to keep diffs.
c) I will (should) get access to the events I need.
From what I have tested, I think this should work. I had hoped that umbraco would expose these things, but having looked at the code to global, I very much doubt it.
Awesome. Let us know how you get on. I have in the past wanted to put in an IOC container and a log4net into some external stuff i was doing that i was using in umbraco but had the same issue as you when it came to the global.asax, couple of other people have posted similar things.
Testing shows that it works, but there is an obvious caveat:
You can't use any event that is already used inside Umbraco's Global.asax.cs file, as the functions in that file are not declared as virtual; i.e. You cannot override them.
Anything that is not already declared in the existing global is fair game.
On a side note - the only thing in that file that really looks like it does anything is the ApplicationEnd event. The rest are empty, or in the case of SessionStart appears to not be doing anything unless I have missed something, and "new Session();" actually accomplishes something non-obvious - in which case a comment above it would really help document that.
Niels/Per ... any thoughts with regards to removing the unused methods, or at least making them virtual for the next release?
The only other thing is becuase the global.asax has been customised you would need to remember not overwrite during upgrade, still better than modifying core code. Might be worth putting a request into codeplex regarding making methods virtual. Put link in to it in post i'll vote for it!
(Note: Copied from a previous post for the sake of clarity).
I am using Umbraco 4.0.0.
I need to add the following method to a "Global.asax" file under Umbraco Website Folder:
[code]Protected Sub ApplicationAcquireRequestState( _
ByVal sender As Object, ByVal e As System.EventArgs)
Dim principal As System.Security.Principal.IPrincipal
Try
principal = _
CType(Session("CslaPrincipal"), System.Security.Principal.IPrincipal)
Catch
principal = Nothing
End Try
If principal Is Nothing Then
' didn't get a principal from Session, so
' set it to an unauthenticted PTPrincipal
CSLAxx.Library.Security.xxPrincipal.Logout()
'FormsAuthentication.SignOut
Else
' use the principal from Session
Csla.ApplicationContext.User = principal
End If
End Sub[/code]
This is required because I am integrating the Custom CSLA .NET Authentication with Umbraco Membership Provider. This method "ApplicationAcquireRequestState" is required to avoid initialization the Principal/Identity Object every time the website is accessed, ie, to improve performance.
If I do not add this file "Global.asax", everything else work fine, and the only problem is that the creation of the Identity and Principal objects will occur on every visit to the website for every user, which is really a bad thing.
However, after I add the Global.asax to my Umbraco Website, I will get this error as soon as I try to access the website:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30560: 'globalasax' is ambiguous in the namespace 'ASP'.
Source Error:
Line 94: Public Sub New()
Line 95: MyBase.New
Line 96: If (Global.ASP.globalasax.initialized = false) Then
Line 97: Global.ASP.globalasax.initialized = true
Line 98: End If
Source File: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\d9ed7379\566fad7a\Appglobal.asax.pxvuwuud.0.vb Line: 96 [/quote]
Any one could recommend a resolution or a work around to solve this problem.
I have never worked with HTTP Modules inside a class in app_code, so I appreciate giving me some examples or hints.
In the Appcode file you can create a class that Implements IHttpModule Then you can override the methods. Check out this post that explains how to build a HTTPModule and how to configure it when you use the appcode folder instead of a seperate DLL.
thank you for the posts, however I was not able to access the method Application_Start from the IHttpModule as well as the methods Application_End, Session_Begin and Session_Start and Session_End. I searched a bit and found out that through the implementation of an IHttpModule it is not possible.
I need to overide (put some logic) the Application_Start method, is there any alternative?
I've created another website project with its own custom global asax events. The the resulting project dll works fine with umbraco by dumping it into the \bin folder of our website. I'm not sure if this is what you're looking for thought so apologies in advanced.
I just thought I'd let you know I've cracked it, finally!
The problem was the App_global.asax.dll was in the BIN folder, I have removed this and my conflict disappeared and my Application_Start is now working.
@Slace.. Am I right from your comment that I can just remove this file without breaking any Umbraco functionality?
Thanks again for your quick replies, just what I needed!
Yes, you can remove it without any problem, it doesn't do anything vital (in fact, I'm pretty sure what it does is ignored through out the site anyway).
App_global.asax.dll I've only come across in legacy site upgrades, or sites without a global.asax file.
If you want to work with the HttpApplication you can only have 1 instance of the class and it's best practice to have a global.asax file so you can found the pointer to that instance.
Also, don't put your class in the App_Code folder, this causes the type to be compiled before the application states into a dynamic assembly (ie - one in your ASP.NET Temporary Cache folder). It's then really hard to debug (app startup compiler errors are what you get) and you can't use it as a referenced assembly.
How strange, I have the original App_global.asax.dll in my /bin folder along with my own global_asax compiled code in my own project dll also in /bin. Have I missed something!?
Does someone has this working in Juno? I'm not getting the Application_Start to run for me. (I removed the app_global.asax.dll) what am I missing? hmm...
public class RavenDbInit : umbraco.Global
{
protected override void Application_Start(object sender, EventArgs e)
{
base.Application_Start(sender, e);
Log.Add(LogTypes.Custom,-1,"Application start");
}
}
I remember raising this on codeplex and was told 452 will have ability to override which is also there in juno and you are trying but i recall there was a reason why this wont work but cant remember try reflecting in umbraco.Global or view the umbraco source directly as a solution try http://our.umbraco.org/forum/developers/extending-umbraco/10564-Accessing-globalasax-events- Matts comment of inherting from ApplicationBase as that effectively acts as application_start
Need to access events in the global
I have need to access some events in the global.asax - but I'm not seeing any particular way to do this short of recompiling umbraco. I really don't want to have a custom version of umbraco running for this website, does anyone have any ideas how I might be able to get to these events?
Right now, I specifically need ProfileOnMigrateAnonymous, but I could potentially see needing ApplicationStart, or Session_Start in the future too.
sblomfield,
not sure if this will work but could you not drop a class into the App_code dir that inherits from System.Web.HttpApplication and implement events there? although not sure how that would tie in with events that have already been handled in global.asax?
Regards
Ismail
Ismail,
Without the Global.asax file pointing to my class I don't think that my class would ever get instantiated. But you gave me a good idea... :)
I'm going to try to create a new class that inherits from umbraco.Global, and then point Global.asax at this new class. I think then that:
a) Umbraco will continue to get the events it needs,
b) I will not have to modify and recompile umbraco, forcing the client to keep diffs.
c) I will (should) get access to the events I need.
From what I have tested, I think this should work. I had hoped that umbraco would expose these things, but having looked at the code to global, I very much doubt it.
sblomfield,
Awesome. Let us know how you get on. I have in the past wanted to put in an IOC container and a log4net into some external stuff i was doing that i was using in umbraco but had the same issue as you when it came to the global.asax, couple of other people have posted similar things.
So if it works that would be brilliant.
Regards
Ismail
Ismael,
Testing shows that it works, but there is an obvious caveat:
You can't use any event that is already used inside Umbraco's Global.asax.cs file, as the functions in that file are not declared as virtual; i.e. You cannot override them.
Anything that is not already declared in the existing global is fair game.
On a side note - the only thing in that file that really looks like it does anything is the ApplicationEnd event. The rest are empty, or in the case of SessionStart appears to not be doing anything unless I have missed something, and "new Session();" actually accomplishes something non-obvious - in which case a comment above it would really help document that.
Niels/Per ... any thoughts with regards to removing the unused methods, or at least making them virtual for the next release?
Thanks,
Scott
Scott,
The only other thing is becuase the global.asax has been customised you would need to remember not overwrite during upgrade, still better than modifying core code. Might be worth putting a request into codeplex regarding making methods virtual. Put link in to it in post i'll vote for it!
Regards
Ismail
(Note: Copied from a previous post for the sake of clarity).
I am using Umbraco 4.0.0.
I need to add the following method to a "Global.asax" file under Umbraco Website Folder:
[code]Protected Sub ApplicationAcquireRequestState( _
ByVal sender As Object, ByVal e As System.EventArgs)
Dim principal As System.Security.Principal.IPrincipal
Try
principal = _
CType(Session("CslaPrincipal"), System.Security.Principal.IPrincipal)
Catch
principal = Nothing
End Try
If principal Is Nothing Then
' didn't get a principal from Session, so
' set it to an unauthenticted PTPrincipal
CSLAxx.Library.Security.xxPrincipal.Logout()
'FormsAuthentication.SignOut
Else
' use the principal from Session
Csla.ApplicationContext.User = principal
End If
End Sub[/code]
This is required because I am integrating the Custom CSLA .NET Authentication with Umbraco Membership Provider. This method "ApplicationAcquireRequestState" is required to avoid initialization the Principal/Identity Object every time the website is accessed, ie, to improve performance.
If I do not add this file "Global.asax", everything else work fine, and the only problem is that the creation of the Identity and Principal objects will occur on every visit to the website for every user, which is really a bad thing.
However, after I add the Global.asax to my Umbraco Website, I will get this error as soon as I try to access the website:
[quote]Server Error in '/' Application.
--------------------------------------------------------------------------------
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30560: 'globalasax' is ambiguous in the namespace 'ASP'.
Source Error:
Line 94: Public Sub New()
Line 95: MyBase.New
Line 96: If (Global.ASP.globalasax.initialized = false) Then
Line 97: Global.ASP.globalasax.initialized = true
Line 98: End If
Source File: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\d9ed7379\566fad7a\Appglobal.asax.pxvuwuud.0.vb Line: 96 [/quote]
Any one could recommend a resolution or a work around to solve this problem.
I have never worked with HTTP Modules inside a class in app_code, so I appreciate giving me some examples or hints.
Thank you.
Tarek.
Hi,
In the Appcode file you can create a class that Implements IHttpModule Then you can override the methods. Check out this post that explains how to build a HTTPModule and how to configure it when you use the appcode folder instead of a seperate DLL.
Cheers,
Richard
Hello,
thank you for the posts, however I was not able to access the method Application_Start from the IHttpModule as well as the methods Application_End, Session_Begin and Session_Start and Session_End. I searched a bit and found out that through the implementation of an IHttpModule it is not possible.
I need to overide (put some logic) the Application_Start method, is there any alternative?
thanks in advance,
Ricardo
Hi All,
Did anyone find a solution to this?
I also need to add some code to the Application_Start is this possible?
Cheers,
Chris
Why can't you just inherit from the HttpApplication class, repoint the global.asax to use yours and ignore the Umbraco one.
The Umbraco one does nothing anyway.
We've got 2 custom implementations of the HttpApplication in the Snapshot assemblies.
I have modified the 4.1 version so that it the Application_State is virtual and you can inherit from the umbraco.Global class.
I've created another website project with its own custom global asax events. The the resulting project dll works fine with umbraco by dumping it into the \bin folder of our website. I'm not sure if this is what you're looking for thought so apologies in advanced.
Hi Mo / Slace,
Thank you both for your reply.
I just thought I'd let you know I've cracked it, finally!
The problem was the App_global.asax.dll was in the BIN folder, I have removed this and my conflict disappeared and my Application_Start is now working.
@Slace.. Am I right from your comment that I can just remove this file without breaking any Umbraco functionality?
Thanks again for your quick replies, just what I needed!
Cheers,
Chris
Yes, you can remove it without any problem, it doesn't do anything vital (in fact, I'm pretty sure what it does is ignored through out the site anyway).
App_global.asax.dll I've only come across in legacy site upgrades, or sites without a global.asax file.
If you want to work with the HttpApplication you can only have 1 instance of the class and it's best practice to have a global.asax file so you can found the pointer to that instance.
Also, don't put your class in the App_Code folder, this causes the type to be compiled before the application states into a dynamic assembly (ie - one in your ASP.NET Temporary Cache folder). It's then really hard to debug (app startup compiler errors are what you get) and you can't use it as a referenced assembly.
Hi Chris,
How strange, I have the original App_global.asax.dll in my /bin folder along with my own global_asax compiled code in my own project dll also in /bin. Have I missed something!?
Does someone has this working in Juno? I'm not getting the Application_Start to run for me. (I removed the app_global.asax.dll) what am I missing? hmm...
Jonas,
I remember raising this on codeplex and was told 452 will have ability to override which is also there in juno and you are trying but i recall there was a reason why this wont work but cant remember try reflecting in umbraco.Global or view the umbraco source directly as a solution try http://our.umbraco.org/forum/developers/extending-umbraco/10564-Accessing-globalasax-events- Matts comment of inherting from ApplicationBase as that effectively acts as application_start
Regards
Ismail
Thanks Ismail & Matt, worked like a charm. Cheers!
For those looking for Version > 8 information you might want to visit
Subscribing to Events
is working on a reply...