| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Collections.ObjectModel; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Xml; |
| | | 9 | | using WsdlNS = System.Web.Services.Description; |
| | | 10 | | using XsdNS = System.Xml.Schema; |
| | | 11 | | using System.Reflection; |
| | | 12 | | using System.Xml.Serialization; |
| | | 13 | | |
| | | 14 | | namespace CoreWCF.Description |
| | | 15 | | { |
| | | 16 | | [XmlRoot(ElementName = MetadataStrings.MetadataExchangeStrings.MetadataSection, Namespace = MetadataStrings.Metadata |
| | | 17 | | public class MetadataSection |
| | | 18 | | { |
| | 95 | 19 | | private readonly Collection<XmlAttribute> _attributes = new Collection<XmlAttribute>(); |
| | | 20 | | |
| | 190 | 21 | | public MetadataSection() : this(null, null, null) { } |
| | | 22 | | |
| | 95 | 23 | | public MetadataSection(string dialect, string identifier, object metadata) |
| | | 24 | | { |
| | 95 | 25 | | Dialect = dialect; |
| | 95 | 26 | | Identifier = identifier; |
| | 95 | 27 | | Metadata = metadata; |
| | 95 | 28 | | } |
| | | 29 | | |
| | 24 | 30 | | public static string ServiceDescriptionDialect { get { return WsdlNS.ServiceDescription.Namespace; } } |
| | 71 | 31 | | public static string XmlSchemaDialect { get { return XsdNS.XmlSchema.Namespace; } } |
| | 0 | 32 | | public static string PolicyDialect { get { return MetadataStrings.WSPolicy.NamespaceUri; } } |
| | 0 | 33 | | public static string MetadataExchangeDialect { get { return MetadataStrings.MetadataExchangeStrings.Namespace; } |
| | | 34 | | |
| | | 35 | | [XmlAnyAttribute] |
| | 0 | 36 | | public Collection<XmlAttribute> Attributes => _attributes; |
| | | 37 | | |
| | | 38 | | [XmlAttribute] |
| | 190 | 39 | | public string Dialect { get; set; } |
| | | 40 | | |
| | | 41 | | [XmlAttribute] |
| | 190 | 42 | | public string Identifier { get; set; } |
| | | 43 | | |
| | | 44 | | [XmlAnyElement] |
| | | 45 | | [XmlElement(MetadataStrings.XmlSchema.Schema, typeof(XsdNS.XmlSchema), Namespace = XsdNS.XmlSchema.Namespace)] |
| | | 46 | | //typeof(WsdlNS.ServiceDescription) produces an XmlSerializer which can't export / import the Extensions in the |
| | | 47 | | //We use change this to typeof(string) and then fix the generated serializer to use the Read/Write |
| | | 48 | | //methods provided by WsdlNS.ServiceDesciption which use a pregenerated serializer which can export / import the |
| | | 49 | | [XmlElement(MetadataStrings.ServiceDescription.Definitions, typeof(WsdlNS.ServiceDescription), Namespace = WsdlN |
| | | 50 | | [XmlElement(MetadataStrings.MetadataExchangeStrings.MetadataReference, typeof(MetadataReference), Namespace = Me |
| | | 51 | | [XmlElement(MetadataStrings.MetadataExchangeStrings.Location, typeof(MetadataLocation), Namespace = MetadataStri |
| | | 52 | | [XmlElement(MetadataStrings.MetadataExchangeStrings.Metadata, typeof(MetadataSet), Namespace = MetadataStrings.M |
| | 613 | 53 | | public object Metadata { get; set; } |
| | | 54 | | |
| | 0 | 55 | | internal string SourceUrl { get; set; } |
| | | 56 | | |
| | | 57 | | public static MetadataSection CreateFromPolicy(XmlElement policy, string identifier) |
| | | 58 | | { |
| | 0 | 59 | | if (policy == null) |
| | | 60 | | { |
| | 0 | 61 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(policy)); |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | if (!IsPolicyElement(policy)) |
| | | 65 | | { |
| | 0 | 66 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(policy), |
| | 0 | 67 | | SR.Format(SR.SFxBadMetadataMustBePolicy, MetadataStrings.WSPolicy.NamespaceUri, MetadataStrings.WSPo |
| | | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | MetadataSection section = new MetadataSection(); |
| | | 71 | | |
| | 0 | 72 | | section.Dialect = policy.NamespaceURI; |
| | 0 | 73 | | section.Identifier = identifier; |
| | 0 | 74 | | section.Metadata = policy; |
| | | 75 | | |
| | 0 | 76 | | return section; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public static MetadataSection CreateFromSchema(XsdNS.XmlSchema schema) |
| | | 80 | | { |
| | 71 | 81 | | if (schema == null) |
| | | 82 | | { |
| | 0 | 83 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(schema)); |
| | | 84 | | } |
| | | 85 | | |
| | 71 | 86 | | MetadataSection section = new MetadataSection(); |
| | | 87 | | |
| | 71 | 88 | | section.Dialect = XmlSchemaDialect; |
| | 71 | 89 | | section.Identifier = schema.TargetNamespace; |
| | 71 | 90 | | section.Metadata = schema; |
| | | 91 | | |
| | 71 | 92 | | return section; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | public static MetadataSection CreateFromServiceDescription(WsdlNS.ServiceDescription serviceDescription) |
| | | 96 | | { |
| | 24 | 97 | | if (serviceDescription == null) |
| | | 98 | | { |
| | 0 | 99 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(serviceDescription)); |
| | | 100 | | } |
| | | 101 | | |
| | 24 | 102 | | MetadataSection section = new MetadataSection(); |
| | | 103 | | |
| | 24 | 104 | | section.Dialect = ServiceDescriptionDialect; |
| | 24 | 105 | | section.Identifier = serviceDescription.TargetNamespace; |
| | 24 | 106 | | section.Metadata = serviceDescription; |
| | | 107 | | |
| | 24 | 108 | | return section; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | internal static bool IsPolicyElement(XmlElement policy) |
| | | 112 | | { |
| | 0 | 113 | | return (policy.NamespaceURI == MetadataStrings.WSPolicy.NamespaceUri |
| | 0 | 114 | | || policy.NamespaceURI == MetadataStrings.WSPolicy.NamespaceUri15) |
| | 0 | 115 | | && policy.LocalName == MetadataStrings.WSPolicy.Elements.Policy; |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | } |