用户代码C#未处理的异常

时间:2015-10-02 08:04:27

标签: c# asp.net wcf

我有WCF服务,我想提出异常,当我提出异常然后我得到错误异常未被用户代码处理。请有人能想出这个问题。我的界面如下:

 [ServiceContract]
 public interface IADSICAuthentication
 {
        [WebInvoke(Method = "POST", UriTemplate = "Login", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        [OperationContract]
        Response LoginClient(Credentials credentials);
}

我的LoginCLient方法定义如下

public Response LoginClient(Credentials oCredentials)
 {
   try
   {
    if(oCredentials.PassWord == null)
    {
        //something
    }
    else
    {
      throw new Exception(string.Format("UnAuthorize User : 401"));
    }
   }
   catch (Exception e)
   {
      throw; // I am getting Exception here Unhandled by user code
   }


}

2 个答案:

答案 0 :(得分:1)

您可以使用FaultException。 FaultException可以抛出特定的异常,因此您可以使用catch来识别类型。

我在这里给出了一个类似的答案:Proper way to throw exception over WCF

答案 1 :(得分:0)

您应该使用FaultException<T>并标记ServiceContract类,以包含预期的类型。

有关如何使用它的详细信息,请访问MSDN - FaultException