Kernel of the problem : at the end of your using block (which is usually a very good idea!), The WCF proxy will be deleted. However, during the removal of the WCF proxy server, exceptions may occur - and this can lead to incorrect application operation. Since this is done implicitly at the end of the using block, you cannot even see where the error occurs.
Typically, Microsoft recommends a pattern like this:
private ServiceClient proxy; try { proxy = new ServiceClient("ConfigName", "http://serviceaddress//service.svc"); string result = proxy.Method(); proxy.Close(); } catch (CommunicationException e) { // possibly log error, possibly clean up proxy.Abort(); } catch (TimeoutException e) { // possibly log error, possibly clean up proxy.Abort(); } catch (Exception e) { // possibly log error, possibly clean up proxy.Abort(); throw; }
You must explicitly call the proxy.Close() method and be prepared to handle any exceptions that may arise from this call.
marc_s
source share