是否还有其他原因,我应该考虑省略发射的默认值是一个坏的做法呢?是一个、默认值、做法、原因

2023-09-04 11:38:24 作者:你要辛福

我与WCF工作了一段时间,现在和地方客户端和服务器往往是共同发布;也就是说,新的版本已几乎总是被释放的同时。互操作性和版本不是问题(在这种情况下,至少)。

I've worked with WCF for awhile now and in places where both client and server tend to be co-released; that is, new versions have almost always been released at the same time. Interoperability and versioning aren't issues (in this case at least).

MSDN文档, DataMemberAttribute.EmitDefaultValue 和数据协定版本,表明它是一个不好的做法发出默认值,除非有特定的需要,并且支持版本。

The MSDN documentation, DataMemberAttribute.EmitDefaultValue and Data Contract Versioning, suggests it is a bad practice to emit the default value unless there is a specific need and to support versioning.

在实践中,我发现它很有用,有时关键省略默认值,特别是当一个WCF服务必须回叫多个客户端。在高负荷的时候,更大的信息放置在服务器上的高内存pressure,并需要更长的时间来传输。

In practice, I've found it useful and at times critical to omit the default value, especially when a WCF service must call back multiple clients. At times of high load, the bigger messages place high memory pressure on the server and take longer to transmit.

还有没有其他原因,这应该避免?

Are there any other reasons why this should be avoided?

推荐答案

我还使用 DataMemberAttribute.EmitDefaultValue = FALSE 在一些点尝试和限制数据量收发。就我而言,我在客户端和服务器方面的事情的控制权,所以我没有任何问题了。

I am also using DataMemberAttribute.EmitDefaultValue = false in some spots to try and limit the amount of data being transmitted. In my case, I am in control of both the client and server side of things, so I don't have any problems with it.

我没有找到一个参考以一个潜在的冲突与 DataMemberAttribute.IsRequired ,我不知道之前:

I did find a reference to a potential conflict with DataMemberAttribute.IsRequired, which I didn't know about before:

互动IsRequired

...如果 IsRequired 设置为   真正,(这表明一个值必须是present)和    EmitDefaultValue 设置为错误(表示该值不能   是present如果它被设置为默认值),默认值这个   数据成员不能被序列化,因为其结果将是   矛盾的。如果这样的数据成员设置为它的默认值   (通常为空或零)和序列化尝试,一   SerializationException被抛出。

...If IsRequired is set to true, (which indicates that a value must be present) and EmitDefaultValue is set to false (indicating that the value must not be present if it is set to its default value), default values for this data member cannot be serialized because the results would be contradictory. If such a data member is set to its default value (usually null or zero) and a serialization is attempted, a SerializationException is thrown.

通常情况下,这不应该是一个问题,因为一旦你尝试序列化一个对象标记为成员 EmitDefaultValue = FALSE IsRequired = TRUE 和默认值时,你会得到一个 SerializationExeception ,所以这个问题是非常明显的(我只是测试它)。不过,我可以看到的情况,其中 EmitDefaultValue ,并在一段时间以后, IsRequired 设置为,产生的问题(希望陷入测试的变化部署之前)。

Normally, this shouldn't be a problem because as soon as you try to serialize an object with a member marked with EmitDefaultValue = false, IsRequired = true, and a default value, you get a SerializationExeception, so the problem is very obvious (I just tested it out). However, I could see situations where the EmitDefaultValue is false and at some later time, IsRequired is set to true, creating problems (hopefully caught in testing before the change is deployed).

一个更可能出现的问题与该组合:一个客户端可以具有缺省值发送数据,并且,这将不会有问题反序列化。然后,您的服务可能将其保存到数据库中,然后尝试把它背出来送,这会抛出异常。

One more possible problem with this combination: a client could send data with a default value, and this will be deserialized without a problem. Your service might then save it to the database, and then attempt to send it back out, which will throw an exception.

所有他这样说,我觉得你正在使用的设置文件中提到的具体原因。要知道潜在的冲突与 IsRequired

All that being said, I think you are using the setting for the specific reasons noted in the documentation. Just be aware of the potential conflict with IsRequired.