内存泄漏与异步WCF服务调用

时间:2013-12-10 20:47:00

标签: c# wcf asynchronous memory-leaks

我有一个WCF Windows服务,Service1,调用另一台WCF Windows服务,Service2,在另一台机器上找到。 Service1消耗的内存增长,直到我被迫重启服务。我已经运行了一个分析器,看起来呼叫没有被正确处理。

Profiler image

我相信我正在从Service1正确地调用Service2并正确处理。

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service1 : IService, IDisposable
{
    private static bool CallService2()
    {
        Record record = //Create a Record
        Service2Client client = new Service2Client();
        try
        {
            client.BeginAddRecord(record, Service2Callback, client);
        }
        catch
        {
            try
            {
                client.Close();
            }
            catch
            {
                client.Abort();

            }
        }
    }    
    public static void Service2Callback(IAsyncResult ar)
    {
        bool closed = false;
        try
        {
            ((Service2Client)ar.AsyncState).EndAddRecord(ar);
            ((Service2Client)ar.AsyncState).Close();
            closed = true;
        }
        catch (Exception e)
        {
            //error logging
        }
        finally
        {
            try
            {
                if (!closed)
                {
                    ((Service2Client)ar.AsyncState).Abort();
                }
            }catch(Exception){
                //ErrorLogging
            }
        }
    }
}

仔细检查分析器结果显示我传递给Service2的“Record”对象永远不会从内存中释放。

谢谢你, AFrieze

0 个答案:

没有答案