捕获到ASP.NET ASMX Web服务的SOAP请求

考虑将传入SOAP请求记录到ASP.NET ASMX Web服务的要求。任务是捕获发送到Web服务的原始XML。

传入消息需要记录以进行调试检查。应用程序已经有自己的日志库在使用,所以理想的用法是这样的:

//string or XML,it doesn't matter.
string incomingSoapRequest = GetSoapRequest();

Logger.LogMessage(incomingSoapRequest);

>是否有任何简单的解决方案来捕获传入SOAP请求的原始XML?
>您将处理哪些事件以访问此对象和相关属性
>有没有反正IIS可以捕获传入的请求和推送到日志?

解决方法

捕获原始消息的一种方法是使用 SoapExtensions

SoapExtensions的一个替代方法是实现IHttpModule,并在输入流进入时抓取输入流。

public class LogModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += this.OnBegin;
    }

    private void OnBegin(object sender,EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
        HttpContext context = app.Context;

        byte[] buffer = new byte[context.Request.InputStream.Length];
        context.Request.InputStream.Read(buffer,buffer.Length);
        context.Request.InputStream.Position = 0;

        string soapMessage = Encoding.ASCII.GetString(buffer);

        // Do something with soapMessage
    }

    public void dispose()
    {
        throw new NotImplementedException();
    }
}

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...