Redigera

Dela via


SoapException.Code Property

Definition

Gets the type of SOAP fault code.

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

Property Value

An XmlQualifiedName that specifies the SOAP fault code that occurred.

Examples

The following Web Form example calls the Math Web Service method, which throws an exception if a division by zero occurs. Once the exception is thrown, the Web Form catches the exception and outputs the exception details, including the Actor and Code properties into a HtmlTable control.

<%@ 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>

In order for the preceding Web Form to use the following Math XML Web service example, a namespace of MyMath was specified during the creation of the proxy class.

<%@ 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

Remarks

The Code property can only be set when creating a new instance of the SoapException class.

The SoapException class is for use by XML Web service clients that call XML Web service methods over SOAP. ASP.NET handles whether the client that calls uses SOAP. This is when an exception occurs in an XML Web service. If the client uses SOAP, ASP.NET wraps the specific exception into a SoapException and sets the Actor and Code properties.

The set of available codes, known as SOAP Fault Codes for SOAP protocol version 1.1, are the following:

Item Description
VersionMismatchFaultCode An invalid namespace for a SOAP envelope was found.
MustUnderstandFaultCode Not all SOAP elements require processing. However, if a SOAP element is marked with the MustUnderstand attribute with a value of 1, it is required. Failure to process the element generates this exception.
ClientFaultCode A client call was not formatted correctly or did not contain the appropriate information. For example, the client call might not have the proper authentication or payment information. It is generally an indication that the message must be changed before it is resent.
ServerFaultCode An error occurred during the processing of a client call on the server, however, the problem is not due to the message contents. For example, an upstream server might not respond to a request due to network problems. Typically, with this type of exception, the client call might succeed later. If an XML Web service throws an exception, other than SoapException and the client calls using SOAP, ASP.NET converts the exception to a SoapException, setting the Code property to ServerFaultCode and throws it back to the client.

Applies to

See also