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