| | | 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 CoreWCF.Channels; |
| | | 8 | | using CoreWCF.Runtime; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Description |
| | | 11 | | { |
| | | 12 | | //For export we provide a builder that allows the gradual construction of a set of MetadataDocuments |
| | | 13 | | public abstract class MetadataExporter |
| | | 14 | | { |
| | 47 | 15 | | private PolicyVersion _policyVersion = PolicyVersion.Policy12; |
| | | 16 | | |
| | | 17 | | //prevent inheritance until we are ready to allow it. |
| | 47 | 18 | | internal MetadataExporter() |
| | | 19 | | { |
| | 47 | 20 | | } |
| | | 21 | | |
| | | 22 | | public PolicyVersion PolicyVersion |
| | | 23 | | { |
| | 511 | 24 | | get => _policyVersion; |
| | 24 | 25 | | set => _policyVersion = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(val |
| | | 26 | | } |
| | | 27 | | |
| | 322 | 28 | | public Collection<MetadataConversionError> Errors { get; } = new Collection<MetadataConversionError>(); |
| | 2892 | 29 | | public Dictionary<object, object> State { get; } = new Dictionary<object, object>(); |
| | | 30 | | |
| | | 31 | | public abstract void ExportContract(ContractDescription contract); |
| | | 32 | | public abstract void ExportEndpoint(ServiceEndpoint endpoint); |
| | | 33 | | |
| | | 34 | | public abstract MetadataSet GetGeneratedMetadata(); |
| | | 35 | | |
| | | 36 | | internal PolicyConversionContext ExportPolicy(ServiceEndpoint endpoint, BindingParameterCollection bindingParame |
| | | 37 | | { |
| | 41 | 38 | | PolicyConversionContext policyContext = new ExportedPolicyConversionContext(endpoint, bindingParameters); |
| | | 39 | | |
| | 246 | 40 | | foreach (IPolicyExportExtension exporter in policyContext.BindingElements.FindAll<IPolicyExportExtension>()) |
| | | 41 | | { |
| | | 42 | | try |
| | | 43 | | { |
| | 82 | 44 | | exporter.ExportPolicy(this, policyContext); |
| | 82 | 45 | | } |
| | 0 | 46 | | catch (Exception e) |
| | | 47 | | { |
| | 0 | 48 | | if (Fx.IsFatal(e)) |
| | | 49 | | { |
| | 0 | 50 | | throw; |
| | | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateExtensionException(exporter, e)); |
| | | 54 | | } |
| | | 55 | | } |
| | | 56 | | |
| | 41 | 57 | | return policyContext; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | protected internal PolicyConversionContext ExportPolicy(ServiceEndpoint endpoint) |
| | | 61 | | { |
| | 9 | 62 | | return ExportPolicy(endpoint, null); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | private sealed class ExportedPolicyConversionContext : PolicyConversionContext |
| | | 66 | | { |
| | | 67 | | private readonly PolicyAssertionCollection _bindingAssertions; |
| | | 68 | | private readonly Dictionary<OperationDescription, PolicyAssertionCollection> _operationBindingAssertions; |
| | | 69 | | private readonly Dictionary<MessageDescription, PolicyAssertionCollection> _messageBindingAssertions; |
| | | 70 | | private readonly Dictionary<FaultDescription, PolicyAssertionCollection> _faultBindingAssertions; |
| | | 71 | | |
| | | 72 | | internal ExportedPolicyConversionContext(ServiceEndpoint endpoint, BindingParameterCollection bindingParamet |
| | 41 | 73 | | : base(endpoint) |
| | | 74 | | { |
| | 41 | 75 | | BindingElements = endpoint.Binding.CreateBindingElements(); |
| | 41 | 76 | | _bindingAssertions = new PolicyAssertionCollection(); |
| | 41 | 77 | | _operationBindingAssertions = new Dictionary<OperationDescription, PolicyAssertionCollection>(); |
| | 41 | 78 | | _messageBindingAssertions = new Dictionary<MessageDescription, PolicyAssertionCollection>(); |
| | 41 | 79 | | _faultBindingAssertions = new Dictionary<FaultDescription, PolicyAssertionCollection>(); |
| | 41 | 80 | | BindingParameters = bindingParameters; |
| | 41 | 81 | | } |
| | | 82 | | |
| | 167 | 83 | | public override BindingElementCollection BindingElements { get; } |
| | | 84 | | |
| | 40 | 85 | | public override BindingParameterCollection BindingParameters { get; } |
| | | 86 | | |
| | 145 | 87 | | public override PolicyAssertionCollection GetBindingAssertions() => _bindingAssertions; |
| | | 88 | | |
| | | 89 | | public override PolicyAssertionCollection GetOperationBindingAssertions(OperationDescription operation) |
| | | 90 | | { |
| | 159 | 91 | | lock (_operationBindingAssertions) |
| | | 92 | | { |
| | 159 | 93 | | if (!_operationBindingAssertions.ContainsKey(operation)) |
| | | 94 | | { |
| | 159 | 95 | | _operationBindingAssertions.Add(operation, new PolicyAssertionCollection()); |
| | | 96 | | } |
| | 159 | 97 | | } |
| | | 98 | | |
| | 159 | 99 | | return _operationBindingAssertions[operation]; |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | public override PolicyAssertionCollection GetMessageBindingAssertions(MessageDescription message) |
| | | 103 | | { |
| | 318 | 104 | | lock (_messageBindingAssertions) |
| | | 105 | | { |
| | 318 | 106 | | if (!_messageBindingAssertions.ContainsKey(message)) |
| | | 107 | | { |
| | 318 | 108 | | _messageBindingAssertions.Add(message, new PolicyAssertionCollection()); |
| | | 109 | | } |
| | 318 | 110 | | } |
| | 318 | 111 | | return _messageBindingAssertions[message]; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | public override PolicyAssertionCollection GetFaultBindingAssertions(FaultDescription fault) |
| | | 115 | | { |
| | 0 | 116 | | lock (_faultBindingAssertions) |
| | | 117 | | { |
| | 0 | 118 | | if (!_faultBindingAssertions.ContainsKey(fault)) |
| | | 119 | | { |
| | 0 | 120 | | _faultBindingAssertions.Add(fault, new PolicyAssertionCollection()); |
| | | 121 | | } |
| | 0 | 122 | | } |
| | 0 | 123 | | return _faultBindingAssertions[fault]; |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | private Exception CreateExtensionException(IPolicyExportExtension exporter, Exception e) |
| | | 129 | | { |
| | 0 | 130 | | string errorMessage = SR.Format(SR.PolicyExtensionExportError, exporter.GetType(), e.Message); |
| | 0 | 131 | | return new InvalidOperationException(errorMessage, e); |
| | | 132 | | } |
| | | 133 | | } |
| | | 134 | | } |