SoapException.Code Propriedade

Definição

Recebe o tipo de código de falha 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

Valor de Propriedade

E XmlQualifiedName que especifique o código de falha SOAP que ocorreu.

Exemplos

O exemplo seguinte do Web Form chama o Math método Web Service, que lança uma exceção se ocorrer uma divisão por zero. Uma vez lançada a exceção, o Web Form captura a exceção e exporta os detalhes das exceções, incluindo as Actor propriedades e Code num HtmlTable controlo.

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

Para que o Web Form anterior utilizasse o seguinte Math exemplo de serviço Web XML, foi especificado um namespace de MyMath durante a criação da classe proxy.

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

Observações

A Code propriedade só pode ser definida ao criar uma nova instância da SoapException classe.

A SoapException classe destina-se a clientes de serviços Web XML que chamam métodos de serviço Web XML através de SOAP. O ASP.NET trata se o cliente que chama utiliza o SOAP. Isto ocorre quando ocorre uma exceção num serviço Web XML. Se o cliente usar SOAP, ASP.NET envolve a exceção específica numa SoapException e define as propriedades Actor e Code.

O conjunto de códigos disponíveis, conhecidos como Códigos de Falha SOAP para o protocolo SOAP versão 1.1, são os seguintes:

Iteme Descrição
VersionMismatchFaultCode Foi encontrado um namespace inválido para um envelope SOAP.
MustUnderstandFaultCode Nem todos os elementos SOAP requerem processamento. No entanto, se um elemento SOAP for marcado com o MustUnderstand atributo com valor 1, é necessário. A falha em processar o elemento gera esta exceção.
ClientFaultCode Uma chamada de cliente não estava formatada corretamente ou não continha a informação apropriada. Por exemplo, a chamada do cliente pode não ter a autenticação ou a informação de pagamento adequada. Geralmente indica que a mensagem deve ser alterada antes de ser reenviada.
ServerFaultCode Ocorreu um erro durante o processamento de uma chamada de cliente no servidor, no entanto, o problema não se deve ao conteúdo da mensagem. Por exemplo, um servidor a montante pode não responder a um pedido devido a problemas de rede. Normalmente, com este tipo de exceção, a chamada do cliente pode ter sucesso mais tarde. Se um serviço Web XML lançar uma exceção, diferente de SoapException e o cliente chamar usando SOAP, ASP.NET converte a exceção numa SoapException, definindo a propriedade Code para ServerFaultCode e devolve-a ao cliente.

Aplica-se a

Ver também