Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting session in .NET ASMX web-service

Tags:

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?

like image 551
Matt Avatar asked Jul 21 '10 08:07

Matt


People also ask

How do I get to session in web method?

To enable the session in a web service it must use the EnableSession Property of the WebMethod attribute. It must set EnableSession to true as shown in the preceding example. Without setting the EnableSession property to true, the session will not work otherwise the unhandled exception occurs.

How do I call Asmx web service?

Adding Reference of the Web Service in Visual Studio 1. Right click the project in Solution Explorer and choose Add Service Reference option from the context menu. 2. Now in the Add Service Reference Dialog you need to click on the Advanced button.

How do you set a session value in WebMethod?

Hi alhakimyyes, First correct your data source property of OracleConnection then add DataBase property to specify the name of database you are using the select query. Use parameterized query.

Can we maintain user session in web services?

By default, web service does not support session state. For achieving high scalability, web service is designed stateless. Suppose the requirement is to use session management in a web service to retain user specific information, you need to use the session in your web service.


1 Answers

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

[WebMethod(EnableSession = true)]
public string checkSession()
{
    return HttpContext.Current.Session.SessionID
}
like image 132
Pietro Allievi Avatar answered Sep 28 '22 06:09

Pietro Allievi