Closing WCF connections using a static/centralized class

382 views Asked by At

I want to begin implementing a better solution for closing my WCF connections throughout my code, as well as handle any issues with exceptions during that process. I plan on implementing the solution found here but rather than duplicate this across my classes I'd like to write a static class that I can send my open connections to for closure and exception handling, like so:

public static class WCFManager
{
    public static void CloseConnection(ServiceClient serviceClient)
    {
        try
        {
            serviceClient.Close();
        }
        catch (CommunicationException e)
        {
            var error = e.Message;
            serviceClient.Abort();
            //TODO: Log error for communication exception
        }
        catch (TimeoutException e)
        {
            var error = e.Message;
            serviceClient.Abort();
            //TODO: Log error for timeout exception
        }
        catch (Exception e)
        {
            var error = e.Message;
            serviceClient.Abort();
            //TODO: Log error for exception
        }
    }
}

Where I am running into an issue is in the fact that I have many service client types and I am not sure what the base class is I should target for the WCFManager.CloseConnection() method to accept. Each service client seems to be a unique class and I can't find a suitable interface or base class among them. For example:

//Inside Class1:
var alphaServiceClient = new AlphaService.AlphaServiceClient();
alphaServiceClient.Open();
WCFManager.CloseConnection(alphaServiceClient); //<-- Requires AlphaServiceClient type

//Inside Class2:
var betaServiceClient = new BetaService.BetaServiceClient();
betaServiceClient.Open();
WCFManager.CloseConnection(betaServiceClient); //<-- Requires BetaServiceClient type

Questions:

1: I'd like to avoid creating an override of WCFManager.CloseConnection() for each service client type, but is this the only option I have?

2: Is this even a good option, or will passing the connection around cause more potential issues?

3. Since I am load balancing my WCF server across 2-4 servers, is closing the connection each time it is used the best option, or is creating a static reference to each ServiceClient once a better scenario (I am pretty sure it is not, but would love a second opinion on this!)

FYI: I'm using NetTcpBinding and am adding ServiceReferences within solution explorer.

Thank you!

1

There are 1 answers

1
ErnieL On BEST ANSWER

1: I'd like to avoid creating an override of WCFManager.CloseConnection() for each service client type, but is this the only option I have?

All WCF proxies inherit from ICommunicationObject. That is in fact the interface that defines the Abort() and Close() methods. In order to call your method, always cast to ICommunicationObject first.

Also minor suggestion: What you are doing works even better as an extension method of ICommunicationObject. Then it becomes

((ICommunicationObject)alphaServiceClient).CloseConnection();

2: Is this even a good option, or will passing the connection around cause more potential issues?

This is a helper method. Its hardly "passing around the connection". Its fine.

  1. Since I am load balancing my WCF server across 2-4 servers, is closing the connection each time it is used the best option

Yes. Use the connection and close it.