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?
This can be used to register system.serviceModel extensions without altering the tool's configuration file. Specifies the output to be generated by the tool. Valid values are code, metadata or xmlSerializer. Svcutil.exe can generate code for service contracts, clients and data types from metadata documents.
This can be defined either in the configuration file of Svcutil.exe, or in another configuration file specified using the /svcutilConfig option. The URL to a service endpoint that provides metadata or to a metadata document hosted online.
When the service binding is one of the system-provided bindings (see System-Provided Bindings ), and the ProtectionLevel property is set to either None or Sign, Svcutil generates a configuration file using the <customBinding> element instead of the expected system-provided element.
The WCF service must be running before the svcutil tool is started. The WCF service must expose its metadata using an HTTP port in addition to the WebSphere MQ custom channel endpoint references to generate a client directly from a running service. The custom channel must be registered in the configuration data for svcutil.
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 !
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With