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参数,因为这两种类型的参数不能作为匿名方法的参数。

posted on 2018-02-12 10:23  游戏人间  阅读(144)  评论(0编辑  收藏  举报