by Andreas

Aborting AJAX requests in ASP.NET applications

imageI’ve been working on a pretty extensive web application lately, build on top of the .NET framework but with around 80% client side functionality (KnockoutJS, jQuery). There’s a lot of AJAX calls back to the server (naturally.. ) and one of the challenges I faced was that ASP.NET refused to navigate away from a page until all AJAX requests were processed. I did some searching and found out that an AJAX request could be aborted by adding some functionality to the window.onbeforeunload event:

window.onbeforeunload = function() {
    if (ajaxRequest !== undefined) {
        ajaxRequest.abort();
    }
};

 

By adding some logging to console I could clearly see that as soon as I clicked a link to navigate away this event was called and processed. But still nothing happened until the request had fully been processed – which made me think this had to do with the browser. Wrong assumption, and when writing this it seems very obvious to me why but then again retrospect is an ugly thing.

A bit more research and it turned out that this was all due to ASP.NET session state (which is enabled by default). Because of the session state my site serializes all requests within the same session, so the request for a new page would not be processed until the first one had completed. Turning session state off completely was out of the question, but by changing my .ashx handler (which receives all my ajax requests) to implement the IReadOnlySessionState interface rather than IRequiresSessionState there is no lock on the current session and concurrent requests are made possible.

Something to keep in mind though is that aborting an ajax request obviously doesn’t stop the server from processing it.

by Andreas

.NET HTTP Handlers (.ashx) – How to get access to Session

.NET HTTP Handlers (.ashx) are really useful for jQuery / JSON communication, but if you are using these as part of a bigger web application you will find that you don’t have immediate access to the current Session data. In a recent project I needed to validate some of the current user’s session information before allowing the handler to be invoked. In order to get access to HttpContext.Current.Session from the System.Web namespace you’ll have to add the System.Web.SessionState.IReadOnlySessionState interface in the class definition:

image

This will allow you to access the current session data:

// get session data
var sessionVariable = HttpContext.Current.Session["sessionVariableName"];
by Andreas

How to kill an RDP session on a remote server

My RDP session is locked up, and the remote server is located roughly 16 000km away. I’ve still got contact with it, but when initiating a remote desktop session all I get is a black screen. This is how you kill a RDP session on a remote machine:

First, if the server is on a different domain map up one of the drives to gain access (replace the hidden text with the IP address or server name):

image

Next, get a list of running sessions on the remote server. There are two ways, one using qwinsta and one using session (Windows 7):

image

or:

image

 

Kill the session by using the ID (3 in my case):

image

or

image

And if all else fails, reboot the whole thing:

image