MetadataSection.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Description
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Text;
  10. using System.Xml;
  11. using WsdlNS = System.Web.Services.Description;
  12. using XsdNS = System.Xml.Schema;
  13. using System.Reflection;
  14. using System.Xml.Serialization;
  15. [XmlRoot(ElementName = MetadataStrings.MetadataExchangeStrings.MetadataSection, Namespace = MetadataStrings.MetadataExchangeStrings.Namespace)]
  16. public class MetadataSection
  17. {
  18. Collection<XmlAttribute> attributes = new Collection<XmlAttribute>();
  19. string dialect;
  20. string identifier;
  21. object metadata;
  22. string sourceUrl;
  23. static XmlDocument xmlDocument = new XmlDocument();
  24. public MetadataSection()
  25. : this(null, null, null)
  26. {
  27. }
  28. public MetadataSection(string dialect, string identifier, object metadata)
  29. {
  30. this.dialect = dialect;
  31. this.identifier = identifier;
  32. this.metadata = metadata;
  33. }
  34. static public string ServiceDescriptionDialect { get { return System.Web.Services.Description.ServiceDescription.Namespace; } }
  35. static public string XmlSchemaDialect { get { return System.Xml.Schema.XmlSchema.Namespace; } }
  36. static public string PolicyDialect { get { return MetadataStrings.WSPolicy.NamespaceUri; } }
  37. static public string MetadataExchangeDialect { get { return MetadataStrings.MetadataExchangeStrings.Namespace; } }
  38. [XmlAnyAttribute]
  39. public Collection<XmlAttribute> Attributes
  40. {
  41. get { return attributes; }
  42. }
  43. [XmlAttribute]
  44. public string Dialect
  45. {
  46. get { return this.dialect; }
  47. set { this.dialect = value; }
  48. }
  49. [XmlAttribute]
  50. public string Identifier
  51. {
  52. get { return this.identifier; }
  53. set { this.identifier = value; }
  54. }
  55. [XmlAnyElement]
  56. [XmlElement(MetadataStrings.XmlSchema.Schema, typeof(XsdNS.XmlSchema), Namespace = XsdNS.XmlSchema.Namespace)]
  57. //typeof(WsdlNS.ServiceDescription) produces an XmlSerializer which can't export / import the Extensions in the ServiceDescription.
  58. //We use change this to typeof(string) and then fix the generated serializer to use the Read/Write
  59. //methods provided by WsdlNS.ServiceDesciption which use a pregenerated serializer which can export / import the Extensions.
  60. [XmlElement(MetadataStrings.ServiceDescription.Definitions, typeof(WsdlNS.ServiceDescription), Namespace = WsdlNS.ServiceDescription.Namespace)]
  61. [XmlElement(MetadataStrings.MetadataExchangeStrings.MetadataReference, typeof(MetadataReference), Namespace = MetadataStrings.MetadataExchangeStrings.Namespace)]
  62. [XmlElement(MetadataStrings.MetadataExchangeStrings.Location, typeof(MetadataLocation), Namespace = MetadataStrings.MetadataExchangeStrings.Namespace)]
  63. [XmlElement(MetadataStrings.MetadataExchangeStrings.Metadata, typeof(MetadataSet), Namespace = MetadataStrings.MetadataExchangeStrings.Namespace)]
  64. public object Metadata
  65. {
  66. get { return this.metadata; }
  67. set { this.metadata = value; }
  68. }
  69. internal string SourceUrl
  70. {
  71. get { return sourceUrl; }
  72. set { sourceUrl = value; }
  73. }
  74. public static MetadataSection CreateFromPolicy(XmlElement policy, string identifier)
  75. {
  76. if (policy == null)
  77. {
  78. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("policy");
  79. }
  80. if (!IsPolicyElement(policy))
  81. {
  82. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("policy",
  83. #pragma warning suppress 56506 // [....], policy cannot be null at this point since it has been validated above.
  84. SR.GetString(SR.SFxBadMetadataMustBePolicy, MetadataStrings.WSPolicy.NamespaceUri, MetadataStrings.WSPolicy.Elements.Policy, policy.NamespaceURI, policy.LocalName));
  85. }
  86. MetadataSection section = new MetadataSection();
  87. section.Dialect = policy.NamespaceURI;
  88. section.Identifier = identifier;
  89. section.Metadata = policy;
  90. return section;
  91. }
  92. public static MetadataSection CreateFromSchema(XsdNS.XmlSchema schema)
  93. {
  94. if (schema == null)
  95. {
  96. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("schema");
  97. }
  98. MetadataSection section = new MetadataSection();
  99. section.Dialect = MetadataSection.XmlSchemaDialect;
  100. section.Identifier = schema.TargetNamespace;
  101. section.Metadata = schema;
  102. return section;
  103. }
  104. public static MetadataSection CreateFromServiceDescription(WsdlNS.ServiceDescription serviceDescription)
  105. {
  106. if (serviceDescription == null)
  107. {
  108. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceDescription");
  109. }
  110. MetadataSection section = new MetadataSection();
  111. section.Dialect = MetadataSection.ServiceDescriptionDialect;
  112. section.Identifier = serviceDescription.TargetNamespace;
  113. section.Metadata = serviceDescription;
  114. return section;
  115. }
  116. internal static bool IsPolicyElement(XmlElement policy)
  117. {
  118. return (policy.NamespaceURI == MetadataStrings.WSPolicy.NamespaceUri
  119. || policy.NamespaceURI == MetadataStrings.WSPolicy.NamespaceUri15)
  120. && policy.LocalName == MetadataStrings.WSPolicy.Elements.Policy;
  121. }
  122. }
  123. }