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

前端 未结 26 1753
余生分开走
余生分开走 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:19

    I have written a simple base class that handles this. It's available as a NuGet package and it's quite easy to use.

    //MemberServiceClient is the class generated by SvcUtil
    public class MemberServiceManager : ServiceClientBase
    {
        public User GetUser(int userId)
        {
            return PerformServiceOperation(client => client.GetUser(userId));
        }
    
        //you can also check if any error occured if you can't throw exceptions       
        public bool TryGetUser(int userId, out User user)
        {
            return TryPerformServiceOperation(c => c.GetUser(userId), out user);
        }
    }
    

提交回复
热议问题