tags:

views:

44

answers:

2

i have created a custom exception in the business layer and also using wcf layer where I am calling the methods in the business layer then in another website i am calling the method from wcf. i can see the message that i wrote in custom exception but the program goes staright to exception (the second catch block) instead of hitting my first catch block(where the custom exception is) when i hover over the exception i see my message but it's inside something called faultexception which i am not familiar with. and in there under details..there i see type= CanOnlyApplyOnceException. here is my code:

 protected void AddNewApplication()
    {
        try
        {
            using (var proxy = new ServiceReference1.ServiceClient())
            {
                proxy.AddApplication(new Application
                {
                    Credentials = 2,
                    Comments = txtComments.Text,
                });
            }
        }
        catch (CanOnlyApplyOnceException c)
        {
            ErrorSummary.AddError(c.Message, this);
            return;
        }
        catch (Exception)
        {
            lblStatus.Text = "There has been an error.  Please try again";
        }
    }
A: 

FaultException is thrown on the client if a service throws an exception.

Depending on the service, you may be able to write

    catch (FaultException<CanOnlyApplyOnceException> c)
    {
        ErrorSummary.AddError(c.Detail.Message, this);
        return;
    }
SLaks
Yeah i did this already and still goes straight to Exception.
Then you need to catch the non-generic `FaultException`.
SLaks
this is the detail inside exception {"You can only apply once!"} [System.ServiceModel.FaultException<System.ServiceModel.ExceptionDetail>]: {"You can only apply once"} Data: {System.Collections.ListDictionaryInternal} HelpLink: null InnerException: null Message: "You can only apply once" Source: "mscorlib" and also the type CanOnlyApplyOnce is inside the details of this. How do i get the type?
You need to check `ex.Detail.Type`.
SLaks
+3  A: 

You need to do 2 things:

  1. Add FaultContract attribute on your method declaration on your WCF service like this:

    
    [OperationContract]
    [FaultContract((typeof(CanOnlyApplyOnceException))]
    void AddApplication(Application your_variable_name); 
    

Then make sure you throw exception of type CanOnlyApplyOnceException.

  1. Change your catch block to this:

    catch (FaultException<CanOnlyApplyOnceException> c)
    
Jojo Sardez