Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

visual studio 2010 - .NET auto-generated WSDL client does not handle abstract type when maxOccurs attribute is used

I have a rather complicated WSDL that I am adding as a service reference in Visual Studio. The problem I am having is that the auto-generated client code is not properly handling an abstract type (and ignoring all the types that substitute for that abstract type). I don't want to post the entire WSDL but here is a snippet:

<complexType name="AddTextFields">
    <complexContent>
        <extension base="ti:TransformationInstructions">
            <sequence>
                <element name="textFieldList"
                    type="atf:TextFieldList" />
            </sequence>
        </extension>
    </complexContent>
</complexType>

<complexType name="TextFieldList">
    <sequence>
        <element ref="atf:TextFieldBase"
            maxOccurs="unbounded" />
    </sequence>
</complexType>

<element name="TextFieldBase" abstract="true"/>

<element name="textField" substitutionGroup="atf:TextFieldBase">
  <complexType>
    <sequence>
      ...
    </sequence>
  </complexType>
</element>

<element name="checkBox" substitutionGroup="atf:TextFieldBase">
  <complexType>
    <sequence>
      ...
    </sequence>
  </complexType>
</element>

So you can see here TextFieldList can have unlimited elements of type TextFieldBase (which can be textField, checkBox and some others that I omitted). However, when creating the WSDL client, .NET doesn't seem to care about this and generates textFieldList like so:

[System.Xml.Serialization.XmlArrayAttribute(Order=0)]
    [System.Xml.Serialization.XmlArrayItemAttribute("TextFieldBase", IsNullable=false)]
    public object[] textFieldList {
        get {
            return this.textFieldListField;
        }
        set {
            this.textFieldListField = value;
            this.RaisePropertyChanged("textFieldList");
        }
    }

I have some other abstract types in the WSDL that do work correctly and in trying to compare them I discovered that if I remove the maxOccurs="unbounded" attribute from TextFieldList definition, then .NET will correctly generate the types that plug into the abstract type. Unfortunately, we need to be able to have 1 or more textFieldList elements so that will not work.

Is this just a bug in .NET? Or is there some workaround that would work without drastically changing the WSDL?

One workaround I considered was changing the implementation to be a choice instead of using abstract:

<complexType name="TextFieldList">
<choice maxOccurs="unbounded">
    <element ref="atf:textField" />
    <element ref="atf:checkBox" />
    <element ref="atf:radioButtonGroup" />
    <element ref="atf:listBox" />
    <element ref="atf:comboBox" /> 
</choice>
</complexType>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Moving the maxOccurs attribute 'fixes' this.

<complexType name="TextFieldList">
    <sequence maxOccurs="unbounded">
        <element ref="atf:TextFieldBase" />
    </sequence>
</complexType>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...