Groups | Blog | Home
all groups > dotnet web services > march 2008 >

dotnet web services : Preserving static variables in web services.


biztalk2006
3/16/2008 4:28:01 PM
I have a static variable in my class that contains a socket connection.
This class is called from a webservice. I donot create a socket for each
request .
the socket is created and connected to the target address for the first
request.The subsequent requests each utilize the
same socket.
I have utilized mutexes around the static variable for any operations.The
class which contains the socket variable is made
singleton.
The problems that I am currently facing is that the static socket variable
is getting lost after about
30 minutes of inactivity.
The network is not causing this problem. since this is executed under aspnet
worker process and
I have a feeling that the aspnet worker process is causing this problem.
the question is how can I preserve static variables in aspnet worker process.
The webservice is hosted under IIS 5.0 and is made as a separte application
biztalk2006
3/17/2008 5:51:02 AM
Hello John
Thanks for the quick reply. Is there any event or code that I can write in
my webservice to close the socket gracefully when the appdomain is being
destroyed?
I would like the socket to be closed because there is a limit on the number
or sockets that I can open to the remote system.
Thanks


[quoted text, click to view]
John Saunders [MVP]
3/17/2008 7:12:56 AM
[quoted text, click to view]

Static variables are scoped to the AppDomain in which they are created. When
the AppDomain id destroyed, the variables are also destroyed.

Your best option is to not depend on the variable always being initialized.
If you find that the socket is null, reinitialize it.
--
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer

biztalk2006
3/17/2008 5:17:02 PM
Hello John
After careful analysis, I have come to the conclusion that the value in my
static variable is preserved but the socket is becoming inactive meaning I am
not able to send any data over the socket after 30 minutes of inactivity. It
shows as established when I run netstat -na but when I tried to receive any
data it throws me an exception saying that the remote host has forcibly
closed the connection. This leads me to believe that tcp sockets have an idle
time out . do you have any increasing the idletime out of tcp sockets or
keeping alive the socket. I was reading on keep alive but it can not be used
for client sockets. Let me know if you have any information on how to
increase the idle time out of the socket

[quoted text, click to view]
John Saunders [MVP]
3/17/2008 9:24:43 PM
[quoted text, click to view]

Actually, I think you're making a mistake with this approach, period.

A web service is not the same thing as a Windows Service. It's not something
that starts up when the system starts, and just keeps running. You should
not expect your socket, or anything else, to just hang around in memory
and/or the process, waiting for something to happen.

You should create a simple Windows Service for this, and connect the two via
..NET Remoting, or WCF. The service won't have any problem with the socket
closing, as the service will always be running.
--
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer

AddThis Social Bookmark Button