Developer forum

Forum » Dynamicweb 9.0 Upgrade issues » Recommended code for custom Global.asax

Recommended code for custom Global.asax

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

How should a custom Global.asax look in a custom project? I tried something like this:

public class Global : HttpApplication
{   
  public void Application_AuthenticateRequest(object sender, EventArgs e)
  {
    GlobalAsaxHandler.Application_AuthenticateRequest(sender, e);
  }
  ...
}

However, that either crashes in Application_Start of the Dynamicweb Start handler with a null reference exception or I end up in an endless loop. Could it be that I am missing the checks for the Preloader and/or the code that sets up the context?

I hacked my way around it with the following:

public class Global : Dynamicweb.Admin.Global
{   
  public void Session_Start(object sender, EventArgs e)
  {
    GlobalAsaxHandler.Session_Start(sender, e);
    // My stuff on Session_Start here
  }
}

This works and runs my custom Global code but it feels a bit wrong as I am "new-ing" Session_Start.

Is there a best practice?

Thanks,

Imar


Replies

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

Hi Imar,

You are right. It seems like we may have a problem here. We will take a closer look at it.

As a workaround you could implement a custom IHttpModule instead.

/Morten

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Thanks. I'll use an IHttpModule or some lazy-loaded code instead.

Imar

 
Martin Vang
Reply

I know this is a late answer, but you might find it of interest nevertheless.

Starting from 9.1 Global.asax will not contain any custom code. All Dynamicweb-related events and lifecycle states are handled in GlobalAsaxHandler, which in turn exposes our more explicit lifecycle through notifications.

Documentation of this lifecycle will be made available on the documentation portal, as soon as we find a good place for it.

 
Frederik Gadegaard
Reply

Hello, 

I am wondering if it is now possible somehow to hook into Session_Start and Session_End events?

From the documentation it seems like there is no way of doing so: https://doc.dynamicweb.com/documentation-9/platform/system-architecture/application-lifecycle

And implementing IHttpmodule is not viable for specifically these two events, whereas for example Application_Error doable. (based on my knowledge at least). 

Kind Regards
Frederik

 

 

 
Nicolai Pedersen
Reply

It is possible. But you probably do not want to do that and it is probably not needed. 

You can just create your own Global.asax file and add your own code - map it to our implementations, see below.

But if you tell us why you need this - what you are trying to achieve, we might have a better solution for you. I.e. using a notification subscriber.

Imports System.ComponentModel
Imports System.Diagnostics
Imports Dynamicweb.Frontend

<EditorBrowsable(EditorBrowsableState.Never)>
<Browsable(False)>
Public Class [Global]
    Inherits HttpApplication

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        GlobalAsaxHandler.Application_Start(sender, e)
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        GlobalAsaxHandler.Session_Start(sender, e)
    End Sub

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        GlobalAsaxHandler.Application_BeginRequest(sender, e)
    End Sub

    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
        GlobalAsaxHandler.Application_AuthenticateRequest(sender, e)
    End Sub

    Sub Application_OnPreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
        GlobalAsaxHandler.Application_OnPreRequestHandlerExecute(sender, e)
    End Sub

    Sub Application_EndRequest(ByVal sender As Object, ByVal e As EventArgs)
        GlobalAsaxHandler.Application_EndRequest(sender, e)
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        GlobalAsaxHandler.Application_Error(sender, e)
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        GlobalAsaxHandler.Session_End(sender, e)
    End Sub

    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        GlobalAsaxHandler.Application_End(sender, e)
    End Sub
End Class

 

You must be logged in to post in the forum