Re: Member variables in Web Service class



Hi,

Mark Ingram wrote:
Hi, if I have the following class:

[WebService(Namespace = "http://www.softease.com/Podium";)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MyWebService : System.Web.Services.WebService
{
private String m_name;

[WebMethod]
public String SayHello(String name)
{
m_name = name;
return "Hello " + m_name;
}

[WebMethod]
public String SayGoodbye()
{
return "Goodbye " + m_name;
}
}

That won't work will it? Because WebService's are stateless? Even though I keep an instance of the web service on my client.

So how can I store information, such as a username? I know I can use the Session object, but I was lead to believe that it isn't best to use the Session object in web services??


Any help is much appreciated.

Cheers,

You are correct, it's exactly like with Pages (ASPX files with code behind), a new instance is created on every Request. Member variables will be discarded together with the whole class when the Response has been sent.

You are also correct that you can access the Session object in web services, but you must mark the web method with
[WebMethod( enableSession=true )]

Otherwise, the session will not be enabled.

Also, make sure that the Global.asax file is present in your project, or else the session management is not done correctly. In ASP.NET 1.1, Global.asax was added per default to each new web app (web service or web site). In ASP.NET 2.0, it's not anymore, but you can add one manually.

See
http://geekswithblogs.net/lbugnion/archive/2006/10/11/93737.aspx

In a page (ASPX), you can also use the enablesessionstate in the Page directive, but I am not sure if you can also do this in a web service. Adding the Global.asax should be a better choice in that case.

There is nothing wrong with using Session in web services, as long as you are aware of the cost (performance, memory, etc...). In fact, more and more I tend to believe that making a stateless web application (as opposed to a website) is almost not possible. Stateless web sites, in the contrary, are not a problem.

Greetings,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
.



Relevant Pages

  • Member variables in Web Service class
    ... [WebMethod] ... public String SayGoodbye() ... Even though I keep an instance of the web service on my client. ... I know I can use the Session object, but I was lead to believe that it isn't best to use the ...
    (microsoft.public.dotnet.framework.aspnet)
  • returning more than first element from sql server
    ... IM AM NEW TO WEB SERVICE AND C# AND HAVE A QUESTION / PROBLEM ... [WebMethod] ... public string logtime(string ClientUserName) ...
    (microsoft.public.dotnet.framework.aspnet.webservices)
  • RE: Outlook 2007 Mobile Service - GetUserInfo and SendXms input pa
    ... with xmsUser parameter still being null ... Outlook displays the following error message: ... response from the Web service because of a problem connecting to the server. ... public string GetServiceInfo() ...
    (microsoft.public.office.developer.outlook.vba)
  • RE: Outlook 2007 Mobile Service - GetUserInfo and SendXms input pa
    ... I've created a screencast showing how to set up IIS7 and create an Outlook ... with xmsUser parameter still being null ... response from the Web service because of a problem connecting to the server. ... public string GetServiceInfo() ...
    (microsoft.public.office.developer.outlook.vba)
  • Re: AJAX - AutoCompleteExtender ASP.Net 2.0 (Page Method vs. Web S
    ... [WebMethod] ... [ScriptMethod] ... In aspx site: ... I want to put the web service code into a page ...
    (microsoft.public.dotnet.general)