Global.asax file conversion from 1.1 to 2.0

Global.asax file conversion from 1.1 to 2.0

Old forum URL: forums.lhotka.net/forums/t/4085.aspx


Bob Bedell posted on Monday, December 24, 2007

Hello,

I'm building CSLA from version 1.53 using C# Express 2005 and Visual Web Developer 2005. So I'm using the CSLA 1.53 code with .NET 2.0. I'm running Vista, can't install VS 2003, don't have time to learn CSLA 2.0 or 3.0 yet, and don't have the bucks for VS 2005 or 2008.

I have the framework built with no problems. I have the Windows version of Project Tracker running with no problems. I have an issue with the Web version of Project Tracker.

How do I translate the 1.53 Global.asax file (with codebehind, .NET 1.1) to a .NET 2.0 Global.asax file with no codebehind file? Of course I'm assuming I can, and that all esle will work OK. I am able to log into PTWeb fine, but when I click the Projects link, VWD complains that it requires a BusinessPrincipal object. Can I fix this problem with the appropriate code in a .NET 2.0 Global.asax file? Thats all I can think of thats missing from the app at the moment, that is present in the CSLA 1.53 app.

Thanks much,

Bob

 

Bob Bedell replied on Monday, December 24, 2007

This did not work, but may represent progress. While the .NET 2.0 implementation of Global.asax is to use a single file, it is possible to get Global.asax to use a code file. The CSLA 1.53 code I have in place is listed below. I'm not sure is Application_AcquireRequestState and Global_AcquireRequestState are identical, but they throw the same error: Session state is not available in this context. The offending line of code in Global.asax.cs is:

// set the security principal to our BusinessPrincipal

if (Session["CSLA-Principal"] != null)

At least I have a brand new error!! Thats gotta' be a sign of progress. Any help appreciated.

Global.asax

<%@ Application Language="C#" CodeBehind="Global.asax.cs" Inherits="Global" %>

Global.asax.cs

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Threading;

using System.Security.Principal;

/// <summary>

/// Summary description for Global

/// </summary>

public class Global : System.Web.HttpApplication

{

// added

public Global()

{

InitializeComponent();

}

void Application_Start(object sender, EventArgs e)

{

// Code that runs on application startup

}

void Application_End(object sender, EventArgs e)

{

// Code that runs on application shutdown

}

void Application_Error(object sender, EventArgs e)

{

// Code that runs when an unhandled error occurs

}

void Session_Start(object sender, EventArgs e)

{

// Code that runs when a new session is started

}

void Session_End(object sender, EventArgs e)

{

// Code that runs when a session ends.

// Note: The Session_End event is raised only when the sessionstate mode

// is set to InProc in the Web.config file. If session mode is set to StateServer

// or SQLServer, the event is not raised.

}

// added

protected void Application_AcquireRequestState(Object sender, EventArgs e)

{

// set the security principal to our BusinessPrincipal

if (Session["CSLA-Principal"] != null)

{

Thread.CurrentPrincipal = (IPrincipal)Session["CSLA-Principal"];

HttpContext.Current.User = Thread.CurrentPrincipal;

}

else

{

if (Thread.CurrentPrincipal.Identity.IsAuthenticated)

{

System.Web.Security.FormsAuthentication.SignOut();

Server.Transfer("Login.aspx");

}

}

}

private void InitializeComponent()

{

//

// Global

//

this.AcquireRequestState += new System.EventHandler(this.Application_AcquireRequestState);

}

}

I'm not sure if

Bob Bedell replied on Tuesday, December 25, 2007

OK,

Lastest development:

While I continue to get the 'Session state not available in this context' error when running the app in VWD, the app does run correctly when I view it in IE. Anyone ever run into anything like that? The Session object apparently isn't instantiated when the Application_AcquireRequestState event fires in VWD, but it apparently is when the compiled app is run in the browser. Why?

Thanks,

Bob

Bob Bedell replied on Tuesday, December 25, 2007

BUT...when I run the app in Debug...

The code breaks at
if (Session["CSLA-Principal"] != null)
sometimes. If it does break here, it cycles back through this line of code a couple of times until, apparently, session state IS available.

EVENTUALLY, the session state becomes available, and the app proceeds correctly. I get the same behavior on subsequent page requests.

It almost like there is a delay in initializing session state, and when I run the app in debug mode, app processing slows down enough to allow session state to be initialized? I'm just reaching here. But have no idea what else could be going on. Any thoughts?

Bob

Bob Bedell replied on Tuesday, December 25, 2007

I resolved these issues satisfactorily. I have a .NET 2.0 Global.asax file in place, and the Session state catches up eventually. Its working.

JoeFallon1 replied on Wednesday, December 26, 2007

Bob,

I think the issue you just worked through has been discussed before. I recall running into it too. I believe I put a small test at the top of the method to see if Session was available yet or not. If not, then it skipped over my code.

I think the numerous passes may be related to the number of Http Handlers in the project. In a brand new 1 page project it should probably only get called once. I use some 3rd party controls for Reporting and they may affect this too.

Joe

 

Bob Bedell replied on Wednesday, December 26, 2007

Thanks for the reply Joe. I did find some initial helpful info at:

http://forums.lhotka.net/forums/thread/2420.aspx

I find the session issue really only affects stepping through code in debug mode. Just have to tap F-11 a few extra times. The project displays fine when requested in a browser, though.

Best,

Bob

 

Copyright (c) Marimer LLC