WCF:使用JSON编码SOAP消息正文 [英] WCF: Use JSON to encode a SOAP Message Body

查看:127
本文介绍了WCF:使用JSON编码SOAP消息正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有服务的SOAP实现,到目前为止,我们有一些遗留代码将args包装并返回到另一个对象中,以解决RPC方法上的某些序列化/泛型问题.

We have SOAP implementations of our services and up till now we had some legacy code that was wrapping our args and returns in another object to get around some serialization / generics on RPC methods.

优化后,我们实现了此类,以便Json序列化(DataContractJsonSerializer)并用GZipp压缩我们的复杂请求参数和响应对象.

After optimization, we had implemented this class so that it Json serialized (DataContractJsonSerializer) and GZipped our complex request params and response objects.

我现在想将这些内容下推到WCF堆栈中.我真正想要的是能够在标准SOAP服务上将消息正文编码为GZipped Json的功能.我们需要交易支持和安全性等,因此我们需要能够支持标准绑定.

I now want to push this stuff down into the WCF stack. What I really want is the ability to encode the message body as GZipped Json on a standard SOAP service. We need transactional support and security etc so we need to be able to support the standard bindings.

我设法实现了一个操作行为来对Json进行序列化,以便消息信息集将包含与json兼容的XML.然后,我想添加一个MessageEncoder,以将xml转换为消息正文中的json.这是我遇到的问题.我无法序列化整个消息,因为我们仍然具有标准的soap标头等,并且s:body在与json兼容的xml之前仍包含根Request或Response对象.我认为我可以使用JsonReaderWriterFactory只是不确定如何将其应用于消息的correc部分.

I have managed to implement an Operation Behavior to Json serialize so that the message infoset will contain json compatible XML. I then wanted to add a MessageEncoder to turn the xml into json in the message body. This is where I have issues. I can't serialize the whole message as we still have standard soap headers etc and the s:body still contains a root Request or Response object before the json compatible xml. I figure I can use the JsonReaderWriterFactory just not sure how to apply to the correc portion of the message.

如果我能弄清楚如何阅读和编写此Json部分,我知道以后可以做GZIP.

I know I can do the GZIP later if I can just figure out reading and writing this Json section.

有什么想法吗? 皮特

好.所以要澄清一下.

我的服务看起来像这样

[ServiceContract]
public interface IMyService {
  [OperationContract]
  Person SavePerson(Person personToSave);

  [OperationContract]
  Person GetPersons();
}

,GetPersons()方法的输出如下所示:

and the output from the GetPersons() method would look something like this:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
  <s:Header>
    <a:Action s:mustUnderstand="1">http://www.petegoo.com/wcf/MyService/IMyService/GetPersonsResponse</a:Action>
    <a:RelatesTo>urn:uuid:a18ccf1c-0793-4240-ba6f-9e86b6f2fdf6</a:RelatesTo>
  </s:Header>
  <s:Body>[{"DateOfBirth":"\/Date(286801200000+1300)\/","FirstName":"Foo","Id":1,"LastName":"Bar"},{"DateOfBirth":"\/Date(333720000000+1200)\/","FirstName":"Foo","Id":1,"LastName":"Bar"}]</s:Body>
</s:Envelope>

注意:以上内容是临时性的,因此在语法或语义上可能不正确.

Note: The above was ad hoc so may not be syntactically or semantically correct.

推荐答案

啊!只要意识到这有多大.万一对其他人有用,它将离开这里.

Ahh!! Just realise how old this is. Will leave here in-case it works for anyone else.

我本人对此并不陌生,但作为建议,您是否尝试过研究MessageInspectors?您可以将它们添加到clientRuntimedispatchRuntime中,以在WCF发送SOAP消息之前和之后播放" SOAP消息.

I'm fairly new to all this myself but, as a suggestion, have you tried looking into MessageInspectors? You can add these to the clientRuntime and dispatchRuntime to "play" with SOAP messages before and after they are sent by WCF.

通过实现IDispatchMessageInspector和/或IClientMessageInspector,您可以在发送消息之前接收到呼叫,读取当前的soap消息xml,更改body元素的内容以包含压缩内容(也许将其添加为新的子节点),然后重新生成消息,并将其传递回WCF以发送出去.在接收端,您再次在AfterReceiveRequest中抓取消息,然后逆转该过程.

By implementing IDispatchMessageInspector and / or IClientMessageInspector you could receive a call before the message is sent, read the current soap message xml, alter the content of the body element to contain your zipped content (perhaps adding it as a new child node) and then re-build the message and pass it back to WCF to send out. At the receiving end you grab the message again in AfterReceiveRequest and reverse the process.

不知道这是否对您有用,但希望对您有所帮助. W

Don't know if this will work for you but hope it helps. W

这篇关于WCF:使用JSON编码SOAP消息正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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