Using a custom WCF serializer in Silverlight

前端 未结 2 808
深忆病人
深忆病人 2020-12-30 05:41

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

相关标签:
2条回答
  • 2020-12-30 06:22

    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.

    0 讨论(0)
  • 2020-12-30 06:25

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

    0 讨论(0)
提交回复
热议问题