views:

77

answers:

3

I have a class library (c#) with many methods that call the same web service (asmx).

What is the best practice for instantiating the web service.

  • Instantiate the web service once and pass it as a parameter to each method
  • Or instantiate and dispose the web service in each method.
+5  A: 

What you are instantiating is a local proxy class that calls the service, so it isn't as costly as you may think.

As web services are supposed to be stateless, either method would work. I doubt you will see much of a difference in performance.

Oded
A: 

Thank you for your answer, it makes perfect sense. I will instantiate and dispose of the web service inside each method.

Apologies for posting the same question twice.

chrystad
You can post comments to an answer as well, instead of posting your response to my answer as a new answer...
Oded
A: 

It seams like a bad practice to make a new instance of the service, hook up complete events each time you have to call a service method

usually i make an instance variable and then instantiate the service in the constructor and hook up all complete events there

and only call the methods when needed this approach works well, except if you do it in an User-control it breaks the Visual Studio Designer

LiFo