开发者

Using a custom WCF serializer in Silverlight

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. prov开发者_开发问答ide a different serialization engine. I will provide the engine (protobuf-net) - I just need to get WCF to use it!

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.

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


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.

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.

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.


Further to Carlos' answer, since WP7.1 (Mango) is SL4 compatible, it might have the extensibility points required.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜