I am new to WCF and am interested what is the best practice to call a service method. The application will consist in a bunch of forms and I would like to know if it's better to declare a global client instance for each form and then just call the methods when needed. Or is it better to instantiate the client proxy before each method call and close it right after.
Asked
Active
Viewed 357 times
2 Answers
0
I believe creating a global var of client for each form will do for you, no need to instantiate service each time before calling the service method.
public MyService ser {get; set;}
Inside class constructor.
ser = new MyService();

Saket Choubey
- 916
- 6
- 11
0
The most simple and safest way is constructing client proxy every time you use it. The drawback of this approach is loosing perfomance, but depending on your binding (http, net.tcp, etc) and service mode (PerCall, Statefull, Singleton) you will not notice the difference (see this answer WCF Proxy Pooling - Is it worth it?).
If you create a proxy on the form level, when this proxy is in the faulted state (because of the connection problems), you won't be able to reuse it and will have to reopen form.