SoapException.Code 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取 SOAP 错误代码的类型。
public:
property System::Xml::XmlQualifiedName ^ Code { System::Xml::XmlQualifiedName ^ get(); };
public System.Xml.XmlQualifiedName Code { get; }
member this.Code : System.Xml.XmlQualifiedName
Public ReadOnly Property Code As XmlQualifiedName
属性值
一个 XmlQualifiedName ,指定发生的 SOAP 错误代码。
示例
以下 Web 窗体示例调用 Math Web 服务方法,如果发生除数零,则会引发异常。 引发异常后,Web 窗体将捕获异常并将异常详细信息(包括 Actor 和 Code 属性)输出到 HtmlTable 控件中。
<%@ Page Language="C#" %>
<html>
<head>
<script runat=server language="C#">
void Page_Load(Object o, EventArgs e)
{
int UsageCount;
// Create a new instance of the proxy class.
MyMath.Math math = new MyMath.Math();
// Make a call to the Math XML Web service, which throws an exception.
try
{
math.Divide(3, 0);
}
catch (System.Web.Services.Protocols.SoapException error)
{
// Populate the table with the exception details.
ErrorTable.Rows.Add(BuildNewRow("Fault Code Namespace", error.Code.Namespace));
ErrorTable.Rows.Add(BuildNewRow("Fault Code Name", error.Code.Name));
ErrorTable.Rows.Add(BuildNewRow("SOAP Actor that threw Exception", error.Actor));
ErrorTable.Rows.Add(BuildNewRow("Error Message", error.Message));
return;
}
}
HtmlTableRow BuildNewRow(string Cell1Text, string Cell2Text)
{
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell1 = new HtmlTableCell();
HtmlTableCell cell2 = new HtmlTableCell();
// Set the contents of the two cells.
cell1.Controls.Add(new LiteralControl(Cell1Text));
// Add the cells to the row.
row.Cells.Add(cell1);
cell2.Controls.Add(new LiteralControl(Cell2Text));
// Add the cells to the row.
row.Cells.Add(cell2);
return row;
}
</script>
</head>
<body>
<table id="ErrorTable"
CellPadding=5
CellSpacing=0
Border="1"
BorderColor="black"
runat="server" />
</body>
<%@ Page Language="VB"%>
<html>
<head>
<script runat=server language="VB">
Sub Page_Load(o As Object, e As EventArgs)
Dim UsageCount As Integer
' Create a new instance of the proxy class.
Dim math As New MyMath.Math()
' Make a call to the Math XML Web service, which throws an exception.
Try
math.Divide(3, 0)
Catch err As System.Web.Services.Protocols.SoapException
' Populate our Table with the Exception details
ErrorTable.Rows.Add(BuildNewRow("Fault Code Namespace", err.Code.Namespace))
ErrorTable.Rows.Add(BuildNewRow("Fault Code Name", err.Code.Name))
ErrorTable.Rows.Add(BuildNewRow("SOAP Actor that threw Exception", err.Actor))
ErrorTable.Rows.Add(BuildNewRow("Error Message", err.Message))
Return
End Try
End Sub 'Page_Load
Function BuildNewRow(Cell1Text As String, Cell2Text As String) As HtmlTableRow
Dim row As New HtmlTableRow()
Dim cell1 As New HtmlTableCell()
Dim cell2 As New HtmlTableCell()
' Set the contents of the two cells.
cell1.Controls.Add(New LiteralControl(Cell1Text))
' Add the cells to the row.
row.Cells.Add(cell1)
cell2.Controls.Add(New LiteralControl(Cell2Text))
' Add the cells to the row.
row.Cells.Add(cell2)
Return row
End Function 'BuildNewRow
</script>
</head>
<body>
<table id="ErrorTable"
CellPadding=5
CellSpacing=0
Border="1"
BorderColor="black"
runat="server" />
</body>
为了使前面的 Web 窗体使用以下 Math XML Web 服务示例,在创建代理类期间指定了一个命名空间 MyMath 。
<%@ WebService Language="C#" Class="Math"%>
using System.Web.Services;
using System;
public class Math : WebService {
[WebMethod]
public float Divide(int dividend, int divisor) {
if (divisor == 0)
throw new DivideByZeroException();
return dividend/divisor;
}
}
<%@ WebService Language="VB" Class="Math"%>
Imports System.Web.Services
Imports System
Public Class Math
Inherits WebService
<WebMethod()> _
Public Function Divide(dividend As Integer, divisor As Integer) As Single
If divisor = 0 Then
Throw New DivideByZeroException()
End If
Return Convert.ToSingle(dividend / divisor)
End Function 'Divide
End Class 'Math
注解
Code只能在创建新类实例SoapException时设置该属性。
此类 SoapException 供通过 SOAP 调用 XML Web 服务方法的 XML Web 服务客户端使用。 ASP.NET 处理调用是否使用 SOAP 的客户端。 这是当 XML Web 服务中发生异常时。 如果客户端使用 SOAP,ASP.NET 将特定异常包装到一个 SoapException 并设置 Actor 和 Code 属性中。
可用代码集(称为 SOAP 协议版本 1.1 的 SOAP 错误代码)如下:
| 物品 | 说明 |
|---|---|
| VersionMismatchFaultCode | 找到 SOAP 信封的命名空间无效。 |
| MustUnderstandFaultCode | 并非所有 SOAP 元素都需要处理。 但是,如果使用值为 1 的属性标记 MustUnderstand SOAP 元素,则需要它。 无法处理该元素将生成此异常。 |
| ClientFaultCode | 客户端调用的格式不正确或不包含相应的信息。 例如,客户端调用可能没有适当的身份验证或付款信息。 通常表明消息在重新发送之前必须更改。 |
| ServerFaultCode | 在服务器上处理客户端调用期间发生错误,但问题不是由于消息内容所致。 例如,上游服务器可能由于网络问题而无法响应请求。 通常,如果出现这种类型的异常,客户端调用可能会在以后成功。 如果 XML Web 服务引发异常(而不是 SoapException 使用 SOAP 的客户端调用)ASP.NET 将异常转换为异常 SoapException,请将 Code 该属性设置为该属性 ServerFaultCode 并将其抛回到客户端。 |