Regarding Session_End and Application_End methods in asp.net - asp.net

I'm trying to implement a class (lets call it CustomerConnection class) which implements System.Web.HttpApplication interface. I tried to implement two events session_end and application_end events in this class in this way:
public class CustomerConnection: System.Web.HttpApplication
{
protected void Session_End(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
SqlConnection connection = context.Items[Database_Con] as SqlConnection;
connection.close();
}
protected void Application_End(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
//someotherEvent
}
But it seems these events are not getting executed neither for the application end or session log out.
How should i make these events get´executed ? Is it like i have to notify to certain other event?
I'm just creating a new object of customer class. is it enough to get these events executed or my approach is fundamentally wrong?
Please help me out
Thanks in anticipation

You need this in global.asax:
<%# Application Language="C#" Inherits="CustomerConnection"%>
Caveat, I've never tried this, I found it here: http://www.codedigest.com/Articles/ASPNET/34_Adding_CodeBehind_for_Gloabalasaz_x_file_in_aspnet_20.aspx

The object that you have created isn't connected to anything, so it can't receive any events from the application. You have to put the event handlers in the object that is used for the web application, i.e. in the Global.asax.cs file.
Besides, there is no Session_End event, it's Session_OnEnd.

Related

How to handle former global event handlers in ASP.NET 5?

Actually, we use ASP.NET 4.5 (VS 2013) and want to replace Global.asax.cs with new Startup.cs file, which comes from OWIN specification.
We log the start and the end of application using Application_Start and Application_End handlers in this way:
protected void Application_Start(object sender, EventArgs e)
{
_log.Info("The app starts.");
}
protected void Application_End(object sender, EventArgs e)
{
_log.Info("The app ends.");
}
But, as I know there is no such handlers in the new Startup.cs file. So, my question is - how can we do that?
You already know when the application is starting since Startup.cs is executing then. For the Application_End event have a look at this answer: Is there Application_End from Global.asax in Owin?

Initialize variables before having anyone enter the web app

How can I initialize variables before my Web Application starts? I want to have, for example an Application type, to count number of logged in users in my website (it's an example, I have more variables that need initialization). How can I perform that?
It could be very useful if there was some event that would be called at the start of the Web App. I thought that I could check in each Page_Load event if a certain Session called, e.g Session["Started"] is null, and if it, redirect to an .aspx page for initialization. Or, even better, have a class called, e.g MyPage which inherits from System.Web.UI.Page and have her constructor get a function as an argument, which will be called after the initialization of the base class Page_Load.
Maybe I'm just bothering, is there any built-in event called that I can overwrite to initialize everything I want in the beginning?
You can use the Global.asax file for this. In particular you can use the Application_Start, Session_Start, Session_End events.
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
}
Here for more informations.

How to log information on session_start

I want to log on browser information on a session_start event but, of course in global.asax I can't use
Request.Browser
how should I set it up?
You can use Session_Start in the global.asax file.
protected void Session_Start(Object sender, EventArgs e)
{
HttpContext context = base.Context;
HttpRequest request = context.Request;
HttpBrowserCapabilities browserCapabilities = request.Browser;
// log, for example the browser's name:
string browserName = browserCapabilities.Browser; // Firefox in my case
}
The Session_Start event is fired the first time when a user’s session is started. This typically contains for session initialization logic code.
Working with the ASP.NET Global.asax file

WCF\ASP.NET interoperability

I have a server application written in WCF using asynchronous callbacks, and a webforms application in ASP.NET.
All of the communication is fine between the 2 applications, I can call the exposed functions in the server via the web application, and the server can send callbacks to the web application, however sometimes the functions within the callback work, and other times, they don't.
For example, I would like a login button on the web app to send a username and password to the server, the server checks this against the database, and if the login information is correct, it should send a callback, which opens a new page in the web app.
Here is the relevant server code:
public void Login(String username, String password)
{
//DoCheckAgainstDatabase(username, password);
ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>();
callback.LoginSuccess();
}
and the web application code:
private InstanceContext _instanceContext;
private ServiceClient _service;
public CallbackHandler MyCallbackHandler = new CallbackHandler();
protected void Page_Load(object sender, EventArgs e)
{
_instanceContext = new InstanceContext(MyCallbackHandler);
_backEnd = new ServiceClient(_instanceContext, "NetTcpBinding_IAU", "net.tcp://localhost/MyService/Service");
_backEnd.Open();
MyCallbackHandler.LoginSucceeded += OnLoginSucceeded;
}
protected void LoginButton_Click(object sender, EventArgs e)
{
_backEnd.Login(UsernameTextBox.Text, PasswordTextBox.Text);
}
private void OnLoginSucceeded(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "OpenClientWindow", "window.open('Client.aspx','_self');", true);
}
I can put in breakpoints, and see that everything is working fine, it's just that the code 'ScriptManager.RegisterStartupScript...' does not execute properly all the time.
Could this be something to do with threading? Could anyone suggest a way to fix this please?
Thanks in advance!
David
It occurs to me that it's possible your page life cycle may be ending - or at least getting to the Render stage, which is where the start up script would be written to the output - before the callback is called.
Is it possible to call your service synchronously, and not proceed out of LoginButton_Click until the service call returns?
I think you are missing script tag - wrap your window.oppen with it, like
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "OpenClientWindow", "<script>window.open('Client.aspx','_self');</script>", true);
Thank you Ann L. for guidance on this. I have added a ManualResetEvent, and then in the button click method, I wait until I have received the callback, then proceed with opening the new page:
private InstanceContext _instanceContext;
private ServiceClient _service;
public CallbackHandler MyCallbackHandler = new CallbackHandler();
private ManualResetEvent _resetEvent = new ManualResetEvent(false);
protected void Page_Load(object sender, EventArgs e)
{
_instanceContext = new InstanceContext(MyCallbackHandler);
_backEnd = new ServiceClient(_instanceContext, "NetTcpBinding_IAU", "net.tcp://localhost/MyService/Service");
_backEnd.Open();
MyCallbackHandler.LoginSucceeded += OnLoginSucceeded;
}
protected void LoginButton_Click(object sender, EventArgs e)
{
_backEnd.Login(UsernameTextBox.Text, PasswordTextBox.Text);
_resetEvent.WaitOne();
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "OpenClientWindow", "window.open('Client.aspx','_self');", true);
}
private void OnLoginSucceeded(object sender, EventArgs e)
{
_resetEvent.Set();
}

On-Session-expire-event?

I'm programming an MVC3 application. Now I hava to call a script if the users session expire.
Is there something like a event on-session-expire, that get fired when the user session expired?
Thanks a lot!
In your Global.asax
you can create a
protected void Session_End(object sender, EventArgs e) { }
method which should be called when a session ends.

Resources