简体   繁体   English

wcf odata客户端-如何忽略请求中的空属性

[英]wcf odata client - how to ignore null properties in request

I'm a freshman in this area and currently working with WCF/Odata client to test an odata service. 我是该领域的新生,目前正在与WCF / Odata客户端合作以测试odata服务。

When trying to add some new entity via "DataServiceContext", all the properties with no values assigned will take null values as default, no problem. 尝试通过“ DataServiceContext”添加一些新实体时,所有未分配值的属性将采用空值作为默认值,这没有问题。 When the "add" action is performed by sending the request, the XML/JSON body will always contain the "null" properties, which will cause some issues there. 通过发送请求执行“添加”操作时,XML / JSON主体将始终包含“空”属性,这将在此处引起一些问题。 We cannot make changes to our service for some historical reason. 由于某些历史原因,我们无法更改服务。

Is there any way to configure behaviors of the "DataServiceContext" to ignore all those null properties when serializing the entity objects to be JSON/XML? 在将实体对象序列化为JSON / XML时,是否有任何方法可以配置“ DataServiceContext”的行为以忽略所有这些null属性? Thanks a lot! 非常感谢!

Regards, Peter 问候,彼得

I am afraid there isn't such flag to let you ignore all those null properties. 恐怕没有这样的标记可以让您忽略所有这些null属性。

But You could try RequestPipeline to work around. 但是您可以尝试使用RequestPipeline来解决。

dsc.Configurations.RequestPipeline.OnEntryStarting((arg) =>
{
    arg.Entry.Properties = arg.Entry.Properties.Where((prop) => prop.Value != null );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM