WcfHelper

来源:互联网 发布:ios商城项目源码 编辑:程序博客网 时间:2024/05/10 16:38

对WCF调用的简要封装,示例如下:

/// <summary>    ///                 /*try            {                return WcfClientDispose.Using<AccountClient, Account>(new AccountClient(), client =>                    {                        Account account = client.GetAccount(ptAccount, 1);                    });            }            catch (Exception e)            {                            }*/    /// </summary>    public class WcfHelper    {        public static void Using<T, R>(T client, Action<T> action) where T : ICommunicationObject        {            try            {                action(client);                client.Close();            }            catch (CommunicationException exception)            {                client.Abort();            }            catch (TimeoutException exception2)            {                client.Abort();            }            catch (Exception exception3)            {                client.Abort();                throw exception3;            }        }        public static R Using<T, R>(T client, Func<T, R> action)            where T : ICommunicationObject            where R : class        {            R local2;            try            {                R local = action(client);                client.Close();                local2 = local;            }            catch (CommunicationException exception)            {                client.Abort();                local2 = default(R);            }            catch (TimeoutException exception2)            {                client.Abort();                local2 = default(R);            }            catch (Exception exception3)            {                client.Abort();                throw exception3;            }            return local2;        }    }



原创粉丝点击