在ASP.NET中使用SOAP Extensions捕獲WebService異常

來源:互聯網
上載者:User

Application_Error不能捕獲的異常

      [WebMethod]        public string HelloWorld()        {            throw new Exception("this exption can't be handled by Application_Error Method");            return "Hello World";        }
定義Application_Error將不能捕獲這個異常.
原因需要瞭解一下SOAP請求的在ASP.NET的生命週期.
   

 

Process Message程序呼叫WebMethod時如果產生SoapException,SoapHeaderException 被序列化後,返回在SOAP <Fault>結點.

使用SOAP extension擴充處理Process Message過程產生的異常
我們可以SOAP Extension處理

Code
public class SoapExceptionHandler : System.Web.Services.Protocols.SoapExtension
    {
        public override void ProcessMessage(System.Web.Services.Protocols.SoapMessage message)
        {
            if (message.Stage == SoapMessageStage.AfterSerialize)
            {
                if (message.Exception != null)
                {
                    LogUtil.Log.Error(message.Exception.InnerException);
                }
            }
        }

        public override object GetInitializer(Type serviceType)
        {
            return null;
        }

        public override object GetInitializer(
            LogicalMethodInfo methodInfo,
            SoapExtensionAttribute attribute)
        {
            return null;
        }

        public override void Initialize(object initializer) 
        {
        }
    }
在Web.config system.web結點中配置
Code
        <webServices>
            <soapExtensionTypes>
                <add type="Elplan.App.SoapExceptionHandler, Elplan.App" priority="1" group="High" />
            </soapExtensionTypes>
        </webServices>

調試:

如果使用VS直接運行調試asmx,是不可以的.(因為...它不是一個完整的SOAP請求.)可使用WebServiceStudio.
相關地址:http://www.codeplex.com/WebserviceStudio 

參考文章:

Using SOAP Extensions in ASP.NET
Handling and Throwing Exceptions in XML Web Services
How to create a global exception handler for a Web Service

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.