因此,我正在研究如何格式化从WCF生成的WSDL和XSD,特别是向WSDL和XSD添加注释/文档以及为各种参数添加限制。为wcf生成的xsd添加限制
到目前为止,我已经能够通过创建实现IWSDLExportExtension接口以及IOperationBehavior接口的属性,将文档添加到WSDL和XSD中。
用于修改架构的总体思路看:
http://thorarin.net/blog/post/2010/08/08/Controlling-WSDL-minOccurs-with-WCF.aspx
对于添加注解WSDL见的总体思路:
http://msdn.microsoft.com/en-us/library/ms731731(v=vs.110).aspx
但是我遇到了麻烦试图限制添加到时元素(通过添加简单类型)在xsd中。
从这里我可以得到一个异常,说明我不能设置元素类型,因为它已经有一个只读类型与它关联,或者我可以尝试使用只读类型添加限制,但没有任何反应。
下面是产生异常 (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
}
...
}
而这里的工作都是围绕一个什么也不做:
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;
}
...
}
快速其他说明: 如果我切换到正常循环和实际尝试用新元素替换麻烦元素(哈克我知道...),我得到以下异常:System.InvalidCastException: Unable to cast object of type 'System.Xml.Schema.XmlSchemaSimpleType' to type 'System.Xml.Schema.XmlSchemaParticle'.
This一个我更好奇,因为他们都应该是XmlSchemaElements的权利?
所以基本上,没有人知道如何添加简单类型/添加简单类型限制到xmlschemaelements,并让它显示在使用WCF的WSDL生成的XSD中?
谢谢!
编辑: 添加 tempRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
感谢您的回应,但简单的类型和限制标签仍然没有出现在元素标签下。 –