다음을 통해 공유


방법: ASP.NET을 사용하여 만든 웹 서비스에서 예외 throw

코드 예제

다음 코드 예제에서는 SoapException을 throw하고 Detail 속성을 설정하여 예외에 대한 자세한 정보를 표시합니다. ASP.NET 웹 서비스에서 예외를 throw하는 방법에 대한 자세한 내용은 XML Web Services에서 예외 처리 및 throw를 참조하십시오.

예제

<%@ WebService Language="VB" class="ThrowSoapException"%>

Imports System
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml.Serialization
Imports System.Xml

Public Class ThrowSoapException
    Inherits WebService
    
    ' This Web service method throws a SOAP client fault code. 
    <WebMethod()> _
    Public Sub myThrow()
        
        ' Build the detail element of the SOAP fault.
        Dim doc As New System.Xml.XmlDocument()
        Dim node As System.Xml.XmlNode = _
            doc.CreateNode(XmlNodeType.Element, _
            SoapException.DetailElementName.Name, _
            SoapException.DetailElementName.Namespace)
 
        ' Build specific details for the SoapException.
        ' Add first child of detail XML element.
        Dim details As System.Xml.XmlNode = _ 
            doc.CreateNode(XmlNodeType.Element, _
            "mySpecialInfo1", "http://tempuri.org/")

        ' Add second child of detail XML element with an attribute.
        Dim details2 As System.Xml.XmlNode = _
            doc.CreateNode(XmlNodeType.Element, _
            "mySpecialInfo2", "http://tempuri.org/")
        Dim attr As XmlAttribute = doc.CreateAttribute("t", _ 
            "attrName", "http://tempuri.org/")
        attr.Value = "attrValue"
        details2.Attributes.Append(attr)

        ' Append the two child elements to the detail node.
        node.AppendChild(details)
        node.AppendChild(details2)
                
        'Throw the exception.
        Dim se As New SoapException("Fault occurred", _
            SoapException.ClientFaultCode, _
            Context.Request.Url.AbsoluteUri, node)
        Throw se
        Return
    End Sub
End Class
<%@ WebService Language="C#" class="ThrowSoapException"%>

using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
using System.Xml;

public class ThrowSoapException : WebService 
{
    // This Web service method throws a SOAP client fault code.
    [WebMethod]
    public void myThrow(){

        // Build the detail element of the SOAP fault.
        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        System.Xml.XmlNode node = doc.CreateNode(XmlNodeType.Element,
             SoapException.DetailElementName.Name,              SoapException.DetailElementName.Namespace);

        // Build specific details for the SoapException.
        // Add first child of detail XML element.
        System.Xml.XmlNode details =
          doc.CreateNode(XmlNodeType.Element, "mySpecialInfo1",
                         "http://tempuri.org/");
        System.Xml.XmlNode detailsChild = 
          doc.CreateNode(XmlNodeType.Element, "childOfSpecialInfo",
                         "http://tempuri.org/");
        details.AppendChild(detailsChild);
            
        // Add second child of detail XML element with an attribute.
        System.Xml.XmlNode details2 =
          doc.CreateNode(XmlNodeType.Element, "mySpecialInfo2",
                         "http://tempuri.org/");
        XmlAttribute attr = doc.CreateAttribute("t", "attrName",
                            "http://tempuri.org/");
        attr.Value = "attrValue";
        details2.Attributes.Append(attr);

        // Append the two child elements to the detail node.
        node.AppendChild(details);
        node.AppendChild(details2);
            
        //Throw the exception    
        SoapException se = new SoapException("Fault occurred",
          SoapException.ClientFaultCode,
          Context.Request.Url.AbsoluteUri ,
          node);

        throw se;
        return;
    }
}

참고 항목

작업

방법: 웹 서비스 메서드에서 throw한 예외 처리

참조

SoapException Class
SoapHeaderException Class

개념

XML Web services에서 예외 처리 및 throw
XML Web services 클라이언트 빌드

기타 리소스

Handling and Throwing Exceptions
ASP.NET을 사용하는 XML Web services

Footer image

Copyright © 2007 by Microsoft Corporation. All rights reserved.