C# WCF:有没有办法删除 ExtensionData 字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/635838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:11:16  来源:igfitidea点击:

WCF: Is there a way to remove ExtensionData field?

c#wcfserialization

提问by

I just started using WCF and I already came to a project-altering issue. I created a service and put in reference in a webservice, but the every field in the webservice xml file comes with an ExtensionData field.

我刚开始使用 WCF,我已经遇到了一个改变项目的问题。我创建了一个服务并在 webservice 中引用,但是 webservice xml 文件中的每个字段都带有一个 ExtensionData 字段。

Example:

例子:

removed dead ImageShack link

删除了无效的 ImageShack 链接

I don't want this. I need it to be only:

我不要这个。我只需要它:

removed dead ImageShack link

删除了无效的 ImageShack 链接

Is there a way to remove this field? Some different kind of serialization?

有没有办法删除这个字段?某种不同类型的序列化?

回答by Mike_G

ExtensionData is used to maintain compatibility across services that may share contracts of different versions. It can be safely ignored when passing messages.

ExtensionData 用于维护可能共享不同版本合约的服务之间的兼容性。传递消息时可以安全地忽略它。

You may be able to get rid of it by using something other than the DataContract serializer (im thinking old school [Serializable]), but i could be wrong.

您可以通过使用 DataContract 序列化器以外的其他东西来摆脱它(我认为老派 [Serializable]),但我可能是错的。

回答by Darin Dimitrov

The ExtensionData is actually a feature that must be built into the type to enable round-tripping. It is always emitted by the DataContractSerializer. One possible way of suppressingthis field is using the older XmlSerializer by decorating your service contract interface with the XmlSerializerFormatAttribute.

ExtensionData 实际上是一个必须内置到类型中才能启用往返的功能。它总是由 DataContractSerializer 发出。抑制此字段的一种可能方法是通过使用 XmlSerializerFormatAttribute 装饰您的服务协定接口来使用旧的XmlSerializer

回答by Joe

Not answering the question, but maybe this helps...

不回答问题,但也许这有帮助......

From MSDN http://msdn.microsoft.com/en-us/library/ms731083.aspx:

来自 MSDN http://msdn.microsoft.com/en-us/library/ms731083.aspx

The round-tripping feature may be turned off, either by setting ignoreExtensionDataObject to true in the DataContractSerializer constructor or by setting the IgnoreExtensionDataObject property to true on the ServiceBehaviorAttribute. When this feature is off, the deserializer will not populate the ExtensionData property, and the serializer will not emit the contents of the property.

可以通过在 DataContractSerializer 构造函数中将 ignoreExtensionDataObject 设置为 true 或在 ServiceBehaviorAttribute 上将 IgnoreExtensionDataObject 属性设置为 true 来关闭往返功能。当此功能关闭时,反序列化器不会填充 ExtensionData 属性,并且序列化器不会发出该属性的内容。

回答by marc_s

Most likely, your DataContract classes will be implementing the IExtensibleDataObjectinterface, right? That's responsible for the ExtensionDatafield - just remove that interface, and you should be able to remove the ExtensionDatafields, too, from your DataContracts.

最有可能的是,您的 DataContract 类将实现该IExtensibleDataObject接口,对吗?这对ExtensionData字段负责- 只需删除该接口,您也应该能够ExtensionData从您的 DataContracts 中删除这些字段。

Marc

马克

回答by Bwing

decorate your ServiceImplementation with [ServiceBehavior(IgnoreExtensionDataObject=true)]

用 [ServiceBehavior(IgnoreExtensionDataObject=true)] 装饰你的 ServiceImplementation

[ServiceBehavior(IgnoreExtensionDataObject=true)]
public class Service : IService {}

回答by Johnny5

Setting the property to nullprevent it to appear in the resulting xml :

设置属性以null防止它出现在生成的 xml 中:

foreach(var elem in arrayOfElements)
{
    elem.ExtensionData = null;
}

回答by Valerio Gentile

If your issue is in an assertion of a [TestMethod] you can use http://www.fluentassertions.comto exclude every ExtensionData properties from your assertions.

如果您的问题是在 [TestMethod] 的断言中,您可以使用http://www.fluentassertions.com从您的断言中排除每个 ExtensionData 属性。

Eg.

例如。

objToCheck.ShouldBeEquivalentTo(expectedObj, options => options.Excluding(o => o.ExtensionData));

回答by BehrouzMoslem

Your class should not inherit IExtensibleDataObject.

您的类不应继承 IExtensibleDataObject。

Please refer to the description of the Round-tripping below:

请参考下面的往返描述:

The Round-tripping occurs when data is transferred from a new version to an old version and transferred to the new version of the data contract. Bypass ensures that no data is lost. Turning off bypass makes the type ahead compatible with any future changes supported by the contract version model.

当数据从新版本传输到旧版本并传输到数据合约的新版本时,发生往返。Bypass 确保不会丢失任何数据。关闭绕过使类型提前与合约版本模型支持的任何未来更改兼容。

To enable trip-tripping for a particular type, the type should execute the

要为特定类型启用跳闸,该类型应执行

IExtensibleDataObject

可扩展数据对象

interface.

界面。

The user interface contains an ExtensionData (ExtensionDataObject) attribute. The property stores any information from future versions of the data contract that is unknown to the current version.

用户界面包含一个 ExtensionData (ExtensionDataObject) 属性。该属性存储来自当前版本未知的数据协定未来版本的任何信息。