gpt4 book ai didi

c# - 如何处理 WCF 故障异常

转载 作者:太空狗 更新时间:2023-10-29 21:34:24 26 4
gpt4 key购买 nike

我正在尝试添加有关开源客户端应用程序中 SOAP 故障的更多信息。客户端设置为在遇到任何 SOAP 错误时调用“HandleFault”。 Handle Fault方法如下:

   public static void HandleFault(Message message) {
MessageFault fault = MessageFault.CreateFault(message, Int32.MaxValue);
throw System.ServiceModel.FaultException.CreateFault(fault,
typeof(PermissionDeniedFault),
typeof(EndpointUnavailable),
typeof(InvalidRepresentation),
typeof(UnwillingToPerformFault),
typeof(CannotProcessFilter),
typeof(AnonymousInteractionRequiredFault)
);
}

这是 SOAP 故障的一部分,当我尝试执行某些操作(例如将电话号码从客户端更改为无效格式)时,它作为“消息”传入。

  <s:Body u:Id="_2">
<Fault xmlns="http://www.w3.org/2003/05/soap-envelope">
<Code>
<Value>Sender</Value>
<Subcode>
<Value xmlns:a="http://schemas.xmlsoap.org/ws/2004/09/transfer">a:InvalidRepresentation</Value>
</Subcode>
</Code>
<Reason>
<Text xml:lang="en-US">The request message contains errors that prevent processing the request.</Text>
</Reason>
<Detail>
<RepresentationFailures xmlns="http://schemas.microsoft.com/2006/11/ResourceManagement" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AttributeRepresentationFailure>
<AttributeType>OfficePhone</AttributeType>
<AttributeValue>(123)456-7890</AttributeValue>
<AttributeFailureCode>ValueViolatesRegularExpression</AttributeFailureCode>
<AdditionalTextDetails>The specified attribute value does not satisfy the regular expression.</AdditionalTextDetails>
</AttributeRepresentationFailure>
<CorrelationId>11042dda-3ce9-4563-b59e-d1c1355819a4</CorrelationId>
</RepresentationFailures>
</Detail>
</Fault>

每当遇到故障时,客户端只返回“请求消息包含阻止处理请求的错误。”,我想在重新之前包含“AttributeRepresentationFailure”节点和子节点- 在客户端抛出异常。

我的理解是,我需要定义一个 Fault 类,其中包含要反序列化的那些细节,以便对“CreateFault”的调用可以返回一个 .我已经通读了 http://msdn.microsoft.com/en-us/library/ms733841.aspx,但我只是不明白如何定义类,以便客户端知道抛出的错误类型。

更新

在我添加的客户端处理故障的方法中

 try
{
throw faultexcept;
}
catch (System.ServiceModel.FaultException<InvalidRepresentation> invalidRepresentationFault)
{
throw invalidRepresentationFault;
}
catch (System.ServiceModel.FaultException otherFault)
{
throw otherFault;
}
catch (Exception ex)
{
throw ex;
}

故障总是在基本故障类“otherFault”下被捕获。我的 InvalidRepresentation 类定义如下

 [DataContract(Namespace = Constants.Rm.Namespace)]
public class InvalidRepresentation
{
private string _attributeType;
private string _attributeValue;
private string _attributeFailureCode;
private string _additionalTextDetails;

[DataMember]
public string AttributeType
{
get { return _attributeType; }
set { _attributeType = value; }
}

[DataMember]
public string AttributeValue
{
get { return _attributeValue; }
set { _attributeValue = value; }
}

[DataMember]
public string AttributeFailureCode
{
get { return _attributeFailureCode; }
set { _attributeFailureCode = value; }
}

[DataMember]
public string AdditionalTextDetails
{
get { return _additionalTextDetails; }
set { _additionalTextDetails = value; }
}


public InvalidRepresentation() {

}
}

最佳答案

要添加到 Fredrik 的答案中,您的 Fault 类可以是您需要的任何内容,以将自定义错误的详细信息传达给客户。它不必从另一个类继承或实现接口(interface)。它只需要用 DataContract 属性标记即可。

至于在客户端捕获它:

try
{
...
}
catch (FaultException<MathFault> mathFault)
{
// handle a math fault
}
catch (FaultException<OtherCustomFault> otherFault)
{
// handle another type of custom fault
}
catch (Exception ex)
{
// regular exception handling
}

关于c# - 如何处理 WCF 故障异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17097839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com