Question

So I'm doing some research on how to format the WSDL and XSD generated from WCF, specifically adding annotations/documentation to the WSDL and XSDs along with adding restrictions to various parameters.

So far I've been able to add the documentation to both the WSDL and XSDs by creating attributes that implement the IWSDLExportExtension interface along with the IOperationBehavior Interface.

For the general idea of modifying the schemas see:
http://thorarin.net/blog/post/2010/08/08/Controlling-WSDL-minOccurs-with-WCF.aspx

For the general idea of adding annotations to WSDL see:
http://msdn.microsoft.com/en-us/library/ms731731(v=vs.110).aspx

However I run into trouble when trying to add restrictions to elements(by adding simple types) in the xsd.

From here I can either get an exception to occur stating that I can't set the elements type because it already has a read-only type associated with it, or I can try using the read-only type to add the restriction, but then nothing happens.

Here's the code for generating the exception (System.Xml.Schema.XmlSchemaException: The type attribute cannot be present with either simpleType or complexType.) :

    var ComplexType = ((XmlSchemaElement)Schema.Elements[Parameter.XmlQualifiedName]).ElementSchemaType as XmlSchemaComplexType;
    var Sequence = ComplexType.Particle as XmlSchemaSequence;

    foreach (XmlSchemaElement Item in Sequence.Items)
    {
        if (Item.Name = Parameter.Name && Parameter.Length > 0)
        {
            XmlSchemaSimpleType tempType = new XmlSchemaSimpleType();
            XmlSchemaSimpleTypeRestriction tempRestriction = new XmlSchemaSimpleTypeRestriction();
            XmlSchemaLengthFacet lengthFacet = new XmlSchemaLengthFacet();
            tempType.Content = tempRestriction;
            tempRestriction.Facets.Add(lengthFacet);
            lengthFacet.Value = "" + Parameter.Length;

            Item.SchemaType = tempType; // <-- Problem code

         }
         ...
     }

And here's the work around that does nothing:

    var ComplexType = ((XmlSchemaElement)Schema.Elements[Parameter.XmlQualifiedName]).ElementSchemaType as XmlSchemaComplexType;
    var Sequence = ComplexType.Particle as XmlSchemaSequence;

    foreach (XmlSchemaElement Item in Sequence.Items)
    {
        if (Item.Name = Parameter.Name && Parameter.Length > 0)
        {
            XmlSchemaSimpleTypeRestriction tempRestriction = new XmlSchemaSimpleTypeRestriction();
            XmlSchemaLengthFacet lengthFacet = new XmlSchemaLengthFacet();
            tempRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
            tempRestriction.Facets.Add(lengthFacet);
            lengthFacet.Value = "" + Parameter.Length;

            // Appears to do nothing
            ((XmlSchemaSimpleType)Item.ElementSchemaType).Content = tempRestriction;

         }
         ...
     }

Quick other note: If I switch to a normal for loop and actually try to replace the trouble element with a new element (hackish I know...), I get the following exception: System.InvalidCastException: Unable to cast object of type 'System.Xml.Schema.XmlSchemaSimpleType' to type 'System.Xml.Schema.XmlSchemaParticle'. This one I'm more just curious about since they both should be XmlSchemaElements right?

So basically, does anyone know how to add simple types/add simple type restrictions to xmlschemaelements and have it show up in the XSDs that are generated with the WSDL from WCF?

Thanks!


EDIT: Added
tempRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");

Was it helpful?

Solution

In the second example, don't forget

tempRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");

before defining the facets. Restriction is not a separate schema object, but a derivation method. It derives your own simple type from a pre-existing simple type such as string.

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