QualifiedType accessor to XML Schema type mapping
This previous post shows how WCF LOB Adapter SDK metadata object model can be used to define metadata for operations and/or types so that the WCF contract (WSDL) can be generated from it. The ResolveOperationMetadata() / ResolveTypeMetadata() methods in Microsoft.ServiceModel.Channels.Common.IMetdataResolverHandler implementation class are used to return the corresponding Microsoft.ServiceModel.Channels.Common.OperationMetadata / Microsoft.ServiceModel.Channels.Common.TypeMetadata, which in turn have knowledge generating equivalent XML Schema representation. This XML Schema is an integral part of the WSDL for defining the operation data structure.
The data types for an operation parameter, operation result and/or a complex object can be defined using the following two derived classes of the abstract class called Microsoft.ServiceModel.Channels.Common.QualifiedType.
- Microsoft.ServiceModel.Channels.Common.SimpleQualifiedType
- Microsoft.ServiceModel.Channels.Common.ComplexQualifiedType
The class QualifiedType defines static methods to get the instance for most commonly used types.
Let’s assume defining a simple operation named “Add” that takes in two parameters of type “double” and return a result of type “double”. The second double parameter has an extra restriction. The following code snippet shows using QualifiedType.DoubleType for parameter “n1” and result, but “n2” has an extra restriction to have a particular minimum length. The facets from System.Xml.Schema are used to define restrictions on various data types.
public class SampleAdapterMetadataResolverHandler : IMetadataResolverHandler
{
. . .
public OperationMetadata ResolveOperationMetadata(string operationId, TimeSpan timeout, out TypeMetadataCollection extraTypeMetadataResolved)
{
extraTypeMetadataResolved = null;
// for illustrative purposes show how to resolve one operation here
ParameterizedOperationMetadata opMetadata = new ParameterizedOperationMetadata(operationId, operationId);
opMetadata.OperationNamespace = SampleAdapter.SERVICENAMESPACE;
opMetadata.OperationGroup = "SampleContract";
// Syntax: double Add(double n1, double n2)
opMetadata.DisplayName = "Add";
// Create Parameters
OperationParameter n1 = new OperationParameter("n1", OperationParameterDirection.In, QualifiedType.DoubleType, false);
n1.Description = "First number to add.";
// SimpleQualifiedType doubleSQT = new SimpleQualifiedType(System.Xml.Schema.XmlTypeCode.Double);
SimpleQualifiedType doubleSQT = QualifiedType.DoubleType as SimpleQualifiedType;
XmlSchemaMinLengthFacet minLengthFacet = new XmlSchemaMinLengthFacet();
minLengthFacet.Value = "2";
doubleSQT.XmlSchemaFacets.Add(minLengthFacet);
OperationParameter n2 = new OperationParameter("n2", OperationParameterDirection.In, doubleSQT, false);
n2.Description = "Second number to add.";
opMetadata.Parameters.Add(n1);
opMetadata.Parameters.Add(n2);
// Create Result
opMetadata.OperationResult = new OperationResult(QualifiedType.DoubleType, false);
return opMetadata;
}
}
Use SimpleQualifiedType if you want to add additional restrictions on the type, as shown below, else just use the static member in QualifiedType to get the required type instance.
The following table shows the type mapping between the QualifiedType static type, XML Schema Type and generated Common Language Runtime (CLR) C# equivalent.
QualifiedType Member |
XML Schema Type |
C# |
Target XML Schema Namespace |
AnyType |
xsd:anyType |
Object |
https://www.w3.org/2001/XMLSchema |
Base64BinaryType |
xsd: base64Binary |
byte[] |
https://www.w3.org/2001/XMLSchema |
BooleanType |
xsd: boolean |
Bool |
https://www.w3.org/2001/XMLSchema |
ByteType |
xsd:byte |
Sbyte |
https://www.w3.org/2001/XMLSchema |
CharType |
char |
char |
https://schemas.microsoft.com/2003/10/Serialization/ |
DateTimeType |
xsd:dateTime |
System.DateTime |
https://www.w3.org/2001/XMLSchema |
DecimalType |
xsd:decimal |
decimal |
https://www.w3.org/2001/XMLSchema |
DoubleType |
xsd:double |
double |
https://www.w3.org/2001/XMLSchema |
DurationType |
duration |
System.TimeSpan |
https://schemas.microsoft.com/2003/10/Serialization/ |
FloatType |
xsd:float |
float |
https://www.w3.org/2001/XMLSchema |
GuidType |
guid |
System.Guid |
https://schemas.microsoft.com/2003/10/Serialization/ |
IntType |
xsd:int |
int |
https://www.w3.org/2001/XMLSchema |
LongType |
xsd:long |
long |
https://www.w3.org/2001/XMLSchema |
QNameType |
xsd:QName |
System.Xml.XmlQualifiedName |
https://www.w3.org/2001/XMLSchema |
ShortType |
xsd:short |
short |
https://www.w3.org/2001/XMLSchema |
StreamType |
StreamBody |
System.IO.Stream (if used within OperationParameter and/or OperationResult) byte[] if used within TypeMember. |
https://schemas.microsoft.com/Message |
StringType |
xsd:string |
string |
https://www.w3.org/2001/XMLSchema |
UnsignedByteType |
xsd:unsignedByte |
byte |
https://www.w3.org/2001/XMLSchema |
UnsignedIntType |
xsd:unsignedInt |
uint |
https://www.w3.org/2001/XMLSchema |
UnsignedLongType |
xsd:unsignedLong |
ulong |
https://www.w3.org/2001/XMLSchema |
UnsignedShortType |
xsd:unsignedShort |
ushort |
https://www.w3.org/2001/XMLSchema |
UriType |
xsd:anyUri |
System.Uri |
https://www.w3.org/2001/XMLSchema |