Re: Member variables in Web Service class
- From: Laurent Bugnion <galasoft-lb@xxxxxxxxxx>
- Date: Fri, 27 Oct 2006 17:09:55 +0200
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
.
- References:
- Member variables in Web Service class
- From: Mark Ingram
- Member variables in Web Service class
- Prev by Date: ASP.NET question. How do I set the page size?
- Next by Date: Re: Open Outlook using C#
- Previous by thread: Re: Member variables in Web Service class
- Next by thread: Web Site Manager: irritating data
- Index(es):
Relevant Pages
|