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參數,由於這兩種類型的參數不能做爲匿名方法的參數。blog

相關文章
相關標籤/搜索