23

I have an ASMX webservice hosted alongside my ASP.NET web app. Now, I need to get the users session into the Webservice. To test this I made this simple method:

    [WebMethod(EnableSession = true)]
    public string checkSession()
    {
        return HttpContext.Current.Session["userid"].ToString();
    }

So, first I login to my web app, then in the browser goto my webservice and click "checkSession" on that auto generated test page. I have tested this on 3 computers. All 3 of those work fine with the webapp(so the sessions are being created etc), and 2 of those return the value of Session["userid"] on invoking the webmethod, however the last computer returns "Object reference not set to an instance of an object" because Session is null.

So, whats the difference between these computers and why can my ASP.NET app get the sessions on all computers but the webservice cant?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Matt
  • 1,562
  • 2
  • 17
  • 27
  • 3
    How are you storing your session? If in-proc you need the asmx webservice to run on the same appPool as the webapp. And are you getting the session error for that one machine all the time? – Mikael Svenson Jul 21 '10 at 09:03
  • I dont actually know. All im doing is that when they initially login on the ASP.NET side I set: Session["userid"] = theiridfromdb; Firstly, how do I check if its in-proc, and then how would I run the webservice in the same appPool? Yes, that computer is always giving the error. – Matt Jul 21 '10 at 09:21
  • It looks like inproc is the default, so yes, I am using that. I am running this off a dedicated server running IIS6, and the ASP.NET site and the webservice are one application. As in, they are the same application in IIS and Visual Studio compiles both of their backend to the same DLL. – Matt Jul 21 '10 at 09:30
  • See this post: Session enabled web services - http://www.devhood.com/messages/message_view-2.aspx?thread_id=50261 – Markive Jul 21 '10 at 15:08
  • Nope, didnt help :-/ I have now tested this one my cellphone too, and It works there. So what could it be about that one computer? – Matt Jul 21 '10 at 17:57
  • Start with tracing cookies, the sessionid is stored in a cookie. If for some reason the cookie is missing, you don't have the session. – Wiktor Zychla Aug 19 '12 at 10:41
  • 2
    On a different note - you shouldn't be calling ToString() on an object which could be null - Should check for null first. – Ashish Gupta Sep 09 '12 at 15:12

8 Answers8

18

maybe it's too late, but have you tried this:

[WebMethod(EnableSession = true)]
public string checkSession()
{
    return HttpContext.Current.Session.SessionID
}
Pietro Allievi
  • 386
  • 6
  • 14
  • 3
    This is exactly what I needed to know and it fixed my null Session issue. I just put [WebMethod(EnableSession = true)] on my method, which returns other data. Thank you! – Daniel Williams Jul 24 '17 at 23:08
3

SessionIDs are stored as cookies on the client's browser by default (Session State Overview). So check if that cookie is being created properly on that problem computer. Maybe cookies are disabled for some reason? In that case it would not be sending the SessionID to the server when you are hitting that web service.

kukabuka
  • 91
  • 1
  • 5
3

Can you check how many worker processes is your application using? You can check it in your App pool settings in IIS.

If more than one worker process are being used then it is called to be running a web garden. If that is the case then in proc session will not be useful as this session is not shared among the worker processes. Have a look at http://www.west-wind.com/weblog/posts/2005/Apr/20/Why-you-shouldnt-use-InProc-Session-State-in-ASPNET

dicemaster
  • 1,203
  • 10
  • 22
1

Have you got Session disabled in IIS, this would over rule .net.

Have a look at this http://technet.microsoft.com/en-us/library/cc732964(v=ws.10).aspx - it tells you how to disable session, but shows where to check the setting.

Thanks Fran

Fran Hoey
  • 895
  • 9
  • 18
0

By default web services are and should be stateless. However if you must use session information be sure to use the marker interfaces IReadOnlySessionState and IRequiresSessionState in order to access, use, or modify session state information in the webservice.

DRobertE
  • 3,478
  • 3
  • 26
  • 43
0

Web service by default wont support Session. You need to explicitly specify the parameter in web method attribute

These two things work for me

  1. <add name="Session" type="System.Web.SessionState.SessionStateModule"/> under <httpModules>

  2. enableSessionState="true" in <page> tag

Amicable
  • 3,115
  • 3
  • 49
  • 77
panky sharma
  • 2,029
  • 28
  • 45
0

A session object is accessible by:

var session = this.Session;
var contextSession = this.Context.Session;
IHAFURR
  • 111
  • 4
-1

best way to do this approach check before your session is not null and in the other side you initialize the selected session with your value and then when and where web service working with httpcontext.current.session have value !

web services are stateless and best way is get value in web service instead of set or initialize session value

soheil bijavar
  • 1,213
  • 2
  • 10
  • 18