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?