3

We have noted that WCF DataContractSerializer isn't very efficient when it comes to serializing null values during an SOAP call. For audit purposes, we also record the exact message as sent to disk, so this wastes storage. A typical message with more than one nil element is sent like so:

<MyMessage xmlns="myXmlns">
    <field0>1234567</field0>
    <field1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
    <field2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
    <field3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
    // etc
</MyMessage>

What would be an immeasurable improvement for bandwidth and disk considerations would be to generate a SOAP body such as:

<MyMessage xmlns="myXmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <field0>1234567</field0>
    <field1 xsi:nil="true" />
    <field2 xsi:nil="true" />
    <field3 xsi:nil="true" />
    //etc 
</MyMessage>

So my question is, how can I change my WCF client to consolidate the XSI namespace on the root element, to prevent it from being repeated on each nil element?

4

1 回答 1

2

You can't tweak the serializer, however if you implement a message inspector you can manipulate to your hearts content.

If you want to drop the nulls altogether though, and never see if it's not present you could decorate it

[DataMember(EmitDefaultValue=false)]
于 2012-08-26T17:32:55.973 回答