日期:2014-05-17  浏览次数:20738 次

WCF客户端的异常处理
小弟在用WCF的时候总会出现很多异常
例如不能连接服务器 和 不能联网都会出错
在客户端 我写了一些方法
/// <summary>
    /// 在客户端专门写一个调用服务器方法的类
    /// 方便管理调用方法时出现的问题
    /// </summary>
    public class LoginContractUsed : ILoginContract
    {
        ILoginContract proxy = null;
        public LoginContractUsed()
        {
            try
            {
                ChannelFactory<ILoginContract> fact = new ChannelFactory<ILoginContract>("ILoginContract");
                proxy = fact.CreateChannel();
            }
            catch (Exception e)
            {
                //在异常的时候 调用一个委托 将错误的方法名和类 以及 异常抛出
                if (StaticWcfException.OnWcfException != null)
                {
                    StaticWcfException.OnWcfException("LoginContractUsed", this.GetType(), e);
                }
            }
        }
        //真正的方法调用
        public string Login(string UserName, string UserPwd)
        {
            try
            {
                //返回实际服务器信息
                return proxy.Login(UserName, UserPwd);
            }
            catch(Exception e)
            {
                //如果出错调用本地统一管理委托
                if (StaticWcfException.OnWcfException != null)
                {
                    StaticWcfException.OnWcfException("LoginContractUsed", this.GetType(), e);