WCF内存使用量增加

时间:2014-04-17 21:20:05

标签: c# performance wcf wcf-binding

我有一个WCF客户端和一个WCF服务。为了测试,当我不断地同步调用WCF方法时,每次内存使用量会大约增加1 mb。我该如何阻止这种情况发生?

MyService.JobsClient Client = new MyService.JobsClient();
Client.Endpoint.Address = new System.ServiceModel.EndpointAddress(ConfigurationManager.AppSettings["ServicePath"]);
Client.WCFGetSystemState(System.Environment.MachineName); 

1 个答案:

答案 0 :(得分:2)

您应确保在使用后关闭客户端。您可以遵循此模式(取自MSDN):

MyService.JobsClient Client = new MyService.JobsClient();
Client.Endpoint.Address = new System.ServiceModel.EndpointAddress(ConfigurationManager.AppSettings["ServicePath"]);
try
{
    Client.WCFGetSystemState(System.Environment.MachineName); 
    Client.Close();
}
catch (TimeoutException timeout)
{
    // Handle the timeout exception.
    Client.Abort();
}
catch (CommunicationException commException)
{
    // Handle the communication exception.
    Client.Abort();
}