By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,148 Members | 759 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,148 IT Pros & Developers. It's quick & easy.

Single Instance web service??

P: n/a
Is there such a thing as a single instance web service? Is it possible to
have all calls to a web service share one property value by declaring it as
static? I know web service should be stateless, but I am still trying to
understand how instances of a web service are handled at the server side, if
a unique instance is created for every call then should'nt any members
declared as "static" be shared between instances?

Any help is appreciated.
Sep 14 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"aesper" <ae****@discussions.microsoft.comwrote in message
news:2F**********************************@microsof t.com...
Is there such a thing as a single instance web service? Is it possible to
have all calls to a web service share one property value by declaring it
as
static? I know web service should be stateless, but I am still trying to
understand how instances of a web service are handled at the server side,
if
a unique instance is created for every call then should'nt any members
declared as "static" be shared between instances?
A single instance is created per call. Anything declared static will be
shared across all calls within the same AppDomain. More or less, that means
on the same machine and same virtual directory.

Using statics is a bad idea in general in a web service or other ASP.NET
application. This is because each request runs on a separate thread, and
more than one request can be serviced at a time. This implies that more than
one thread can be accessing your statics at the same time.

If at all possible, you should try to make the service stateless.
--
--------------------------------------------------------------------------------
John Saunders | MVP – Windows Server System – Connected System Developer

Sep 14 '07 #2

P: n/a
Thanks for the response.
I will make my web service stateless, but I still would like to know how
application domains are managed for a web service. Is there a way we can
ensure that there is always one application domain for the web service. I ran
a small test and it looks like there are alwasy 5 different instances of the
"static" which suggest that there are 5 different App Domains, does this
sound right?

Thanks agin for your help.

"John Saunders [MVP]" wrote:
"aesper" <ae****@discussions.microsoft.comwrote in message
news:2F**********************************@microsof t.com...
Is there such a thing as a single instance web service? Is it possible to
have all calls to a web service share one property value by declaring it
as
static? I know web service should be stateless, but I am still trying to
understand how instances of a web service are handled at the server side,
if
a unique instance is created for every call then should'nt any members
declared as "static" be shared between instances?

A single instance is created per call. Anything declared static will be
shared across all calls within the same AppDomain. More or less, that means
on the same machine and same virtual directory.

Using statics is a bad idea in general in a web service or other ASP.NET
application. This is because each request runs on a separate thread, and
more than one request can be serviced at a time. This implies that more than
one thread can be accessing your statics at the same time.

If at all possible, you should try to make the service stateless.
--
--------------------------------------------------------------------------------
John Saunders | MVP – Windows Server System – Connected System Developer

Sep 14 '07 #3

P: n/a
"aesper" <ae****@discussions.microsoft.comwrote in message
news:3F**********************************@microsof t.com...
Thanks for the response.
I will make my web service stateless, but I still would like to know how
application domains are managed for a web service. Is there a way we can
ensure that there is always one application domain for the web service. I
ran
a small test and it looks like there are alwasy 5 different instances of
the
"static" which suggest that there are 5 different App Domains, does this
sound right?
No, it doesn't sound right. How did you determine the number of instances of
the static?

The following applies to any ASP.NET application, including an ASP.NET web
service:

The first time a request comes in for the virtual directory, ASP.NET will
see that it does not have an instance of the application running. It will
start such an instance in a new AppDomain. This AppDomain will continue to
exist, containing each instance of the web service, until it "resets". This
can be caused by several actions, including changing files in the bin
folder, or changing the web.config. There are others as well.

Basically, you don't have to worry much about it if you don't use statics,
and don't assume that Application or Session state will always be there. The
latter two could be empty if the AppDomain shut down and you are now running
in a new AppDomain.
--
--------------------------------------------------------------------------------
John Saunders | MVP – Windows Server System – Connected System Developer

Sep 14 '07 #4

P: n/a
Basically what I did is generate a Guid every time the static memebr is
instantiated:

public class Counter
{
private int _value = 0;
private Guid _guid;

public Counter()
{
_value = 0;
_guid = Guid.NewGuid();
}

public string GetNextCounter()
{
_value++;
return _guid.ToString() + " " + _value.ToString();
}
}
Then my web service part looks like this:

[WebService(Namespace = "http://MyCompany.WebServices/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class TestCounterService : System.Web.Services.WebService
{
#region Private Memebers
private static Counter _counter = null;
private static readonly object _lockObject = new object();
#endregion

#region Constructor
public TestCounterService()
{
}
#endregion

[WebMethod]
public string GetNextCounter()
{
string result = string.Empty;
lock (_lockObject)
{
if (_counter == null)
_counter = new Counter();

result = _counter.GetNextCounter();
}

return result;
}

}

When I call the [WebMethod] from different clients I get the Guid which
should correspond to the Counter instance and its corresponding counter, I
launch many instances of the client application and the Guids can always be
grouped in groups of five unique Guids......

Thanks again for all your help!
"John Saunders [MVP]" wrote:
"aesper" <ae****@discussions.microsoft.comwrote in message
news:3F**********************************@microsof t.com...
Thanks for the response.
I will make my web service stateless, but I still would like to know how
application domains are managed for a web service. Is there a way we can
ensure that there is always one application domain for the web service. I
ran
a small test and it looks like there are alwasy 5 different instances of
the
"static" which suggest that there are 5 different App Domains, does this
sound right?

No, it doesn't sound right. How did you determine the number of instances of
the static?

The following applies to any ASP.NET application, including an ASP.NET web
service:

The first time a request comes in for the virtual directory, ASP.NET will
see that it does not have an instance of the application running. It will
start such an instance in a new AppDomain. This AppDomain will continue to
exist, containing each instance of the web service, until it "resets". This
can be caused by several actions, including changing files in the bin
folder, or changing the web.config. There are others as well.

Basically, you don't have to worry much about it if you don't use statics,
and don't assume that Application or Session state will always be there. The
latter two could be empty if the AppDomain shut down and you are now running
in a new AppDomain.
--
--------------------------------------------------------------------------------
John Saunders | MVP – Windows Server System – Connected System Developer

Sep 14 '07 #5

P: n/a
"aesper" <ae****@discussions.microsoft.comwrote in message
news:F9**********************************@microsof t.com...
Basically what I did is generate a Guid every time the static memebr is
instantiated:
....
When I call the [WebMethod] from different clients I get the Guid which
should correspond to the Counter instance and its corresponding counter, I
launch many instances of the client application and the Guids can always
be
grouped in groups of five unique Guids......
Were there five instances of the client application?

You also might consider saving DateTime.Today in a static when instantiated,
and displaying that in the web service response as well.

--
--------------------------------------------------------------------------------
John Saunders | MVP – Windows Server System – Connected System Developer

Sep 15 '07 #6

P: n/a
No, actually there were many more intances of the client application, I
basically have a button and an edit box. When the button is clicked the web
service is called and the result is displayed in the text box. I just keep
hitting the buttons on all instances and I always have 5 unique Guids no
matter how many client instances are running, I tried up to 100 instances.

I will try to display the time stamp and see if the results are consistent
with what I am concluding.

Thanks agin.

"John Saunders [MVP]" wrote:
"aesper" <ae****@discussions.microsoft.comwrote in message
news:F9**********************************@microsof t.com...
Basically what I did is generate a Guid every time the static memebr is
instantiated:
....
When I call the [WebMethod] from different clients I get the Guid which
should correspond to the Counter instance and its corresponding counter, I
launch many instances of the client application and the Guids can always
be
grouped in groups of five unique Guids......

Were there five instances of the client application?

You also might consider saving DateTime.Today in a static when instantiated,
and displaying that in the web service response as well.

--
--------------------------------------------------------------------------------
John Saunders | MVP – Windows Server System – Connected System Developer

Sep 15 '07 #7

P: n/a
I added the DateTime.Now.ToString() to the web service output to indicate
when the static was instantiated. The result is the same: There are always 5
unique time stamp/Guid combinations when several instances of a client are
run. I am just wondering if there is a way to control how many app domains
are created for the web service.

"aesper" wrote:
No, actually there were many more intances of the client application, I
basically have a button and an edit box. When the button is clicked the web
service is called and the result is displayed in the text box. I just keep
hitting the buttons on all instances and I always have 5 unique Guids no
matter how many client instances are running, I tried up to 100 instances.

I will try to display the time stamp and see if the results are consistent
with what I am concluding.

Thanks agin.

"John Saunders [MVP]" wrote:
"aesper" <ae****@discussions.microsoft.comwrote in message
news:F9**********************************@microsof t.com...
Basically what I did is generate a Guid every time the static memebr is
instantiated:
....
When I call the [WebMethod] from different clients I get the Guid which
should correspond to the Counter instance and its corresponding counter, I
launch many instances of the client application and the Guids can always
be
grouped in groups of five unique Guids......
Were there five instances of the client application?

You also might consider saving DateTime.Today in a static when instantiated,
and displaying that in the web service response as well.

--
--------------------------------------------------------------------------------
John Saunders | MVP – Windows Server System – Connected System Developer
Sep 17 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.