0

I have WCF service in which I want raise an Exception, when I am raising an exception then I getting Error Exception unhandled by user code. Please someone can figure this Issue. My Interface is as follows:

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

and my LoginCLient Method defines below

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
   }


}
BASEER HAIDER JAFRI
  • 939
  • 1
  • 17
  • 35

2 Answers2

1

You can use FaultException. FaultException can throw a specific exception, so you will be able to identify the type using catch.

Here I gave an similar answer with an example: Proper way to throw exception over WCF

Community
  • 1
  • 1
Ricardo Pontual
  • 3,749
  • 3
  • 28
  • 43
0

You should use FaultException<T> and mark up your ServiceContract class to include which types are expected.

Details on how to use it are available at MSDN - FaultException

toadflakz
  • 7,764
  • 1
  • 27
  • 40