What is the best workaround for the WCF client `using` block issue?

前端 未结 26 1699
余生分开走
余生分开走 2020-11-22 00:03

I like instantiating my WCF service clients within a using block as it\'s pretty much the standard way to use resources that implement IDisposable:

26条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 00:37

    @Marc Gravell

    Wouldn't it be OK to use this:

    public static TResult Using(this T client, Func work)
            where T : ICommunicationObject
    {
        try
        {
            var result = work(client);
    
            client.Close();
    
            return result;
        }
        catch (Exception e)
        {
            client.Abort();
    
            throw;
        }
    }
    

    Or, the same thing (Func) in case of Service.Use

    These would make returning variables easier.

提交回复
热议问题