I recently needed to implement a shared session between a few different applications, and didn’t find a whole lot on the interwebs about how to do this (I’m starting a second life as a web developer though, it could be that I just don’t know all the terms the cool kids are using). It ended up being a bit easier than I thought it would be, but then again everything is easy when the great folks at LessThanDot’s ASP.net Forum are there to bail me out 😉

Anyway, because I had trouble finding this information I figured it was worth throwing together a blog post to get it all in one place. I’ll try to cover everything here, including the session state installation. In order to test, I set up an ASP.net MVC project (using the beta release), an ASP.net webservice project, and a local SQL Server instance to maintain the session state (all this is running on the one computer for now, so it feels silly, but remember the goal is to eventually get each component onto its own server. I named them “SessionTest.Services” and “SessionTest.Site”. So go ahead and set that up, I’ll still be here I swear.

Wow, you are quick! But you’re just getting started, there’s some more things to do. First, lets install the SQL Server session state on localhost. You’ll need to fire up command prompt to do this and run the command:

C:WindowsMicrosoft.NETFrameworkv2.0.50727aspnet_regsql.exe -ssadd -sstype c -S ALEXCOMPUTER -d ASPState -E

The parameters here mean:

  • ssadd: Add Session State
  • sstype c: Session State type = “custom” (not really, but we need to rename the database)
  • -S ALEXCOMPUTER: Server to install the session state db on. I already have it installed on mine, so you can go ahead and replace this with your computer name. I really appreciate the offer though
  • d ASPState: Name for the database (this is what ASP.net expects)
  • E: Signal for the Session State database to use current windows credentials for authentication (when you deploy to the server, you’ll need to grant access to the service account that ASP.net is running under

It’ll take a minute or so, but eventually it will finish, and it will tell you so. When its’ done, lets mosey on over to Visual Studio.

First thing to do here is add a reference to your class library project to both the webservice and the the website project, so that we can share types. Or type in this case. May as well add that type to the class library now as well.

First thing we want to do is get the MVC project ready. I got rid of everything in the Views folder except for the About and Index pages in Views/Home. I also got rid of the Account controller. Feel free to do this if you want, but I suppose it isn’t necessary. One thing that is necessary, since we want to use jQuery to get the webservice value eventually, is to reference the jQuery script. So just add this to the bottom of the page called Site.Master (before the closing html tag):

Valid XHTML | Valid CSS | LTD Archive