WCF 客户端调用封装

   public static void Invoke<TChannel>(Action<TChannel> action, TChannel proxy)
        {
            ICommunicationObject channel = proxy as ICommunicationObject;
            if (null == channel)
            {
                throw new ArgumentException("The proxy is not a valid channel implementing the ICommunicationObject interface", "proxy");
            }
            try
            {
                action(proxy);
            }

            catch (TimeoutException)
            {
                channel.Abort();
                throw;
            }

            catch (CommunicationException)
            {
                channel.Abort();
                throw;
            }
            finally
            {
                channel.Close();
            }
        }

        public static TReturn Invoke<TChannel, TReturn>(Func<TChannel, TReturn> func, TChannel proxy)
        {
            ICommunicationObject channel = proxy as ICommunicationObject;
            if (null == channel)
            {
                throw new ArgumentException("The proxy is not a valid channel implementing the ICommunicationObject interface", "proxy");
            }
            try
            {
                return func(proxy);

            }

            catch (TimeoutException)
            {
                channel.Abort();
                throw;
            }

            catch (CommunicationException)
            {
                channel.Abort();
                throw;
            }
            finally
            {
                channel.Close();
            }
        }

 服务方式不能包含ref和out参数,因为这两种类型的参数不能作为匿名方法的参数。

原文地址:https://www.cnblogs.com/wonderfuly/p/8443877.html