SvcUtil generates “Order” Named Parameter for xsd:all complexcontent

巧了我就是萌 提交于 2020-01-01 15:39:30

问题


Here is my dilemma, the svcutil command still generates Order Named parameter for complex content who are marked as xsd:all.

Here is my sample schema.

<xsd:complexType name="SpecialReportEntityRow" >
    <xsd:complexContent>
    <xsd:extension base="list:BaseRowExt">
   <xsd:all>
          <xsd:element name="customerName" type="xsd:string" form="unqualified" minOccurs="0" maxOccurs="1" />
    <xsd:element name="Id" type="xsd:long" form="unqualified" minOccurs="0" maxOccurs="1"  />
          <xsd:element name="certificateType" type="xsd:string" form="unqualified" minOccurs="0" maxOccurs="1" />
          <xsd:element name="certificateValidity" type="xsd:long" form="unqualified" minOccurs="0" maxOccurs="1" />
          <xsd:element name="item" type="xsd:long" form="unqualified" minOccurs="0" maxOccurs="1" />
          <xsd:element name="description" type="xsd:string" form="unqualified" minOccurs="0" maxOccurs="1" />
          <xsd:element name="quantity" type="xsd:long" form="unqualified" minOccurs="0" maxOccurs="1" />
          <xsd:element name="startDate" type="xsd:dateTime" form="unqualified" minOccurs="0" maxOccurs="1" />
          <xsd:element name="endDate" type="xsd:dateTime" form="unqualified" minOccurs="0" maxOccurs="1" />
      </xsd:all>
  </xsd:extension>
    </xsd:complexContent>
 </xsd:complexType> 

For the above schema, Here is the snippet generated proxy. Please see that it generates Order Named Parameter in the Attribute. Despite the schema contains xsd:all content.

The main problem is that the order in which the service sends that data is alphabetic or any random order, hence the schema has xsd:all instead of xsd:sequence. Now at runtime if we have order named parameter the object SpecialReportEntityRow is initialized to default values not with the service data. E.g. CertificateType property is initialized to empty string, CertificateValidaty intialized to 0, all the properties are getting initialized to their default values. There is no deserialization error thrown at runtime.

If i modify the proxy class by removing the XmlElementAttribute Order named parameter then at runtime the object SpecialReportEntityRow is getting proper service data.

Could anyone guide me how do i resolve this issue, i do not want to modify the proxy class and why does xsd:all is not considered by svcutil.

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)]
    public string customerName
    {
        get
        {
            return this.customerNameField;
        }
        set
        {
            this.customerNameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=1)]
    public long orgId
    {
        get
        {
            return this.orgIdField;
        }
        set
        {
            this.orgIdField = value;
        }
    }

Sample Response XML:

<ns5:rows xsi:type="ns7:SpecialReportEntityRow">
           <certificateType>Dummy Type 1</certificateType>
           <certificateValidity>2</certificateValidity>
           <customerName>Customer1</customerName>
           <description>Revocations by Reason - Unused</description>
           <item>17</item>
        </ns5:rows>
        <ns5:rows xsi:type="ns7:SpecialReportEntityRow">
           <certificateType>Dummy Type 2</certificateType>
           <certificateValidity>2</certificateValidity>
           <customerName>Custome1</customerName>
           <description>Revocations by Reason- Ca Compromise</description>
           <item>19</item>
        </ns5:rows>

回答1:


There's no way that I know of to suppress the "Order" attribute with svcutil, so we've written a Powershell script that strips it out. It's dirty, but it works and we don't have to worry about manually stripping it out when we regenerate.

We use a powershell script to generate our proxy classes which strips out the "Order" attributes:

svcutil /serializer:XmlSerializer '..\.\Schema\MyService.wsdl' '/n:*,MyService.GeneratedCode'  '/o:MyServiceProxy.cs'  '/nologo' 


(Get-Content .\o:MyServiceProxy.cs) | 
Foreach-Object {
$_ -replace ", ReplyAction=`"\*`"", "" `
       -replace ", Order=.", "" `
       -replace "Order=.", ""
} | 
Set-Content .\o:MyServiceProxy.cs



回答2:


You can also try this : http://wscfblue.codeplex.com

Works pretty well for me



来源:https://stackoverflow.com/questions/5384254/svcutil-generates-order-named-parameter-for-xsdall-complexcontent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!