Reputation: 3131
I have a wcf client.
What is the best way of handling connections ?
Is it to open and close every time you need to contact the service:
void doSomething(){
MyService service = new MyService();
//try
service.doThis(10);
...
service.doThat(20);
service.Close()
// catch
}
Or should I keep opened reference and abort it and reinitialize if connection error occurs:
class Myclass{
MyService service = new MyService();
...
void myFunction(){
try{
service.doThis(10);
}catch(...){
service.abort();
service = new Myservice();
// do something here, but what it a smart thing to to?
}
}
}
Regards
Upvotes: 1
Views: 1133
Reputation: 364389
First approach is common. Second approach is completely wrong. The most important hint to your question is: If you close/abort the proxy, you can't use it again. It opens only once.
If you use first approach you create new proxy each time and you "open" new connection. The open here can have different meaning for different bindings and situations. After making the call you gracefully close the proxy. This will also inform the server about closing connection.
Second approach uses similar steps except the last one which forcibly closes the connection. This will not inform the server about connection closing. But you will not be able to reuse unclosed connection on the server.
If you want to reuse proxy you have to left it opened. Than you have to handle some other complexity with timeouts on server (receiveTimeout - by default connection is closed after 10 minute of inactivity) and unhandled exceptions. If you have session based connection or service each unhandled exception will make the communication channel faulted and you will be only able to call Abort on the channel.
Upvotes: 1