我有一个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);
答案 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();
}