WPF应用程序中的FaultException仍然会在WCF服务中引发错误

时间:2016-01-13 20:20:16

标签: c# wpf wcf faultexception

我正在尝试在我的WCF服务中创建 FaultException ,以便我可以在我的WPF应用程序中显示错误。我遇到的问题是FaultException仍然显示/崩溃到我的服务,并且不想在我的WPF应用程序中显示错误(在消息框中)。

这是我的编码:

服务

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();
        }
    }
}

客户端应用

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());
        }
    }
}

我可能做错了什么?

1 个答案:

答案 0 :(得分:0)

您可以将断点放在Catch(...)区域吗? fe.Code.Name属性的真正价值是什么?