Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF: collection proxy type on client

Tags:

wcf

I have the following type in wsdl (it is generated by third party tool):

<xsd:complexType name="IntArray">
  <xsd:sequence>
    <xsd:element maxOccurs="unbounded" minOccurs="0" name="Elements" type="xsd:int" /> 
  </xsd:sequence>
</xsd:complexType>

Sometimes Visual Studio generates:

public class IntArray : System.Collections.Generic.List<int> {}

And sometimes it doesn't generate any proxy type for this wsdl and just uses int[].

Collection type in Web Service configuration is System.Array.

What could be the reason for such upredictable behavior?

Edited:

I found the way how I can reproduce this behavior.

For examle we have two types:

<xsd:complexType name="IntArray">
  <xsd:sequence>
    <xsd:element maxOccurs="unbounded" minOccurs="0" name="Elements" type="xsd:int" /> 
  </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="StringArray">
  <xsd:sequence>
    <xsd:element maxOccurs="unbounded" minOccurs="0" name="Elements" type="xsd:string" /> 
  </xsd:sequence>
</xsd:complexType>

VS generates:

public class IntArray : System.Collections.Generic.List<int> {}

public class StringArray : System.Collections.Generic.List<string> {}

Now I change StringArray type:

<xsd:complexType name="StringArray">
  <xsd:sequence>
    <xsd:element maxOccurs="unbounded" minOccurs="0" name="Elements" type="xsd:string" /> 
    <xsd:any minOccurs="0" maxOccurs="unbounded" namespace="##any" processContents="lax" />
  </xsd:sequence>
  <xsd:anyAttribute namespace="##any" processContents="lax"/>
</xsd:complexType>

VS generates proxy type for StringArray only. But not for IntArray.

Edited:

Reference.svcmap:

  <ClientOptions>
    <GenerateAsynchronousMethods>false</GenerateAsynchronousMethods>
    <EnableDataBinding>true</EnableDataBinding>
    <ExcludedTypes />
    <ImportXmlTypes>false</ImportXmlTypes>
    <GenerateInternalTypes>false</GenerateInternalTypes>
    <GenerateMessageContracts>false</GenerateMessageContracts>
    <NamespaceMappings />
    <CollectionMappings />
    <GenerateSerializableTypes>true</GenerateSerializableTypes>
    <Serializer>Auto</Serializer>
    <ReferenceAllAssemblies>true</ReferenceAllAssemblies>
    <ReferencedAssemblies />
    <ReferencedDataContractTypes />
    <ServiceContractMappings />
  </ClientOptions>
like image 713
Marat Faskhiev Avatar asked Mar 04 '10 05:03

Marat Faskhiev


1 Answers

If you view all files for the project and then view the file called Reference.svcmap for the appropriate service reference could you please let me know what the following config options are in the xml?

<ExcludedTypes />
<ImportXmlTypes>false</ImportXmlTypes>
<GenerateInternalTypes>false</GenerateInternalTypes>
<GenerateSerializableTypes>false</GenerateSerializableTypes>
<Serializer>Auto</Serializer>

Sorry about putting it in as an answer but it was horribly unreadable in the comments.

Edit

Ok, so what is happening here is following:

  1. You are using auto for the serializer.
  2. The default is DataContractSerializer
  3. When generating the proxy code, there is a check for forbidden xsd elements.
  4. If forbidden elements are found, the XmlSerializer is used.

In your case, adding the xsd:any element is causing the serialization mode to change. If you want consistent serialization, you will have to remove the forbidden element or force the proxy generation to use XmlSerialization all the time.

Here is a link about the allowable schema elements for the DataContractSerializer.

Cheers -Leigh

like image 116
Leigh S Avatar answered Oct 28 '22 17:10

Leigh S