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?

AdrianSean
  • 397
  • 1
  • 5
  • 21

1 Answers1

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;
    }
}
Rico Suter
  • 11,548
  • 6
  • 67
  • 93
  • 2
    Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Panagiotis Kanavos Jan 13 '15 at 14:13
  • 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() – AdrianSean Jan 13 '15 at 16:06
  • 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 – Rico Suter Jan 13 '15 at 16:17
  • 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? – AdrianSean Jan 13 '15 at 16:21
  • 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) – Rico Suter Jan 13 '15 at 16:22
  • So the difference here with client creation is that if done via an interface then i need to apply a using statement and override Dispose as per above to ensure connection is elegantly handled. If done via Concrete instantiation as opposed to interface i can just call Close & Abort as oer normal. – AdrianSean Jan 13 '15 at 16:33
  • You always need to call the logic or something similar (see link to same question)... It seems that there is no consensus about the right way, but only calling dispose, close or abort is not enough.. – Rico Suter Jan 13 '15 at 17:23