0

I'm trying to create a FaultException in my WCF service, so that I can display the error in my WPF application. The problem that I am having is that the FaultException still displays/crashes to my service and does not want to display the error in my WPF application(in the messagebox).

Here is my coding:

Service:

public void AddNewSupplier(SupplierData Data)
{
    using (Database db = new Database())
    {
        var supplier = new Supplier
        {
            Name = Data.Name,
            Address = Data.Address,
            ContactNumber = Data.ContactNumber,
            Description = Data.Description,
            Terms = Data.Terms
        };
        if (db.Supplier.Any(sup => supplier.Name.Contains(sup.Name)))
        {
            throw new FaultException(new FaultReason("The database already contains the name " + supplier.Name + ". Please enter a diffirent name"), new FaultCode("Multiple Items"));
        }
        else
        {
            db.Supplier.Add(supplier);
            db.SaveChanges();
        }
    }
}

Client Side Application:

private void btnAdd_Click(object sender, RoutedEventArgs e)
{
    using (MKCServiceClient service = new MKCServiceClient())
    {
        try
        {
            service.AddNewSupplier(new SupplierData
            {
                Name = txtName.Text,
                Address = txtAddress.Text,
                ContactNumber = txtContactNumber.Text,
                Description = txtDescription.Text,
                Terms = txtTerms.Text
            });                
        }
        catch (FaultException fe)
        {
            if (fe.Code.Name == "Multiple Items")
                MessageBox.Show(fe.Reason.ToString());
        }
    }
}

What could I be doing wrong?

CareTaker22
  • 1,260
  • 3
  • 18
  • 36
  • 1
    WCF exceptions will only come across the wire if the exception goes 'unhandled' in the server side logic. So if you have a parent level try/catch it too will need to rethrow to the client – pnm Jan 13 '16 at 20:33
  • Thanks for the comment! @pnm - Parent level try/catch? There is no other try and catch in my `AddNewSupplier` method and the only try/catch in my application works fine, but the *service* exception still gets thrown first, where it should not even have been thrown at all. – CareTaker22 Jan 13 '16 at 20:42
  • Is your service exception making it to the client? Is so, it might be wrapping your fault exception. check the service exception's InnerException property. – pnm Jan 13 '16 at 20:48
  • It reaches the client, but only if I insert a breakpoint in the client's method. Otherwise, the InnerException in my service is `null`. – CareTaker22 Jan 13 '16 at 20:59
  • add the following below your existing catch() in your client method: catch(exception ex) {throw;} place a break point on that and see if the exception coming back has your exception as an inner exception. – pnm Jan 13 '16 at 21:03
  • I've done like you said and placed the breakpoint at `catch (Exception) { throw; }`, but for some reason I can't step over the `else` in my service and then it doesn't want to hit the new catch breakpoint in my client application – CareTaker22 Jan 13 '16 at 21:15
  • EDIT: Oky, for some reason the `MessageBox.Show` actually works perfectly in the client side and it gives me the correct message, but now all I need to do is to stop the exception being thrown in my service. It's really annoying, because I can't continue to use my client application after the service exception has been thrown... – CareTaker22 Jan 13 '16 at 21:22
  • 1
    note that 'throw' is in essence a return statement. the debugger will effectively 'step out'. – pnm Jan 13 '16 at 21:22
  • Does the 'throw' need to be in the service coding or application? Just making sure – CareTaker22 Jan 13 '16 at 21:23
  • Correct, the throws would typically only need to be inside the service. Without seeing more of your server/client infrastructure it will be difficult to provide further guidance. If you're only seeing a 'unhandled exception' dialog in the debugger, this won't be seen by a user once deployed. – pnm Jan 13 '16 at 22:08

1 Answers1

0

Could you place the breakpoint in your Catch(...) area? What is the real value of fe.Code.Name property?

smq93
  • 94
  • 1
  • 9
  • Let me check quickly ;) – CareTaker22 Jan 13 '16 at 20:25
  • Ok, so when I place a breakpoint at `fe.Code.Name`, it throws the error in my service and then it opens up the `MessageBox.Show` in my Application, but when I remove the breakpoint, it throws the exception in my service and gets stuck at the `else` for some reason and doesn't run through all the code... – CareTaker22 Jan 13 '16 at 20:37
  • 1
    try to change FaultException type in Catch(...) to the just Exception type. Maybe there is another exception? – smq93 Jan 13 '16 at 20:40
  • Sorry, I can't do that, as i'm getting all my data from my *service model* I need to use `FaultException` or else I would not be able to catch the exception at all – CareTaker22 Jan 13 '16 at 20:44