2010-04-06 17 views

risposta

11

Un'alternativa a SoapExtensions è quello di implementare IHttpModule e afferrare il flusso di input come è in arrivo.

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(); 
    } 
} 
+2

forse ovvio, ma potrebbe essere necessario registrare l'IHttpModule nel web.config

1

Presumo si vogliono registrare la richiesta SOAP per il tracciamento; forse hai un consumatore del tuo servizio che ti sta dicendo che ti sta mandando un buon SOAP, ma non ci credi, sì?

In tal caso, è necessario (temporaneamente) enable trace logging on your service.

Se si sta tentando di eseguire la registrazione generica, non preoccuparsi del pacchetto SOAP, poiché è pesante; i tuoi registri si gonfieranno rapidamente. Basta registrare le cose importanti, come ad es. "Aggiungi chiamato, X = pippo, Y = barra".

+1

Sfortunatamente deve essere permanente, quindi non posso abilitare la traccia solo temporaneamente. –

+0

@bangoker: Sebbene tu abbia trovato una soluzione che soddisfa le tue necessità immediate, ti suggerisco di riconsiderare il tuo corso. La registrazione di ogni richiesta SOAP è pesante ed estremamente inutile. – Randolpho

6

Sì, lo puoi fare usando SoapExtensions. Ecco un nice article che viene eseguito attraverso il processo.

+0

Penso che sia quello che stavo cercando, grazie! –

+4

Il link è rotto. – wRAR

+0

Smarcare come la risposta in quanto il collegamento è rotto –

5

È possibile anche leggere il contents of the Request.InputStream.

In questo modo è più utile per i casi in cui si desidera eseguire la convalida o altre azioni all'interno del metodo Web a seconda del contenuto dell'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; 
    } 
    } 
} 

NOTA: aggiornato per riflettere Johns commento qui sotto.

+0

Hai provato questo codice? Tra l'altro, non penso che dovresti usare i blocchi 'using', dal momento che non stai creando il' Stream'. Anche lo 'StreamReader' non dovrebbe essere in un blocco' using', dato che lo smaltimento chiuderà anche il flusso e lo stream non appartiene a te. Mi chiedo anche se questo interagirà con qualsiasi 'SoapExtension' che può essere successivamente configurato. –

+0

Sì, ho provato e funziona bene (prova a cliccare sul link), come per usare o non usare, è solo una scelta del programmatore di come vuoi strutturare il tuo codice. –

+1

@Steven: in questo caso ti stai sbagliando sull'uso di "uso". Se fosse 'using (var x = new StreamReader())' allora sarebbe una scelta - lo 'StreamReader' appartiene a te perché lo hai creato. In questo caso, il 'Stream' appartiene all'oggetto' Request' - non l'hai creato, quindi non dovresti chiamare 'Dispose' su di esso - mai. –

Problemi correlati