Using Xsd as the type definition language in WCF in Visual Studio 2012, is there anyway to encode "known types" in the Xsd file?



We use Xml Schema files to define our Data Contracts in WCF4.


Such that the following schema:



<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://ift.tt/tphNwY" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="IncomingDocument">
<xs:complexType>
<xs:sequence>
<xs:element name="SomeMessage" type="xs:string" minOccurs="0"/>
<xs:element name="Payload" type="xs:anyType" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="SomeType">
<xs:sequence>
<xs:element name="Something" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="AnotherOne">
<xs:sequence>
<xs:element name="ANumber" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:schema>


Creates the following Data Contract (most of the content on the classes have been cut for brevity):



[DebuggerStepThrough]
[GeneratedCode("System.Runtime.Serialization", "4.0.0.0")]
[DataContract]
[Serializable]
internal partial class SomeType
{
[DataMember]
internal string Something;
}

[DebuggerStepThrough]
[GeneratedCode("System.Runtime.Serialization", "4.0.0.0")]
[DataContract]
[Serializable]
internal partial class AnotherOne
{
[DataMember]
internal int ANumber;
}

[DebuggerStepThrough]
[GeneratedCode("System.Runtime.Serialization", "4.0.0.0")]
[DataContract]
[Serializable]
[KnownType(typeof(SomeType))]
[KnownType(typeof(AnotherOne))]
internal partial class IncomingDocument
{
[DataMember]
internal string SomeMessage;
[DataMember]
internal object Payload;
}


The problem comes when I try to use this in a WCF service and we put the (required) arbitrary types inside the Payload.


For instance, we receive an Xml document from a 3rd party and we need to forward it onto a different web service where it's stored in a database.


At the moment, the only KnownTypes are the ones the Xsd reader found inside the Xsd file itself.


If we put an XmlElement in Payload, you get the error about adding it to the list of KnownTypes.


Specifically, we absolutely need XmlElement to be on the KnownType list.


I know you can add DataContract specific stuff in your Xsd using things like:



<xs:annotation>
<xs:appinfo>
<msxd:DefaultValue EmitDefaultValue="false"/>
</xs:appinfo>
</xs:annotation>


But I can't find a reference guide to these annotations, I'm hoping KnownType exists.


Is there any way to add an attribute to that without it being blitzed every time it's auto-generated? Or, how to I define KnownTypeAttrivutes using Xsd as the type definition language for WCF?


No comments:

Post a Comment