1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //------------------------------------------------------------
5 namespace System.ServiceModel.Description
8 using System.Collections.Generic;
9 using System.Collections.ObjectModel;
12 using WsdlNS = System.Web.Services.Description;
13 using XsdNS = System.Xml.Schema;
14 using System.Reflection;
15 using System.Xml.Serialization;
17 [XmlRoot(ElementName = MetadataStrings.MetadataExchangeStrings.MetadataSection, Namespace = MetadataStrings.MetadataExchangeStrings.Namespace)]
18 public class MetadataSection
20 Collection<XmlAttribute> attributes = new Collection<XmlAttribute>();
25 static XmlDocument xmlDocument = new XmlDocument();
27 public MetadataSection()
28 : this(null, null, null)
32 public MetadataSection(string dialect, string identifier, object metadata)
34 this.dialect = dialect;
35 this.identifier = identifier;
36 this.metadata = metadata;
39 static public string ServiceDescriptionDialect { get { return System.Web.Services.Description.ServiceDescription.Namespace; } }
40 static public string XmlSchemaDialect { get { return System.Xml.Schema.XmlSchema.Namespace; } }
41 static public string PolicyDialect { get { return MetadataStrings.WSPolicy.NamespaceUri; } }
42 static public string MetadataExchangeDialect { get { return MetadataStrings.MetadataExchangeStrings.Namespace; } }
45 public Collection<XmlAttribute> Attributes
47 get { return attributes; }
53 get { return this.dialect; }
54 set { this.dialect = value; }
58 public string Identifier
60 get { return this.identifier; }
61 set { this.identifier = value; }
65 [XmlElement(MetadataStrings.XmlSchema.Schema, typeof(XsdNS.XmlSchema), Namespace = XsdNS.XmlSchema.Namespace)]
66 //typeof(WsdlNS.ServiceDescription) produces an XmlSerializer which can't export / import the Extensions in the ServiceDescription.
67 //We use change this to typeof(string) and then fix the generated serializer to use the Read/Write
68 //methods provided by WsdlNS.ServiceDesciption which use a pregenerated serializer which can export / import the Extensions.
69 [XmlElement(MetadataStrings.ServiceDescription.Definitions, typeof(WsdlNS.ServiceDescription), Namespace = WsdlNS.ServiceDescription.Namespace)]
70 [XmlElement(MetadataStrings.MetadataExchangeStrings.MetadataReference, typeof(MetadataReference), Namespace = MetadataStrings.MetadataExchangeStrings.Namespace)]
71 [XmlElement(MetadataStrings.MetadataExchangeStrings.Location, typeof(MetadataLocation), Namespace = MetadataStrings.MetadataExchangeStrings.Namespace)]
72 [XmlElement(MetadataStrings.MetadataExchangeStrings.Metadata, typeof(MetadataSet), Namespace = MetadataStrings.MetadataExchangeStrings.Namespace)]
73 public object Metadata
75 get { return this.metadata; }
76 set { this.metadata = value; }
79 internal string SourceUrl
81 get { return sourceUrl; }
82 set { sourceUrl = value; }
85 public static MetadataSection CreateFromPolicy(XmlElement policy, string identifier)
89 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("policy");
92 if (!IsPolicyElement(policy))
94 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("policy",
95 #pragma warning suppress 56506 // [....], policy cannot be null at this point since it has been validated above.
96 SR.GetString(SR.SFxBadMetadataMustBePolicy, MetadataStrings.WSPolicy.NamespaceUri, MetadataStrings.WSPolicy.Elements.Policy, policy.NamespaceURI, policy.LocalName));
99 MetadataSection section = new MetadataSection();
101 section.Dialect = policy.NamespaceURI;
102 section.Identifier = identifier;
103 section.Metadata = policy;
107 public static MetadataSection CreateFromSchema(XsdNS.XmlSchema schema)
111 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("schema");
114 MetadataSection section = new MetadataSection();
116 section.Dialect = MetadataSection.XmlSchemaDialect;
117 section.Identifier = schema.TargetNamespace;
118 section.Metadata = schema;
122 public static MetadataSection CreateFromServiceDescription(WsdlNS.ServiceDescription serviceDescription)
124 if (serviceDescription == null)
126 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceDescription");
129 MetadataSection section = new MetadataSection();
131 section.Dialect = MetadataSection.ServiceDescriptionDialect;
132 section.Identifier = serviceDescription.TargetNamespace;
133 section.Metadata = serviceDescription;
138 internal static bool IsPolicyElement(XmlElement policy)
140 return (policy.NamespaceURI == MetadataStrings.WSPolicy.NamespaceUri
141 || policy.NamespaceURI == MetadataStrings.WSPolicy.NamespaceUri15)
142 && policy.LocalName == MetadataStrings.WSPolicy.Elements.Policy;