>>No simple fix for these. Maybe you should store the items that aren't Request/Response related in the Context.Items collection rather than as framework properties. It would mean fairly substantial changes to your existing code but I see no obvious alternative.
>
>I found something simple.
>
>First of all, the global.asax now only contains this:
>
>
>< %@ Application Language="VB" % >
>
><object id="LXFramework" runat="server" class="Framework.Framework.App" scope="Application" />
>
><script runat=server>
>
> Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
> LXFramework.nApplicationMode = 3
> LXFramework.oServer = Server
> LXFramework.Initialize()
> End Sub
>
> Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
> LXFramework.ErrorSetup()
> End Sub
>
> Sub Application_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
> Dim loContext As Object = Context.Handler
> Dim loType As Object = loContext.GetType()
> Dim lcType As String = loType.ToString
>
> ' .vb and .config not accepted
> If lcType = "System.Web.HttpForbiddenHandler" Then
> Response.Redirect("Default.aspx")
> End If
>
> End Sub
>
></script>
>
>
>Secondly, all the ASPX pages inherits from the WebForm class of the framework like this:
>
>
>< %@ Page Language="VB" AutoEventWireup="false" Inherits="Framework.Framework.WebForm" % >
>
>
>Then, the Web Form class is having everything it needs to instantiate the framework and make it available at the page level. Then, from the page level, I can then use a reference to oApp to start things up:
>
>
>Namespace Framework
>
> Public Class WebForm
> Inherits System.Web.UI.Page
>
> Public oApp As Framework.App = New Framework.App()
>
> Sub New()
> End Sub
>
> Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
> oApp.nApplicationMode = 3
> oApp.oServer = Server
> oApp.Initialize()
> oApp.oRequest = Request
> oApp.oResponse = Response
> oApp.oPage = Me
> oApp.BeginRequest()
> End Sub
>
> Sub Page_UnLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Unload
> oApp.EndRequest()
> oApp.oResponse.Write(oApp.GetJavascriptMessage)
> End Sub
>
> End Class
>
>End Namespace
>
>
>
><script runat="server">
> Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
> Dim loDataEntry As DataEntryPurchase = New DataEntryPurchase(oApp)
> loDataEntry.GenerateForm()
> End Sub
></script>
>
>
>This works and it responds fast. However, I want it to be faster. The next goal would be to get a reference to the global.asax LXFramework object in the WebForm class so I would be able to have the class like this which the initialization to the framework would be removed because I would be able to get a referenced copy of LXFramework from global.asax.
>
>
>Namespace Framework
>
> Public Class WebForm
> Inherits System.Web.UI.Page
>
> Public oApp As Framework.App = New Framework.App()
>
> Sub New()
> End Sub
>
> Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
>
> ' This is the line which need to be updated so I could get a reference to the global.asax framework object
> oApp=LXFramework
>
> oApp.oRequest = Request
> oApp.oResponse = Response
> oApp.oServer = Server
> oApp.oPage = Me
> oApp.BeginRequest()
> End Sub
>
> Sub Page_UnLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Unload
> oApp.EndRequest()
> oApp.oResponse.Write(oApp.GetJavascriptMessage)
> End Sub
>
> End Class
>
>End Namespace
>
>
>But, this is the part that is missing. I don't know if it is possible to get a reference to a global.asax object from a class. Is the current context able to help me in this case?
One way is to get a reference from the StaticObjects collection. C# version:
Framework.App oApp = (Framework.App )Application.StaticObjects.GetObject("LXFramework"");
but I fail to see how the code in Page_Init above differs in any real sense to the original code that you had in BeginRequest. You're back to passing a reference into a global object that cannot be quaranteed to be correct when accessed later. You have exactly the same predicament as in the original. Consider just these four lines of code
> oApp.oRequest = Request
> oApp.oResponse = Response
> oApp.oServer = Server
> oApp.oPage = Me
> oApp.BeginRequest()
By the time BeginRequest() is entered oApp.oRequest could already be invalid!
To recap: you *cannot* safely access hit dependent data from within the application scoped framework object except via the current context.
If you need hit related items that are *not* available through the existing objects (such as Request/Response) then you could create an object scoped to the request and add it to the Context.Items collection. e.g:
public class RequestScopeObject
{
public string cCookie = "";
public string cDecrypt = "";
public string cEncrypt = "";
}
Then in BeginRequest():
RequestScopeObject rso = new RequestScopeObject();
HttpContext.Current.Items.Add("RSO", rso);
And in the Framework:
RequestScopeObject rso = (RequestScopeObject)HttpContext.Current.Items["RSO"];
Regards,
Viv