原来开发的程序是.net3.5的,现在需要写一个webservices给客户调用数据,于是还是使用.net3.5写了一个webservices
有一个方法需要传入参数为string a,int b,DateTime c三种,但是调试的时候如果不传参数,在页面上会报出转换错误的异常
这个被测试提给我。需要修改
现在的问题是,我怎么才能捕获到这个异常呢。

解决方案 »

  1.   

      
      先写一个类继承soapHeader
      /// <summary>
        /// Web异常。
        /// </summary>
        public class SoapException : SoapHeader
        {
            /// <summary>
            /// 异常消息。
            /// </summary>
            public string Message;
            /// <summary>
            /// 内部异常。
            /// </summary>
            public string InnerException;
        }
       
    然后调用这个方法,可向客户端抛异常。
            /// <summary>
            /// 修改密码。
            /// </summary>
            /// <param name="User">用户编码。</param>
            /// <param name="Pwd">密码。</param>
            /// <param name="NewPwd">新密码。</param>
            /// <returns>执行结果。</returns>
            [WebMethod(EnableSession = true)]
            [SoapHeader("_SoapException", Direction = SoapHeaderDirection.Fault)]
            [SoapHeader("_CompressFalge", Direction = SoapHeaderDirection.Out)] //还有in 和inout 这个你可以看看SoapHeader类的SoapHeaderDirection属性
            public bool ChangePwd(string User, string Pwd, string NewPwd)
            {
                bool result = false;
                try
                {
                    result = new UserManagement().ChangePassword(User, Pwd, NewPwd);
                }
                catch (Exception exception)
                {
                    _SoapException = new SoapException();
                    _SoapException.Message = exception.Message;
                    if (exception.InnerException != null)
                        _SoapException.InnerException = exception.InnerException.ToString();
                }
                return result;
            }
        }客户端调用out模式
    webservice.WebService  ws=new webservice.WebService ();
    ws.ChangePwd(传入三个参数);  
    Console.WriteLine(ws.SoapException.Message);  
    Console.WriteLine(ws.SoapException.InnerException); 
      

  2.   

             catch (Exception exception)
                {
                    _SoapException = new SoapException();
                    _SoapException.Message = exception.Message;
                    if (exception.InnerException != null)
                        _SoapException.InnerException = exception.InnerException.ToString();
                }
    这句话你看到否