0

I have a wsdl that has a method to send data, instead of send it by this method I want to just get the soap string that would have been sent if I use the method.

If someone know if that possible or how to this, thanks for the helper,

Hay
  • 51
  • 1
  • 8
  • can you share sample source codes?. – Jar Yit Feb 22 '16 at 10:04
  • What for the soap string? I assume You want to have soap envelope – blogprogramisty.net Feb 22 '16 at 10:12
  • actualy I can't, and yes I need the soap envelpe to use it as a string, I'm adding a IBM MQ , so I need to send the soap via MQ. – Hay Feb 22 '16 at 10:16
  • I assume you are using WCF? If you are, you have to implement a MessageInspector and read the body on the BeforeSendRequest event. https://msdn.microsoft.com/en-us/library/aa717047(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.servicemodel.channels.message(v=vs.110).aspx – mnemonic Feb 22 '16 at 10:18
  • just for understanding the solution, is the MessageInspector give me the actual soap as a string? – Hay Feb 22 '16 at 10:27

1 Answers1

0

I obviously don't know what your architecture or infrastructure is like so the following might not be applicable, but it sounds like there might be a 'mix-up' with responsibilities. Instead of publishing the SOAP payload on a MQ it is better to publish only the data the consumer will need to do what it needs to do. E.g. you publish the SOAP envelope onto the queue, but what if there is another consumer also interested in this message? It might not want it in the SOAP format but in a JSON format, or it just wants to update a database table based on the data published. I would say rather push the 'raw' data and let the consumer decide what to do with it...

Anyway... A quick and dirty way to achieve this:

MessageInspector:

public class MessageInspector : 
    IClientMessageInspector
{
    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        var r = request.ToString(); // this will return the SOAP string...
        return request;
    }

    public void AfterReceiveReply(ref Message reply, object correlationState)
    {
        //throw new NotImplementedException();
    }
}

EndpointBehavior:

public class MessageInspectorBehavior : 
    IEndpointBehavior
{
    public void Validate(ServiceEndpoint endpoint)
    {
        //throw new NotImplementedException();
    }

    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
        //throw new NotImplementedException();
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        //throw new NotImplementedException();
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        clientRuntime
            .MessageInspectors
            .Add(new MessageInspector());
    }
}

And then setting the behavior on the client:

class Program
{
    static void Main(
        string[] args
        )
    {
        var ser = new ServiceReference1.Service1Client();
        ser.Endpoint
            .Behaviors
            .Add(new MessageInspectorBehavior());

        var f = ser.GetData(10);

        Console.WriteLine(f);
        Console.ReadKey();
    }
}

the problem here is that the call is still made to the service... The alternative is to create the SOAP message manually

Maybe this will help: How to post SOAP Request from .NET?

Community
  • 1
  • 1
mnemonic
  • 692
  • 1
  • 9
  • 18