views:

283

answers:

3

Hi, I'm writing an ASP.Net MVC application using Jquery to post some data to my users session.

A user 1st accesses my site's page and then I have a button which performs a post using JQuery to an MVC controller method to store some data into the users session.

When the initial page loads I create a session object for my user using a session factory. This works fine. As part of the initial request to my page I put an object called JoinState into my session - this must be in place for any subsequent requests.

When the user clicks the button on the page, a JQuery post is performed to add data to the JoinState object. This posts to an MVC controller method which accesses the already defined session and updates the JoinState object with the posted data.

However, when the post takes places, my application does not recognise the initial session that was created. This leads to an "Object not set to an instance...." (basically a null reference exception) as the already defined JoinState object now does not exist.

I just cannot work out why this is happening. What I have worked out so far is as follows:

  • It only affects users who are accessing the site from outside my network.
  • Users inside my network are not affected.
  • The url name is a dynamic dns name - could this affect how .net sees the session?
  • I've used firebug to debug the POST request and can confirm that the ASP.Net session cookie is being sent.

For some reason, in my JQuery post my original session is not recognised.

Thanks in advance.

A: 

I'm not sure about this but I think the jQuery post is like calling a web service and as such won't have a reference to your object.

You'll need to pass back the session key or something so you can load it and then add to it.

All this is off the top of my head and totally untested.

If i'm wrong let me know and I'll do some testing and maybe remove this answer so as not to confuse the issue.

griegs
Possibly, but the I sort of though that it might replicate a standard form post. It seems to send all of the session identification (e.g. cookies). Ill try changing it to a GET request later to see if that makes a difference. The only other thing I can think of is that the POST url is getting modified somehow, or as its running through a dynamic dns its getting modified causing ASP.Net to not find the original session.
RemotecUk
A: 

If the url name is dynamic, the session cookie issued by ASP.Net wont be valid when the url name changes so you lose the connection to the existing session. You could configure your website for cookie-less sessions, but you'll have to check if you need to manually append the session key query string variable or not.

AUSteve
A: 

Well,,,,,, problem now solved.

  • Nothing to do with MVC.
  • Nothing to do with JQuery
  • Everything to do with me not fully understanding session state in asp.net.

Facts...

  • Worked on local Web Dev server.
  • Application moved to IIS and it stopped working.

Findings...

  • InProc session was being used.
  • Worker processes above 1 had been set in IIS.

Everytime a request was taking place it would hit a new worker process. Because the session was being stored InProc, or in the memory of each individual process, each time a request got a new WP it got a new session.

Final Conclusion....

  • Dont mess with IIS settings you dont understand!!!

Worker Processes now set back to 1 - application working.

Time for bed - thanks to everyone who responded.

RemotecUk