110,538
社区成员
发帖
与我相关
我的任务
分享
public class Client<T> : IDisposable
{
private readonly T client;
public Client()
{
var factory = new ChannelFactory<T>();
client = factory.CreateChannel();
}
public void Dispose()
{
if (disposed) return;
try
{
var c = client as ICommunicationObject;
if (c != null)
{
CloseConnection(c);
}
}
catch (Exception ex)
{
Debug.WriteLine("Client dispose error:" + ex.Message);
}
disposed = true;
}
public static void CloseConnection(ICommunicationObject myServiceClient)
{
if (myServiceClient.State != CommunicationState.Opened)
{
return;
}
try
{
myServiceClient.Close();
}
catch (CommunicationException ex)
{
myServiceClient.Abort();
}
catch (TimeoutException ex)
{
myServiceClient.Abort();
}
catch (Exception ex)
{
myServiceClient.Abort();
throw;
}
}
}