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 錯誤代碼。
範例
以下的網頁表單範例呼叫 Math 了 Web Service 方法,當發生除以零時會拋出例外。 一旦拋出例外,網頁表單會捕捉該例外並輸出例外細節,包括 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>
為了讓前述的網頁表單使用以下 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 類別供 XML Web 服務客戶端使用,這些客戶端透過 SOAP 呼叫 XML Web 服務方法。 ASP.NET 會判斷呼叫的客戶端是否使用 SOAP。 這指的是 XML 網路服務中發生例外的情況。 如果用戶端使用 SOAP,ASP.NET 會將特定例外包裝成 a SoapException ,並設定 Actor and Code 屬性。
可用的代碼集合,稱為 SOAP 協定 1.1 版的 SOAP 故障代碼,如下:
| 項目 | 說明 |
|---|---|
| VersionMismatchFaultCode | 發現一個 SOAP 信封的命名空間無效。 |
| MustUnderstandFaultCode | 並非所有 SOAP 元素都需要處理。 然而,如果 SOAP 元素被標記為 MustUnderstand 1 的屬性,則必須如此。 未處理該元件會產生此例外。 |
| ClientFaultCode | 客戶端通話格式不正確或未包含適當資訊。 例如,客戶通話可能沒有正確的認證或付款資訊。 這通常是訊息必須在重發前被更改的指示。 |
| ServerFaultCode | 在伺服器端處理客戶端通話時發生錯誤,但問題並非出在訊息內容。 例如,上游伺服器可能因網路問題無法回應請求。 通常,除非有此類例外,客戶端通話可能會在後續成功。 如果 XML 網路服務拋出例外 SoapException ,除了 且 客戶端使用 SOAP 呼叫外,ASP.NET 會將該例外轉換成 SoapException,並將屬性設 Code 為 , ServerFaultCode 並回傳給客戶端。 |