Howto use svcutil to generate a C# WCF proxy from a web service that uses restriction to hide elements?

Trond Aarskog picture Trond Aarskog · Oct 22, 2011 · Viewed 7.9k times · Source

I'm creating a client to a web service that is more or less out of my control. Here is a simplified sample of the schema:

<xs:complexType name="A">
    <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="1" name="element1" type="xs:string" />
        <xs:element minOccurs="0" maxOccurs="1" name="element2" type="xs:string" />
    </xs:sequence>
</xs:complexType>

<xs:complexType name="B">
    <xs:complexContent>
        <xs:restriction base="A">
            <xs:sequence>
                <xs:element minOccurs="1" maxOccurs="1" name="element2" type="xs:string" />
            </xs:sequence>
        </xs:restriction>
    </xs:complexContent>
</xs:complexType>

In short we have an object A that contains all elements. The service has several types based on A but with restrictions so that the inherited types are usually smaller than the base type - here exemplified by the type B.

In schema viewers like the one in Visual Studio 2010, SoapUI, etc. this looks as expected. A has 2 elements and B only 1 (= element 2).

By using svcutil I get the full set of elements in both my types A & B, or when playing with the options I get error messages such as:

Error: Type 'B' in namespace 'http://tempuri.org/XMLSchema.xsd' cannot be imported. Complex types derived by restriction not supported. Either change the schema so that the types can map to data contract types or use ImportXmlType or use a different serializer.

Hiding fields/properties in inherited types is not a practice/road I like to travel but if I cannot get the provider to change the WSDL it seems I have to do it that way.

Is there alternatives to svcutil that handle this properly or do I have to hand-code my proxies?


Update 1

As pointed out by John Saunders I have not shown the results of the suggestions from svcutil. That was partly to keep the post short... but here goes:

svcutil schema.xsd /importXmlTypes /datacontractonly results in:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="A", Namespace="http://tempuri.org/XMLSchema.xsd")]
public partial class A : object, System.Runtime.Serialization.IExtensibleDataObject
{

    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

    private string element1Field;

    private string element2Field;

    public System.Runtime.Serialization.ExtensionDataObject ExtensionData
    {
        get
        {
            return this.extensionDataField;
        }
        set
        {
            this.extensionDataField = value;
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false)]
    public string element1
    {
        get
        {
            return this.element1Field;
        }
        set
        {
            this.element1Field = value;
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false)]
    public string element2
    {
        get
        {
            return this.element2Field;
        }
        set
        {
            this.element2Field = value;
        }
    }
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Xml.Serialization.XmlSchemaProviderAttribute("ExportSchema")]
[System.Xml.Serialization.XmlRootAttribute(IsNullable=false)]

public partial class B : object, System.Xml.Serialization.IXmlSerializable
{

    private System.Xml.XmlNode[] nodesField;

    private static System.Xml.XmlQualifiedName typeName = new System.Xml.XmlQualifiedName("B", "http://tempuri.org/XMLSchema.xsd");

    public System.Xml.XmlNode[] Nodes
    {
        get
        {
            return this.nodesField;
        }
        set
        {
            this.nodesField = value;
        }
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        this.nodesField = System.Runtime.Serialization.XmlSerializableServices.ReadNodes(reader);
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        System.Runtime.Serialization.XmlSerializableServices.WriteNodes(writer, this.Nodes);
    }

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public static System.Xml.XmlQualifiedName ExportSchema(System.Xml.Schema.XmlSchemaSet schemas)
    {
        System.Runtime.Serialization.XmlSerializableServices.AddDefaultSchema(schemas, typeName);
        return typeName;
    }
}

Working on an Xml-level is not desirable and would force us to write a wrapper. It's easier to handcode the proxy from the getgo.

svcutil schema.xsd /serializer:XmlSerializer /datacontractonly Gives the error below and is the reason I'm asking for alternative tools.

svcutil schema.xsd /serializer:XmlSerializer /datacontractonly Error: Type 'B' in namespace 'http://tempuri.org/XMLSchema.xsd' cannot be imported. Complex types derived by restriction not supported. Either change the schema so that the types can map to data contract types or use ImportXmlType or use a different serializer.

If you are using the /dataContractOnly option to import data contract types and are getting this error message, consider using xsd.exe instead. Types generated by xsd.exe may be used in the Windows Communication Foundation after applying the XmlSerializerFormatAttribute attribute on your service contract. Alternatively, consider using the /importXmlTypes option to import these types as XML types to use with DataContractFormatAttribute attribute on your service contract.

Using xsd schema.xsd /c gives a type B that inherits A without hiding element1:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/XMLSchema.xsd")]
[System.Xml.Serialization.XmlRootAttribute("request", Namespace="http://tempuri.org/XMLSchema.xsd", IsNullable=false)]
public partial class B : A {
}

/// <remarks/>
[System.Xml.Serialization.XmlIncludeAttribute(typeof(B))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/XMLSchema.xsd")]
public partial class A {

    private string element1Field;

    private string element2Field;

    /// <remarks/>
    public string element1 {
        get {
            return this.element1Field;
        }
        set {
            this.element1Field = value;
        }
    }

    /// <remarks/>
    public string element2 {
        get {
            return this.element2Field;
        }
        set {
            this.element2Field = value;
        }
    }
}

Answer

John Saunders picture John Saunders · Oct 22, 2011

The error message is telling you to either use the /importXmlTypes switch, or to change to using the XmlSerializer. From the help:

/importXmlTypes - Configure the Data Contract serializer to import non-Data Contract types as IXmlSerializable types.

and

/serializer:XmlSerializer - Generate data types that use the XmlSerializer for serialization and deserialization