Why is a message in WCF seemingly always in SOAP f

2019-07-28 11:21发布

问题:

I have implemented a message inspector in WCF by implementing IDispatchMessageInspector.

Putting a break point on this method...

public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
    // Impementation
}

... I can look at the request object to see what is inside.

Clearly I dont understand WCF enough because whatever endpoint binding I use (basichttp, nettcp and netpipe) the message inside is always represented in SOAP format e.g.

<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">
    <s:Header>
        -- headers --
    </s:Header>
    <s:Body>
        -- body --
    </s:Body>
</s:Envelope>

Is this because doing a .ToString() on the request object just represents the message in SOAP format?

I imagined that using another protocol e.g. netTcp would result in a different message payload.

Also lets say I wanted to represent my data in JSON format how would I go about doing this? Or would I end up with JSON formatted data structures inside a SOAP envelope?

回答1:

It is because all bindings you mentioned are designed to use SOAP protocol. They are using either TextMessageEncoder or BinaryMessageEncoder and both these work with SOAP envelopes (except the situation where you use TextMessageEncoder in custom binding with MessageVersion set to None).

The only out-of-the box binding allowing other message formats is WebHttpBinding which uses WebMessageEncoder supporting both XML and JSON.