Is there a way to get the raw SOAP request from within a ASP.NET WebMethod?

后端 未结 4 602
广开言路
广开言路 2020-12-10 15:08

Example:

public class Service1 : System.Web.Services.WebService
{
   [WebMethod]
   public int Add(int x, int y)
   {
       string request = getRawSOAPReque         


        
相关标签:
4条回答
  • 2020-12-10 15:45

    Yes, you can do it using SoapExtensions. Here's a nice article that runs through the process.

    0 讨论(0)
  • 2020-12-10 15:47

    You can also read the contents of the Request.InputStream.

    This way its more useful such as for cases when you want to perform validation or other actions within the WebMethod depending on the contents of the input.

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Xml;
    using System.IO;
    using System.Text;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    
    namespace SoapRequestEcho
    {
      [WebService(
      Namespace = "http://soap.request.echo.com/",
      Name = "SoapRequestEcho")]
      public class EchoWebService : WebService
      {
    
        [WebMethod(Description = "Echo Soap Request")]
        public XmlDocument EchoSoapRequest(int input)
        {
          // Initialize soap request XML
          XmlDocument xmlSoapRequest = new XmlDocument();
    
          // Get raw request body
          Stream receiveStream = HttpContext.Current.Request.InputStream
    
          // Move to begining of input stream and read
          receiveStream.Position = 0;
          using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
          {
            // Load into XML document
            xmlSoapRequest.Load(readStream);
          }
    
          // Return
          return xmlSoapRequest;
        }
      }
    }
    

    NOTE: Updated to reflect Johns comment below.

    0 讨论(0)
  • 2020-12-10 15:50

    An alternative to SoapExtensions is to implement IHttpModule and grab the input stream as it's coming in.

    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, 0, buffer.Length);
            context.Request.InputStream.Position = 0;
    
            string soapMessage = Encoding.ASCII.GetString(buffer);
    
            // Do something with soapMessage
        }
    
        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }
    
    0 讨论(0)
  • 2020-12-10 15:53

    I assume you are wanting to log the SOAP request for tracing; perhaps you have a consumer of your service that is telling you they're sending you good SOAP, but you don't believe them, yes?

    In that case, you should (temporarily) enable trace logging on your service.

    If you are trying to do general purpose logging, don't bother with the SOAP packet, since it's heavy; your logs would bloat up quick. Just log the important stuff, like e.g. "Add called, X=foo, Y=bar".

    0 讨论(0)
提交回复
热议问题