cagin
cagin

Reputation: 5930

How to see SOAP message as web service method calling?

I want to see what I send to web service as calling a method in .Net. For example :

var list = service.SomeWebMethd(req);

I want to see what I send to web serviceas SOAP message. What shoul I do?

Upvotes: 1

Views: 287

Answers (1)

J2D8T
J2D8T

Reputation: 825

After a lot of searching and asking questions I managed to write this class specifically for C# that grabs the SOAP request and response envelopes. Hope this could help you too.

First create a new class and copy paste this code as is just change the namespace.

using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;

//Set namespace to the project name
namespace yourProjectName // <-- EDIT
{
    class SOAPRequestResponse : IEndpointBehavior
    {
        public string lastRequestXML
        {
            get
            {
                return soapInspector.lastRequestXML;
            }
        }

        public string lastResponseXML
        {
            get
            {
                return soapInspector.lastResponseXML;
            }
        }

        private MyMessageInspector soapInspector = new MyMessageInspector();

        public void AddBindingParameters(ServiceEndpoint endPoint, BindingParameterCollection bindingParameters)
        {

        }

        public void ApplyDispatchBehavior(ServiceEndpoint endPoint, EndpointDispatcher endPointDispatcher)
        {

        }

        public void Validate(ServiceEndpoint endPoint)
        {

        }

        public void ApplyClientBehavior(ServiceEndpoint endPoint, ClientRuntime clientRuntime)
        {
            clientRuntime.MessageInspectors.Add(soapInspector);
        }

        public class MyMessageInspector : IClientMessageInspector
        {
            public string lastRequestXML { get; private set; }
            public string lastResponseXML { get; private set; }

            public void AfterReceiveReply(ref Message reply, object corActionState)
            {
                lastResponseXML = reply.ToString();
            }

            public object BeforeSendRequest(ref Message request, IClientChannel channel)
            {
                lastRequestXML = request.ToString();
                return request;
            }
        }
    }
}

Secondly you need to create a new instance of the SOAPRequestRespone class in your main form.

SOAPRequestResponse soapENV = new SOAPRequestResponse();

You will then have to add it to the proxy class like so (also in main form):

service.Endpoint.Behaviors.Add(soapENV);

Finally you can assign the request and response envelopes to string variables like this:

string request = soapENV.lastRequestXML;
string response = soapENV.lastResponseXML;

Hope this helps you as well. There are other tools you can use like SOAPUI.

Upvotes: 1

Related Questions