WCF Data Services: How to avoid the __metadata members

有些话、适合烂在心里 提交于 2019-12-19 07:39:37

问题


I'm currently experimenting with using WCF Data Services as a way to open up our product a bit more while at the same time being able to consume the data from AJAX. One thing that I noticed is that the JSON represention format has these __metada members sprinkled across the data, and in the OData documentation I found this (under Representing Entries):

An optional "__metadata" name/value pair is the only pair that should be included on the object that does not directly represent a Property of the Entry being represented. This name/value pair is not data, but instead, by convention defined in this document, specifies the metadata for the Entry being represented.

My question is, if this metadata is optional, how do I turn it off to save bandwidth?


回答1:


You are right that the WCF Data Services server implementation does not have the ability to not send the __metadata. The fact that the protocol makes it optional doesn't mean that a specific implementation won't send it always. In this case the server generates the __metadata because it may contain vital information for the client and without the the payload might not make sense or provide incomplete information. Currently the server also doesn't provide any option to change this behavior.




回答2:


After spending some time in Reflector, it looks like the only way to do it is by writing my own version of System.Data.Services.Serializers.JsonSerializer. The method that writes the metada object, WriteMetadataObject, is called from WriteComplexTypeProperties and WriteElementWithName without considering any setting that would skip that portion.

Now I have to consider how much work it is to write a serializer that will write out the required JSON, and hook that into the pipeline with a custom $format name.



来源:https://stackoverflow.com/questions/3492706/wcf-data-services-how-to-avoid-the-metadata-members

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!