Prevent svcutil from generating "EmitDefaultValue=false" attributes
I've recently been trying to generate data contracts from xsd files, using svcutil like this:
svcutil.exe /t:code /dconly /out:MyContract.cs /n:*,My.Namespace MyDataDefinition.xsd
The XSD mostly consists of definitions like this:
<xsd:complexType name="SomeComplexObjectTyp开发者_JS百科e">
<xsd:sequence>
<xsd:element name="FirstData" type="xsd:string" minOccurs="0" />
<xsd:element name="SecondData" type="xsd:string" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>
svcutil generates something like this:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="SomeComplexObjectType")]
public partial class PersonInfo : object, System.Runtime.Serialization.IExtensibleDataObject
{
private string FirstDataField;
private string SecondDataField;
[System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false)]
public string FirstData
{
get
{
return this.FirstDataField;
}
set
{
this.FirstDataField= value;
}
}
[System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false)]
public string SecondData
{
get
{
return this.SecondDataField;
}
set
{
this.SecondDataField= value;
}
}
}
Which works fine, however, the "EmitDefaultValue=false" attributes are not necessary. Not to mention that it introduces a lot of noise into the wsdl, adding stuff like this:
<xsd:element minOccurs="0" name="FirstData" nillable="true" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<DefaultValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/" EmitDefaultValue="false"/>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
Currently I'm just hand-editing the generated contract, but that is not ideal from a maintenance standpoint.
Does anyone know how to prevent svcutil from automatically generating these EmitDefaultValue=false attributes?
Please see this article.
On schema import, the EmitDefaultValue property is automatically set to false whenever the WCF-specific annotation mentioned previously is detected. It is also set to false for reference types that have the nillable property set to false to support specific interoperability scenarios that commonly occur when consuming ASP.NET Web services
It appears your only choice is to modify the incoming schema so the relevant elements are nillable.
Remember SvcUtil is just a tool. If it doesn't meet your requirements, write your own tool!
You could use PowerShell to call SvcUtil and edit the generated file whith your custom changes.
A quick example:
#Call SvcUtil
& svcutil #Whatever options you want go here
#Read the generated code
$thierCode = Get-Content "generatedFile.cs"
#Copy each line, editing as you go:
$myCode = @("//This file was generated using MySvcUtil.ps1")
foreach($line in $thierCode)
{
$myCode += $line.Replace("EmitDefaultValue=false", "")
}
#Write the edited code back into the .cs file.
$myCode | Set-Content "generatedFile.cs"
精彩评论