Tôi đang cố thêm nhiều thông tin liên quan đến lỗi SOAP trong ứng dụng khách nguồn mở. Khách hàng được thiết lập để gọi "HandleFault" bất cứ khi nào nó gặp phải bất kỳ lỗi SOAP nào. Phương pháp Xử lý lỗi được hiển thị dưới đây:Cách xử lý ngoại lệ lỗi WCF
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)
);
}
Đây là một phần của lỗi SOAP được thông qua tại như "thông báo" khi tôi cố gắng và làm điều gì đó giống như sự thay đổi số điện thoại sang định dạng không hợp lệ từ khách hàng.
<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>
Bất cứ khi nào lỗi được gặp, khách hàng chỉ trả lại "Thông điệp yêu cầu tài liệu bị lỗi đó ngăn chặn xử lý yêu cầu.", Tôi muốn bao gồm các "AttributeRepresentationFailure" nút và con các nút trước khi ném lại ngoại lệ trong ứng dụng khách.
Cách tôi hiểu nó là tôi cần phải xác định một lớp Fault có chứa các chi tiết đó để được khử khoáng, để cuộc gọi đến "CreateFault" có thể trả về a. Tôi đã đọc qua http://msdn.microsoft.com/en-us/library/ms733841.aspx nhưng tôi chỉ không hiểu chính xác cách xác định lớp để khách hàng biết loại lỗi nào được ném.
CẬP NHẬT
Ở phía client phương pháp xử lý lỗi tôi thêm
try
{
throw faultexcept;
}
catch (System.ServiceModel.FaultException<InvalidRepresentation> invalidRepresentationFault)
{
throw invalidRepresentationFault;
}
catch (System.ServiceModel.FaultException otherFault)
{
throw otherFault;
}
catch (Exception ex)
{
throw ex;
}
Các lỗi luôn bắt gặp dưới lớp lỗi cơ sở "otherFault". Lớp Sơ yếu lý lịch không hợp lệ của tôi được định nghĩa là bên dưới
[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() {
}
}
cảm ơn! Tôi nghĩ rằng tôi đang đi đúng hướng, nhưng khi xác định lớp Fault của tôi, lớp học đó cần phải trông như thế nào để xử lý đúng lỗi trong ví dụ tôi đã gửi ở trên? Ngoài ra, làm thế nào trong mã khách hàng để tôi xác định những gì lỗi đã được ném, tôi giả định rằng được chăm sóc trong mã 'System.ServiceModel.FaultException.CreateFault'? –