1

我注意到如果一个字段被指定为非强制性的,当我们生成代理类时,它也会生成一个与该字段相关的关联的“[fieldname]IsSpecified”布尔值。在使用 Fiddler 检查请求时,如果关联的“isSpecified”设置为 false,这意味着该字段将不会通过线路发送。

我有两个与此相关的问题

1.这有什么意义?纯粹是为了尽量减少通过网络发送的数据量吗?

2.如果没有任何值被传递到 web 服务的参数中,WCF 将为其使用默认数据类型。在整数字段的情况下,默认值为 0。因此,一旦在方法内部,如何判断这个 0 是由客户端为该字段发送的任何内容生成的,还是它们确实通过 0 发送?

4

1 回答 1

4

On your question 2, the Specified fields are not used only by the sending side. On the receiving side, the XML deserialiser will set the Specified fields according to the presence or absence of the corresponding fields on the wire, which allows the service methods to find out whether they were actually transmitted.

As for why you would want to do this apart from compactness of wire representation, an example I've seen is a service which allows you to update several fields in a record at once. In addition to setting the expected non-null values for fields, the service uses different wire representations for two special cases:

  • Update this field to null: <field1 xsi:nil="true" />
  • Don't update this field: The XML element is omitted altogether.
于 2013-08-13T09:10:45.107 回答