使用Silverlight中自定义的WCF序列化 [英] Using a custom WCF serializer in Silverlight

查看:166
本文介绍了使用Silverlight中自定义的WCF序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在满。NET是pretty的容易掉出来的串行 - 无论是在配置,或者通过自定义属性。不过,我想在Silverlight中的WCF类似的东西 - 即提供不同的序列化引擎。我公司将提供引擎(protobuf网) - !我只需要得到WCF使用它

In "full" .NET it is pretty easy to swap out the serializer - either in configuration, or via custom attributes. However, I want to do something similar in Silverlight's WCF - i.e. provide a different serialization engine. I will provide the engine (protobuf-net) - I just need to get WCF to use it!

最后一次我看到这个(前一段时间)没有很好的选择是显而易见的。我最好的杂牌的是,公开数据为字节[] - 但这失去了所有的MEX善良。这是一种耻辱,因为它所有的pretty的,很多的只是工作的在完整的.NET。

Last time I looked at this (some time ago) no good options were obvious. My best kludge was to expose the data as byte[] or Stream - but this loses all the "mex" goodness. Which is a shame, as it all pretty-much just works in full .NET.

这是还可能吗?对于WP7加分太:)

Is this yet possible? Bonus points for WP7 too :)

推荐答案

这是可能的,但它是很困难的事情。类DataContractSerializerOperationBehavior(您通常会通过覆盖CreateSerializer方法取代串行)是内部的SL,所以你不能真正使用它。什么你需要做的是创造一个IOperationBehavior它做了DCSOB做什么 - 即,设置客户端将使用该消息体(XML信息集)和运行参数/返回值之间进行转换的IClientMessageFormatter。这将意味着写code从Message对象包装/拆开包装,从操作参数为/。并非完全不可能,但它应该工作。

It is possible, but it's quite hard to do. The class DataContractSerializerOperationBehavior (where you'd usually replace the serializer by overriding the CreateSerializer method) is internal in SL, so you can't really use it. What you'd need to do is to create an IOperationBehavior which did what the DCSOB does - namely, set the IClientMessageFormatter which the client will use to convert between the Message body (XML Infoset) and the operation parameters / return values. That would mean writing the code to wrap / unwrap the parameters from the operation into / from the Message object. Not completely impossible, but it should work.

至于WP7,它不具备所需的此溶液(IOperationBehavior,IClientMessageFormatter)的可扩展性点不存在(它们被添加上SL4,和WP7与SL3大致兼容),所以不认为这是可能存在 - 或者至少在一个通用的方法(你可以有你所有的行动宣布​​与Message对象 - 消息MyOperation(信息输入) - 在那个阶段,你可以与消息信息集(GetReaderAtBodyContents处理)直接,但将需要在每一次操作完成的呼叫

As far as WP7, it doesn't have the extensibility points required for this solution (IOperationBehavior, IClientMessageFormatter) aren't there (they were added on SL4, and WP7 is roughly compatible with SL3), so I don't think it's possible there - or at least not in a generic way (you can have all your operations declared with Message objects - Message MyOperation(Message input) - and at that stage you can deal with the message Infoset (GetReaderAtBodyContents) directly, but that would need to be done in every operation call.

更新:好吧,我想,这是可以做到的它的Windows Phone 7(和Silverlight 3),以及,它只是变得更加困难。我重新使用一对自定义渠道的扩展点。如何可以做到这一点的描述可以在<一个发现href="http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/21/wcf-extensibility-extensibility-in-windows-phone-and-silverlight-3.aspx" rel="nofollow">http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/21/wcf-extensibility-extensibility-in-windows-phone-and-silverlight-3.aspx.

Updated: well, I tried and it is possible to do it in Windows Phone 7 (and Silverlight 3) as well, it's just a lot harder. I recreated the extensibility points using a pair of custom channels. The description of how this can be done can be found at http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/21/wcf-extensibility-extensibility-in-windows-phone-and-silverlight-3.aspx.

这篇关于使用Silverlight中自定义的WCF序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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