因此,我正在研究如何格式化從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");
感謝您的迴應,但簡單的類型和限制標籤仍然沒有出現在元素標籤下。 –