【问题标题】:WCF custom fault exception not caught correctly in clientWCF 自定义错误异常未在客户端中正确捕获
【发布时间】:2012-02-27 10:55:23
【问题描述】:

我正在尝试创建一些自定义 FaultException。我创建了一个名为 CreateFaultDataContract 类。

[DataContract]
public class CreateFault
{
    private string report;

    public CreateFault(string message)
    {
        this.report = message;
    }

    [DataMember]
    public string Message
    {
        get { return this.report; }
        set { this.report = value; }
    }
}

然后我在服务方法中抛出错误。

IService1.cs

[OperationContract]
[FaultContract(typeof(CreateFault))]
void TestFaultException();

Service1.cs

public void TestFaultException()
{
    throw new FaultException<CreateFault>(new CreateFault("CreateFault message"), "Message abt exception");
}

我在我的客户端中捕获了FaultException

private void btnTest_Click(object sender, RoutedEventArgs e)
{
    try
    {
        ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
        client.TestFaultException();
    }
    catch (FaultException<CreateFault> ex)
    {
        MessageBox.Show(ex.Detail.Message, "Success", MessageBoxButton.OK, MessageBoxImage.Error);
    }
    catch (FaultException ex)
    {
        MessageBox.Show(ex.Message, "Failure", MessageBoxButton.OK, MessageBoxImage.Error);
    }
    catch (Exception ex)
    {
    }
}

现在问题来了。当我在 Visual Studio 2010 中创建 WCF 服务应用程序项目时,它按预期工作。错误出现在:

catch (FaultException<CreateFault> ex)

但是,当我使用自定义 FaultExceptions 创建 WCF 服务库项目时,客户端无法识别我的自定义异常。相反,它会在以下位置捕获错误:

catch (FaultException ex)

为什么它不适用于 WCF 服务应用程序项目?

编辑: 这是我在调试过程中捕获异常时得到的

catch (FaultException ex)

(在即时窗口中输入 ?ex)

{“消息异常”}

[System.ServiceModel.FaultException<WpfApplication1.ServiceReference2.CreateFault>]: {"Message abt exception"}
base {System.ServiceModel.CommunicationException}: {"Message abt exception"}
Action: "http://tempuri.org/IService1/TestFaultExceptionCreateFaultFault"
Code: {System.ServiceModel.FaultCode}
Message: "Message abt exception"
Reason: {Message abt exception}

编辑2:

发现问题。我有两个服务引用,他们都有 CreateFault DataContract。当我运行程序时它使用了错误的。

当我改成

catch (FaultException<ServiceReference2.CreateFault> ex) 

成功了

【问题讨论】:

  • 所有帐户都应该捕获异常。你能检查一下捕获异常是通用的“FaultException”还是只是“FaultException”。
  • 这是我在即时窗口中键入 ?ex 时得到的结果 ?ex {"Message abt exception"} [System.ServiceModel.FaultException]: {"Message abt exception "} base {System.ServiceModel.CommunicationException}: {"Message abt exception"} Action: "tempuri.org/IService1/TestFaultExceptionCreateFaultFault" Code: {System.ServiceModel.FaultCode} Message: "Message abt exception" 原因: {Message abt exception}跨度>
  • 发现问题。我有两个服务引用,他们都有 CreateFault DataContract。当我运行程序时它使用了错误的。当我改为 catch (FaultException ex) 时它起作用了
  • 您能否将您的解决方案添加为答案并将其标记为已接受的答案?这样,我们都可以看到问题已得到解答(您可能会获得投票:-))
  • 好的,会的。只需要等到明天,直到我被允许按接受作为答案

标签: wcf faultexception


【解决方案1】:

发现问题。我有两个服务引用,他们都有 CreateFault DataContract。当我运行程序时它使用了错误的。

当我改成

catch (FaultException<ServiceReference2.CreateFault> ex) 

成功了

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多