6

I am generating a datacontract with svcutil from a webservice.

svcutil /language:cs /noConfig /targetclientversion:Version35 
        /out:Generated\ProductService.cs http://example.com/ProductService.svc?wsdl

The fields generated looks like this:

private System.Nullable<System.DateTime> createdField;
private bool createdFieldSpecified;

How can the fields be both nullable and have a specified field?

SteveC
  • 15,808
  • 23
  • 102
  • 173
adrianm
  • 14,468
  • 5
  • 55
  • 102

2 Answers2

6

it depends on the source Wsdl. I bet there is something this (not sure of the syntax):

<xsd:element name="created" type="xsd:datetime" minOccurs="0" xsd:nil="true" />

svcutil.exe use nillable to produce a Nullable<> field, and minOccurs to produce a field + specified combination.

I also bet the WSDL is not a .Net generated WSDL !

Steve B
  • 36,818
  • 21
  • 101
  • 174
  • That is exactly how it looks. Now I understand. Thanks. You are wrong on .Net. Some other things in the wsdl shows that it is definitely a .Net service – adrianm Jun 14 '11 at 09:49
1

The class generation is driven by XSD schema of the web service.

In order to generate nullable fields. The field should be marked as nillable.

<xs:element minOccurs="0" maxOccurs="1" name="created" type="xs:dateTime" nillable="true" />

The XML will look like this.

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <created xsi:nil="true" />
</root>

I believe that this field in your schema looks like this:

<xs:element minOccurs="0" maxOccurs="1" name="created" />

and it would omit the element completely if createdFieldSpecified = false:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</root>

The bottom line: web service schema should be updated in order to generate nullable fields with svcutil.

Alex Aza
  • 76,499
  • 26
  • 155
  • 134