Welcome to MSDN Blogs Sign in | Join | Help

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

http://www.w3.org/2001/XMLSchema

Base64BinaryType

xsd: base64Binary

byte[]

http://www.w3.org/2001/XMLSchema

BooleanType

xsd: boolean

Bool

http://www.w3.org/2001/XMLSchema

ByteType

xsd:byte

Sbyte

http://www.w3.org/2001/XMLSchema

CharType

char

char

http://schemas.microsoft.com/2003/10/Serialization/

DateTimeType

xsd:dateTime

System.DateTime

http://www.w3.org/2001/XMLSchema

DecimalType

xsd:decimal

decimal

http://www.w3.org/2001/XMLSchema

DoubleType

xsd:double

double

http://www.w3.org/2001/XMLSchema

DurationType

duration

System.TimeSpan

http://schemas.microsoft.com/2003/10/Serialization/

FloatType

xsd:float

float

http://www.w3.org/2001/XMLSchema

GuidType

guid

System.Guid

http://schemas.microsoft.com/2003/10/Serialization/

IntType

xsd:int

int

http://www.w3.org/2001/XMLSchema

LongType

xsd:long

long

http://www.w3.org/2001/XMLSchema

QNameType

xsd:QName

System.Xml.XmlQualifiedName

http://www.w3.org/2001/XMLSchema

ShortType

xsd:short

short

http://www.w3.org/2001/XMLSchema

StreamType

StreamBody

System.IO.Stream (if used within OperationParameter and/or OperationResult)

byte[] if used within TypeMember.

http://schemas.microsoft.com/Message

StringType

xsd:string

string

http://www.w3.org/2001/XMLSchema

UnsignedByteType

xsd:unsignedByte

byte

http://www.w3.org/2001/XMLSchema

UnsignedIntType

xsd:unsignedInt

uint

http://www.w3.org/2001/XMLSchema

UnsignedLongType

xsd:unsignedLong

ulong

http://www.w3.org/2001/XMLSchema

UnsignedShortType

xsd:unsignedShort

ushort

http://www.w3.org/2001/XMLSchema

UriType

xsd:anyUri

System.Uri

http://www.w3.org/2001/XMLSchema

 

Published Monday, July 30, 2007 3:10 PM by sonua

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker