Session keep alive in jQuery style

When you want to keep the session alive when a user is visiting a page without refreshes, there is a technique to make this happen. This is for example useful when you have a survey page with a long list of questions that will take a while to answer completely.

The technique is simple (best solutions mostly are simple). You need to notify the server that the client PC is still browsing the page. All you need is a client side script that sends a Ping message behind the scenes and a server side page that handles that ping request.

First the server side: since the request doesn’t need a GUI, a .NET handler can be used:

 1: using System;
 2: using System.Web;
 3: using System.Web.Services;
 4: using System.Web.SessionState;
 5:  
 6: namespace MvcApplication1.Shared
 7: {
 8:     [WebService(Namespace = "http://tempuri.org/")]
 9:     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 10:     public class KeepSessionAlive : IHttpHandler, IRequiresSessionState
 11:     {
 12:         public void ProcessRequest(HttpContext context)
 13:         {
 14:             context.Session["KeepSessionAlive"] = DateTime.Now;
 15:         }
 16:  
 17:         public bool IsReusable
 18:         {
 19:             get
 20:             {
 21:                 return false;
 22:             }
 23:         }
 24:     }
 25: }

 

2nd: the client side script

 1: <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
 2:  
 3: <asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
 4:     Home Page
 5: </asp:Content>
 6:  
 7: <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
 8:     <script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
 9:     <script language="javascript" type="text/javascript">
 10:         $(function() {
 11:             setInterval(KeepSessionAlive, 10000);
 12:         });
 13:  
 14:         function KeepSessionAlive() {            
 15:             $.post("/KeepSessionAlive.ashx", null, function() {
 16:                 $("#result").append("<p>Session is alive and kicking!<p/>");
 17:             });    
 18:         }    
 19:     </script>
 20:     <h2>Will my session die?</h2>    
 21:     <div id="result"></div>
 22: </asp:Content>

Btw, this is a example from a .NET MVC project, but can easily be a plain .html file.

Have fun,

Add IntelliSense for custom jQuery libraries

When you have worked with Visual Studio, you know the power of IntelliSense which helps you speed up coding. It works well with VB/C# languages, but it can also be useful for writing JavaScript / jQuery.

When writing JavaScript only basic suggestions are shown:

intellisense_without_good_suggestions

As you can see, only ajax.js, dhtml.js and ecma.js are mentioned. I have my own lib scene.dataservice.js in the VS project, but that doesn’t show up here. There is a simple trick to make this work. Just add <reference path=”<your_favourite_script>.js” /> as a comment, and see the magic:

intellisense_with_good_suggestions

Hope this helps,

Developing with DevNullSmtp mailserver

When you are developing a .NET website with e-mail functionality you often want to test the mails your code is sending. Often you can use a SMTP mail server within the domain, but you’re always have to worry about not sending test e-mail to the outside world. Alternatively you could setup your own mail server somewhere, but getting everything in place and configured is not always easy.

Lately I started using DevNullSmtp mailserver for developing mail applications. It’s purpose is simple: catch the mail sent to port 25, log the message and destroy it (hence the name DevNull). It has a graphical user interface and also command line options. Btw, you need Java runtime installed on your PC to run DevNullSmtp. The GUI:

smtpdevnull

 

You can see that I use the default SMTP port 25. After sending mails, you can use the four tabs to inspect the mail actions. You can view the SMTP logs for mail instructions or just keep an eye on the last message (like shown above).

You can see that the mail is a multipart mail which contains rich HTML markup. To check the mail in a mail program like Outlook, you have the option to save incoming message in a folder as .eml files:

DevNullSmtp2

This is a very useful options to verify the look and feel of HTML e-mail. I always have this check enabled via a batch file start.cmd :

"C:\Program Files (x86)\Java\jre6\bin\java" -jar DevNullSmtp.jar -p 25 -s emails

Have fun e-mailing,

SSD performance loss

Recently my laptop is getting slower and slower. The startup is pretty fast, but when using Visual Studio and compiling code it slowwww. First I thought that I needed to clean up Windows and check VS extensions, but after a while my SSD drive was my main suspect. After running some tests on my SSD with HD Tune my feeling got confirmed:

64gb_work_ssd

It’s fine when reading data (roughly 190 MB/s average). So that’s what I see when booting up Windows. Writing data is a different story. It tries to be fast, but something is bothering, look at the spikes.. That’s probably causing VS to be slow and my general experience with the machine.

My 64 gig SSD is from the manufacturer DELL and one of the first generation. With another tool I checked if my drive supports the important TRIM command. Luckily it does. Also I checked that there is enough space on the disk (at least 10%) and that’s also not the problem.

ssd128new

Last weekend I bought a brand new SSD from Samsung, 128 GB in size. The 830 series comes in a nice retail package and a free copy of Norton Ghost. Very useful when you want to upgrade your system with this new disk.

 

Time for a comparison. It’s a bit apple and oranges, since the age difference of SSD is like 2 years. And in SSD terms that’s a lot.

 

Here is the HD Tune benchmark for the new Samsung 830 – 128 GB:

128gb_private_ssd

That’s better. Read & write is much higher of course. My point is that write is a flat line when you compare it with my older SSD.

Just for fun, my WD Velicoraptor is showing me this:

WDC_RAPTOR

 

 

 

 

 

 

 

 

 

It’s OK for a old-fashioned disk, compared with my new SSD this disk really is a dinosaur. Probably I could try to low level format my 64 GB SSD to recover some speed that it used to have. First I need to move the data to another disk.

Fun extension for Visual Studio: achievements

Since a few days I have installed the really fun extension: Visual Studio Achievements. It brings the game element of achievements to you IDE. Like earn 5 points for ‘Save a tree’ achievements: 5 points - Print source code. My boss told me to. I swear!

achievelist

See the complete achievement list on the channel 9 site.

You can install it via the extension manager:

achievements
After installing you have to enter your channel9 credentials for keeping track of the score. Directly after some coding you’re notified when achieving something:

VSscreenshot1sm
Have fun coding!

Ps. You can find my achievements on this page.