views:

179

answers:

4

A simple question, yet I wasn't able to find an answer in google.

How often is a web service class instanciated? Is it once per call to the web service, is it once per (arbitrary period of time), is it once per something else?

EDIT

I was hoping the class lives a good long live but seems like it does not.

The point is, I've got a resource which is used in serving requests, and loading this resource each time anew is not an option (on contrary, loading it once IS an option and is very good performace- and other-wise, and just simply 'wise'). (No, nobody will get upset, the resource does not get blocked or something.)

So how do I have an object within a web service, single instance of which I can use to serve all requests? Sorry if question has a too obvious answer like "use a static variable." Being an experinced programmer, I still haven't been properly introduced to web programming and still struggle with some general concepts.

A: 

Once you created the web service client, you can call as many of its methods as much as you want, without creating a new one.

So, it is not necessary to create a new client instance for each call. Although you could of course. Nothing that stops you.

For example, if you're consuming a web service from inside different methods of another web service, I think it's better to create a new web service client inside each method. (Due to the nature of web services, it's possible that there's a lot of time in between web service calls.)

OTOH, if you're consuming a web service from inside a windows client application, you can easily create one global instance of the web service client and always use that one to call the web service.

fretje
+1  A: 

Do you talking about client or server side. My answer below is about server instances, but answer of @fretje relates to client side.

If we talking about class derived from System.Web.Services.WebService you should think about it as each-time-incarnating class.

If we talking about class derived from IHttpHandler, then you have option IsReusable

Dewfy
Yes, I am talking about server side. I think I better edit the question then.
GSerg
A: 

Something you may find useful is the wsdl.exe tool that comes with the .net framework. You can find some samples/documentation for it here: http://msdn.microsoft.com/en-us/library/7h3ystb6%28VS.80%29.aspx

Basically, if you use this tool in the following format;

wsdl /language:VB /out:myProxyClass.vb http://hostServer/WebserviceRoot/WebServiceName.asmx?WSDL

You will be able to create a proxy class file which you can actually see everything that is going in/out of your system and perform audits, logs, etc. Hope this helps.

Kyle B.
A: 

So.

With all default settings, static variables of the web service class, as it would be in a regular desktop application, are only initialized once. This is true even if nobody calls the web service in a long time. You have to restart the app in IIS manager to have them re-initialized. Which is great and exactly what I wanted. So the answer is as simple as "use a static variable."

GSerg