Question

I have the following Request and Response Data Contract pair for a Webservice method call

  <xs:element name="GetUserOptionsRequest">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="OptionType" type="entities:UserOption" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="GetUserOptionsResponse">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Options" type="entities:UserOption" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

The problem is that what I want to have is a way of saying (pseudocode)

GetUserResponse response = GetuserOptions(new GetUserOptionsRequest(Type T))

And to have the response contain an IList depending on what type I pass through.

With my above data contract XSD, it is expecting an instance of a class in the request object, when I just want to specify a Type definition.

Really I suppose what I would want it

GetUserResponse<T> response = GetUserOptions(new GetUserOptionsRequest<T>());

But Im not sure how to specify generic classes/methods in XSD. Can anyone point me at a good article or explain how I can define this in XSD?

I am using WSCF blue for code generation too.

Was it helpful?

Solution

Unfortunately, it's not possible to define types with open generics in XSD.

Actually, XSD doesn't have a native way of representing generic type arguments at all. However, if you are going to generate .NET types out of the XSD schema, you can mimic closed generic arguments by decorating a type with an <xsd:annotation> containing an <xsd:appinfo> element:

<xs:element name="Foo">
    <xs:complexType>
        <xs:sequence>
            ...
        </xs:sequence>
        <xs:annotation>
            <xs:appinfo>
                <GenericType Name="FooOf{0}{#}"
                             Namespace="...">
                    <GenericParameter Name="string"
                                      Namespace="http://www.w3.org/2001/XMLSchema" />
                </GenericType>
            </xs:appinfo>
        </xs:annotation>
    </xs:complexType> 
</xs:element>

This XSD schema will be represented in the .NET type system as a class of type Foo<string>. Note that in this example the generic argument is specified as string, but nothing prevents you from reusing the same XSD element definition in multiple contracts with different generic arguments.

Also, note that the GenericType element is not part of the XSD standard and will only be interpreted by a .NET code generation tool.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top