0

I would like some clarity around the following (apologies in advance if this is a stupid question)

Am working on some existing code which calls a WCF service.

This code instantiates a WCF service client via an interface and performs the operations it needs i.e.:

IWCFService proxyClient = new WCFServiceClient() 

However what am trying to do here is ensure the connection is closed gracefully i.e.: proxyClient.Close() etc but I cant access these seeing as its created via an interface (which just houses the operations i.e.: DoSomething())

If i instantiate as a WCF service client (and not via interface) i will be able to access the Close() & Abort calls which i can use in try{}catch{} blocks. i.e.:

WCFServiceClient proxyClient = new WCFServiceClient() //do some stuff.. proxyClient.Close() 

Is it a simple case of adding Close() & Abort() to the interface definition and then calling these in the code which should in turn implement the WCF implementations of these?

4
  • Dispose calls Close internally. Just put the proxy inside a using block Commented Jan 13, 2015 at 14:11
  • @PanagiotisKanavos Only that if the communication fails the call to Close throw an exception and Abort must be used. Commented Jan 13, 2015 at 14:12
  • the use of the using() statement is not considerd good practice for WCF clients as they can return an exception in the Close() operation Commented Jan 13, 2015 at 14:13
  • That's true, I commented before noticing the OP is trying to close gracefully. Commented Jan 13, 2015 at 14:16

1 Answer 1

0

I recently wrote an article about the correct handling of a WCF client's life cycle: Only wrapping the instantiation in a using block is not sufficient...

Have a look at http://blog.rsuter.com/?p=975

Summary: Overload Dispose the following way to use the client with the using keyword:

public void Dispose() { if (isDisposed) return; try { if (service.State == CommunicationState.Faulted) service.Abort(); else { try { service.Close(); } catch (Exception closeException) { try { service.Abort(); } catch (Exception abortException) { throw new AggregateException(closeException, abortException); } throw; } } } finally { isDisposed = true; } } 
Sign up to request clarification or add additional context in comments.

7 Comments

Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
i understand the above logic - however the service client is instantiated via an interface (written by one of the team which exposes a couple of business operations) and does not have access to the .Close() and .Abort() functions. Is it better to instanstiate it in the normal way so i can access these methods? i.e.: WCFClientProxy clientProxy = new WCFClientProxy()
You need to check if Dispose is correctly implemented (only calling Close is not enough); it's up to you how to instantiate the proxy object
right - i can see that Dispose is implemented on the WCF service itself (setting some objects to NULL), is my understanding right that on the server side WCF calls this automatically when it is done with the request?
The client side and server side implementation are two different things, you need to check that both sides are implemented correctly (the mentioned problematic applies only for the client side)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.