SoapException.Code 屬性

定義

取得 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 Form 範例會呼叫 Math Web 服務方法,如果零除法發生,則會擲回例外狀況。 擲回例外狀況之後,Web Form 會攔截例外狀況並輸出例外狀況詳細資料,包括 ActorCode 屬性到 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 Form 使用下列 Math XML Web 服務範例,在建立 Proxy 類別期間指定 了 的 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 ,並設定 ActorCode 屬性。

SOAP 通訊協定 1.1 版的可用代碼集稱為 SOAP 錯誤碼,如下:

項目 描述
VersionMismatchFaultCode 找到 SOAP 信封的無效命名空間。
MustUnderstandFaultCode 並非所有 SOAP 元素都需要處理。 不過,如果 SOAP 元素標示 MustUnderstand 為值為 1 的屬性,則為必要專案。 無法處理專案會產生這個例外狀況。
ClientFaultCode 用戶端呼叫的格式不正確,或未包含適當的資訊。 例如,用戶端呼叫可能沒有適當的驗證或付款資訊。 通常表示訊息必須在重新傳送之前變更。
ServerFaultCode 不過,在伺服器上處理用戶端呼叫時發生錯誤,但問題並非因為訊息內容所造成。 例如,上游伺服器可能不會因網路問題而回應要求。 一般而言,有這種類型的例外狀況,用戶端呼叫稍後可能會成功。 如果 XML Web 服務擲回例外狀況,而不是 SoapException 使用 SOAP 呼叫用戶端,ASP.NET 會將例外 SoapException 狀況轉換成 ,並將 屬性 ServerFaultCode 設定 Code 為 ,並將它擲回用戶端。

適用於

另請參閱