Closing WCF connections using static / centralized class
I want to start implementing a better solution for closing my WCF connections throughout my code, and also handle any exception issues during this process. I plan to implement the solution I found here , but instead of duplicating this across my classes, I would like to write a static class with which I can send open connections to close and handle exceptions, for example:
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 ran into the problem is that I have many types of client clients and I'm not sure what the base class is, I have to target the WCFManager.CloseConnection () method. Each client of the service seems to be a unique class and I cannot 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 would like to avoid creating an override of WCFManager.CloseConnection () for each client type, but is this the only option I have?
2: Is this even a good option, or will it be connected, cause more potential problems?
3 .. Since I am loading my WCF server load through 2-4 servers, close the connection every time it is best used, or create a static link to each ServiceClient when the best scenario (I'm sure it isn't, but would love a second opinion about it!)
FYI: I am using NetTcpBinding and add ServiceReferences to explorer for solution.
Thank!
source to share
1: I would like to avoid creating an override of WCFManager.CloseConnection () for each type of client service, but is this the only option I have?
All WCF proxies inherit from ICommunicationObject
. It is actually an interface that defines the Abort () and Close () methods. To call your method, always append to first ICommunicationObject
.
Also a small suggestion: what you are doing works even better as an extension method ICommunicationObject
. Then it becomes
((ICommunicationObject)alphaServiceClient).CloseConnection();
2: Is this even a good option, or will passing the connection around cause more potential problems?
This is a helper method. He is unlikely to be "bypassed". Perfectly.
- Since I am loading my WCF server load through 2-4 servers, it closes the connection every time the best case is used
Yes. Use the connection and close it.
source to share