Global.asax page ? In Asp.net? - asp.net

In web application [asp.net] shall we write code that can retrive data from datbase or insert something in database. in any event of global.asax. when i am writing code in "Application_BeginRequest" even like :
Session["abc"]= 10; it is throwing exception that session can't declare some thing.Session state is not available in this context.

Oh. You need to read about ASP.NET page cycle
Then You will realize that Session state is not available on BeginRequest

Assuming I am understanding your question correctly then you are right that you cannot access session in that event. Try instead putting it in Application.AcquireRequestState. This is where ASP.NET sets up session and such like.
What you can use in BeginRequest is the Context which is acceesible in that request if you just need to set up data for just that request.

Related

How do I instantiate the Profile from an ashx file?

I recently switched some AJAX queries to use ashx files instead of aspx, having discovered that Response.End is no longer in vogue. In the case I'm looking at now, the submission of a purchase order is handled by the ashx. The user clicks the Submit button on the PO and the ashx file records the time and the user in a database table. An object with the current user's data (including their primary key) is stored in the Session and is recorded there by a custom Profile Provider. I have added IReadOnlySessionState in order to be able to access the Session, but it appears that, unless another .net page has been accessed, the Profile Provider doesn't run and the Session has no values. (verified by stepping through the code.)
I tried IRequiresSessionState, but the same results.
I'm assuming that the System.Web.UI.Page HttpHandler instantiates the custom Profile if it hasn't been already and that I'll need to add that to my custom HttpHandler.
I looked briefly at HttpModules, but I don't think that's what I want because they load for each request.
What is the value of HttpContext.Current.Profile? The Profile property is populated during the AcquireRequestState stage of the Request lifecycle by the built in HttpModule ProfileModule.
There is great info here http://odetocode.com/articles/440.aspx on how the internals work.

How do I access the session in order to do a rewrite?

I'm trying to use the HttpContext.Current.RewritePath method to serve up different versions of an application to different customers. I would love to be able to look at the HttpContext.Current.Session object to know who it is making the request. Oddly when I go to look at this at the BeginRequest handler, the Session always shows null. I've tried various other events and have had no luck. Any ideas on this?
You should look at the ASP.Net Application Life Cycle Overview and the Session State Overview pages on the MSDN.
I believe you can do what you're wanting to do by hooking into the Session_OnStart event, which is fired any time a new session is created.

(ASP.NET) How would I determine the length of time a user has been on the site?

I need to display an alert to a user, if they have been on my site for five minutes and have not logged in.
How do I do that?
Would I add soemthing to session on Application_Start? Is there a way to just check the length of someone's session? Perhaps put something in the header/master page and if it exceeds five minutes throw up the alert?
Any help would be appreciated.
Thank you.
EDIT ---
What I ended up doing was using the asp:Timer control found with the AjaxControlToolKit.
Application_Start fires when the IIS Application initially gets loaded.
Session_Start would fire for each new Session that gets started.
If you store the current time in the Session in Session_Start then you can check on either a page load or with an ajax call to determine if five minutes have passed without them logging in.
The answer does lie in Globals.asax, but Application_Start is not it. That is used for when the ASP.NET application actually starts.
I would add DateTime.Now to the session in the Session_Start method in Globals.asax. Then, you can either check it on each page load (for instance, in a base page or Master page's onload event), or use Ajax to poll the server.
If you truly want a 5 minute check, you need to register a 5 minute ajax callback from Session_Start. The tricky part is having a page loaded and ready that can receive it when it fires.
Polling the server with AJAX is probably the more common approach.

Is it possible to destroy a session variable set by ASP.NET (VB.NET) with Javascript?

As the title states, I have a session variable that is set during a login script with asp.net and vb.net code-behind. Can I call a javascript function when a link is clicked that will destroy that session variable?
If it is possible, can I use jQuery to make that process easier?
Yes, use $.post("logout.aspx"); in your Javascript, and create the file logout.aspx that destroys the session.
Server-side code (VB.NET in your case) is the only thing that can access the session and its variables. Your best bet is to create a WebService/WebMethod and use jQuery to call it.
This article should help you get started: Using jQuery to Consume ASP.NET JSON Web Services
Not explicitly - session variables live on the server, while Javascript operates in the client.
The best you can do is use JS to send a request to the server (possibly via Ajax), that will cause the server to delete the sesion variable.
Delete the ASP.NET Session cookie
document.cookie = 'ASP.NET_SessionId=xxx';

Using EndRequest Event to close NHibernate Session with IIS 7

I am using NHibernate on a new ASP.NET project, and am running into what I believe to be strange behavior. I am attempting to manage my session by using an HttpModule to catch the EndRequest event and close the session. This is working fine, however, after the EndRequest event fires, I am getting an exception in the OnLoad event of one of my custom controls that is attempting to read a Property from my object that is lazy loaded. I get an exception stating 'failed to lazily initialize a collection, no session or session was closed'. Turning lazy load off for these properties does fix the problem, and is an acceptable solution. But this seems to be going against what I always thought to be true.
I would assume that the OnLoad event and all server side processing would be done at the point that EndRequest is fired. This is also the first time that I have used IIS 7 on a project. Is this a reason for the behavior? What is the expected behavior?
I just had a 'palm slaps forehead' moment. Despite the fact that I am in fact deploying to an IIS 7 server, I have been debugging using the VS 2008 Built in Web server (Casini). Casini passes all requests through the ASP.NET pipeline, IIS does not. This was causing a request for an image file or javascript file (or any other static resource) to close my NHibernate session before I was actually thinking it should close.
Thanks for the list of resources, I will certainly look into them.
If your object is lazy-init and no session is open, resolving the properties will fail.
Remember that lazy exceptions will most probably appear when you have some relationship with another entity that hasn't been initialized.
http://forum.springframework.org/showthread.php?t=13474
I would also recommend using something like the HTTP Module in Rhino Commons to manage your NHibernate Sessions.
You should use a shrinkwrapped package for dealing with this.
I like to use Autofac with ASP.NET/MVC integration. You simply ask the RequestContainer for an ISession whenever you need it and, because the ISession is IDisposable, the RequestContainer automatically knows to dispose of the ISession when the current request ends. Everything is taken care of for you.
use HttpModule if you need lazy loading. Inherit your class from it and then you'd have two methods you can override (can't remember their names). First one is called each time any page is requested. Open the session there and put the session in viewstate. The other method is called when page is posted back, close your session there.

Resources