tags:

views:

298

answers:

2

Hello there,

I have added reference to WCF Service in my client asp.net website.

Right now, I am just instantiating WCF Service at every service method call as:

TemplateServiceClient objTemplateService = new TemplateServiceClient();
objTemplateService.MethodCall();

I am not sure about the performance down due to above.

If it is going to be a performance hit, Can you suggest me a better approach to call my service methods.

Thank you!

+2  A: 

The only way to know about performance is to test it, so if you've any concerns, you should do that.

It's hard to know without knowing what you're doing and how it will be used.

A web service client is just another object so you can do all the usual things:

  • what you're doing with a new instance every time;
  • reuse the object if the service calls are in the same method;
  • create the object as a field within your class;
  • singleton

Personally, I tend to end up with the second for most things I do but that fits my usage profile.

serialhobbyist
thanks for your reply. I have created a class level object. But because I am calling i am making service calls in different methods, I need to instantiate it in every method. Any work around to avoid that?
inutan
I think the question should be: do you need to? Think about your maintenance. If you create a new instance of the object each time, you can be reasonably sure that it's in the right state and hasn't been changed by some other method. I guess it depends on how things are set up where you are but say some person comes along in six months and makes a change that impacts on the web service but doesn't check every method - she could introduce bugs all over.
serialhobbyist
You could create some horrid global/static/singleton thing and access it from everywhere but should you? Probably not. Performance is probably a primary concern but if it's not an issue, maintenance should be right up there.
serialhobbyist
just to add something useful, to avoid having timeout error on your WCF service, always close the service client after calling methods as objTemplateServiceClient.Close();
inutan
Good one - +1 for that.
serialhobbyist
A: 

As long as the performance is acceptable, it's not a problem. Don't over-optimize prematurely without even knowing whether there indeed IS a performance problem or not...

Measure your performance, and see, if it is.

The per-call scenario is indeed the preferred and recommended scenario for WCF - it just works best that way, no hairy mess with stateful services, sessions or singleton - it just works and scales quite well to even fairly heavy loads.

marc_s