为什么 WCF 中的消息似乎总是采用 SOAP 格式? [英] Why is a message in WCF seemingly always in SOAP format?

查看:12
本文介绍了为什么 WCF 中的消息似乎总是采用 SOAP 格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过实现 IDispatchMessageInspector 在 WCF 中实现了一个消息检查器.

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
}

... 我可以查看 request 对象,看看里面有什么.

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

显然我对 WCF 的理解不够,因为无论我使用什么端点绑定(basichttp、nettcp 和 netpipe),里面的消息总是以 SOAP 格式表示,例如

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>

这是因为对请求对象执行 .ToString() 只是表示 SOAP 格式的消息吗?

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

我想象使用另一种协议,例如netTcp 将导致不同的消息负载.

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

另外,假设我想以 JSON 格式表示我的数据,我将如何去做?或者我最终会在 SOAP 信封内得到 JSON 格式的数据结构?

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?

推荐答案

这是因为你提到的所有绑定都是为了使用 SOAP 协议而设计的.他们使用 TextMessageEncoderBinaryMessageEncoder 并且这两者都适用于 SOAP 信封(除了您在带有 的自定义绑定中使用 TextMessageEncoder 的情况)MessageVersion 设置为 None).

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).

唯一允许其他消息格式的开箱即用绑定是 WebHttpBinding,它使用支持 XML 和 JSON 的 WebMessageEncoder.

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

这篇关于为什么 WCF 中的消息似乎总是采用 SOAP 格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆