Member variables in Web Service class

M

Mark Ingram

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,
 
L

LosManos

hejdig.

The buzz word of today is "stateless". So most people don't want states in
their web services.

Another thing is that web services are slower than "ordinary calls" and
statefull object tend to be more "chatty". So if you have a web service
with a state you might be tempted to use it as an ordinary business object
with lots of calls and that might affect performance.

Otherwise there is nothing wrong with haveing states in you web services.

HTH

/OF
 
M

Mark Ingram

LosManos said:
hejdig.

The buzz word of today is "stateless". So most people don't want states in
their web services.

I'm not concerned with using states, I need them in this instance (I am
writing a file upload web service, with functions for starting the
transfer, appending chunks of data and finishing the transfer). I am
more concerned with the usage of the HttpSessionState and the impact it
will have on the speed of the web service. From what i've read it should
be avoided?
 
L

Laurent Bugnion

Hi,

Mark said:
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
 
M

Mark Ingram

Laurent said:
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

Thanks Laurent.

Is there a ratio that defines *how* much slower a web service will run
with sessions enabled? I can't imagine it would make too much of a
difference
 
L

Laurent Bugnion

Hi,

Mark said:
Thanks Laurent.

Is there a ratio that defines *how* much slower a web service will run
with sessions enabled? I can't imagine it would make too much of a
difference

Never heard of that ;-) Anyway as you can imagine, it will depend much
on the nature of the saved objects, on the frequency with which they
will be accessed, etc... Besides, there are many other places on the
line which will affect the speed with which the page is rendered.

As a rule of thumb, I would keep that in mind, and start by choosing the
easy way (using the session) but with a layering such that you can
easily choose another way to persist your objects if speed becomes an issue.

HTH,
Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top