| | | 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.ObjectModel; |
| | | 6 | | using CoreWCF.Collections.Generic; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Description |
| | | 9 | | { |
| | | 10 | | public class ContractDescription |
| | | 11 | | { |
| | | 12 | | private XmlName _name; |
| | | 13 | | private string _ns; |
| | | 14 | | private SessionMode _sessionMode; |
| | | 15 | | |
| | | 16 | | //ProtectionLevel protectionLevel; |
| | | 17 | | //bool hasProtectionLevel; |
| | | 18 | | |
| | | 19 | | |
| | | 20 | | public ContractDescription(string name) |
| | 7 | 21 | | : this(name, null) |
| | | 22 | | { |
| | 7 | 23 | | } |
| | | 24 | | |
| | 685 | 25 | | public ContractDescription(string name, string ns) |
| | | 26 | | { |
| | | 27 | | // the property setter validates given value |
| | 685 | 28 | | Name = name; |
| | 685 | 29 | | if (!string.IsNullOrEmpty(ns)) |
| | | 30 | | { |
| | 678 | 31 | | NamingHelper.CheckUriParameter(ns, nameof(ns)); |
| | | 32 | | } |
| | | 33 | | |
| | 685 | 34 | | Operations = new OperationDescriptionCollection(); |
| | 685 | 35 | | _ns = ns ?? NamingHelper.DefaultNamespace; // ns can be "" |
| | 685 | 36 | | } |
| | | 37 | | |
| | 1340 | 38 | | public string ConfigurationName { get; set; } |
| | | 39 | | |
| | 12865 | 40 | | public Type ContractType { get; set; } |
| | | 41 | | |
| | 1400 | 42 | | public Type CallbackContractType { get; set; } |
| | | 43 | | |
| | | 44 | | public string Name |
| | | 45 | | { |
| | 6907 | 46 | | get { return _name.EncodedName; } |
| | | 47 | | set |
| | | 48 | | { |
| | 685 | 49 | | if (value == null) |
| | | 50 | | { |
| | 0 | 51 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 52 | | } |
| | | 53 | | |
| | 685 | 54 | | if (value.Length == 0) |
| | | 55 | | { |
| | 0 | 56 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 57 | | new ArgumentOutOfRangeException(nameof(value), SR.SFxContractDescriptionNameCannotBeEmpty)); |
| | | 58 | | } |
| | 685 | 59 | | _name = new XmlName(value, true /*isEncoded*/); |
| | 685 | 60 | | } |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | public string Namespace |
| | | 64 | | { |
| | 12880 | 65 | | get { return _ns; } |
| | | 66 | | set |
| | | 67 | | { |
| | 0 | 68 | | if (!string.IsNullOrEmpty(value)) |
| | | 69 | | { |
| | 0 | 70 | | NamingHelper.CheckUriProperty(value, "Namespace"); |
| | | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | _ns = value; |
| | 0 | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | 85702 | 77 | | public OperationDescriptionCollection Operations { get; } |
| | | 78 | | |
| | 44 | 79 | | internal bool HasProtectionLevel => false; |
| | | 80 | | |
| | | 81 | | public SessionMode SessionMode |
| | | 82 | | { |
| | 2098 | 83 | | get { return _sessionMode; } |
| | | 84 | | set |
| | | 85 | | { |
| | 678 | 86 | | if (!SessionModeHelper.IsDefined(value)) |
| | | 87 | | { |
| | 0 | 88 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val |
| | | 89 | | } |
| | | 90 | | |
| | 678 | 91 | | _sessionMode = value; |
| | 678 | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | public KeyedCollection<Type, IContractBehavior> ContractBehaviors |
| | | 96 | | { |
| | 2739 | 97 | | get { return Behaviors; } |
| | | 98 | | } |
| | | 99 | | |
| | 7431 | 100 | | internal KeyedByTypeCollection<IContractBehavior> Behaviors { get; } = new KeyedByTypeCollection<IContractBehavi |
| | | 101 | | |
| | | 102 | | public Collection<ContractDescription> GetInheritedContracts() |
| | | 103 | | { |
| | 1266 | 104 | | Collection<ContractDescription> result = new Collection<ContractDescription>(); |
| | 13564 | 105 | | for (int i = 0; i < Operations.Count; i++) |
| | | 106 | | { |
| | 5516 | 107 | | OperationDescription od = Operations[i]; |
| | 5516 | 108 | | if (od.DeclaringContract != this) |
| | | 109 | | { |
| | 301 | 110 | | ContractDescription inheritedContract = od.DeclaringContract; |
| | 301 | 111 | | if (!result.Contains(inheritedContract)) |
| | | 112 | | { |
| | 157 | 113 | | result.Add(inheritedContract); |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | } |
| | 1266 | 117 | | return result; |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | public static ContractDescription GetContract<TService>(Type contractType) where TService : class |
| | | 121 | | { |
| | 511 | 122 | | if (contractType == null) |
| | | 123 | | { |
| | 0 | 124 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contractType)); |
| | | 125 | | } |
| | | 126 | | |
| | 511 | 127 | | var typeLoader = new TypeLoader<TService>(); |
| | 511 | 128 | | ContractDescription description = typeLoader.LoadContractDescription(contractType); |
| | 507 | 129 | | return description; |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | public static ContractDescription GetContract<TService>(Type contractType, object serviceImplementation) where T |
| | | 133 | | { |
| | 89 | 134 | | if (contractType == null) |
| | | 135 | | { |
| | 0 | 136 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contractType)); |
| | | 137 | | } |
| | | 138 | | |
| | 89 | 139 | | if (serviceImplementation == null) |
| | | 140 | | { |
| | 0 | 141 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(serviceImplementation)); |
| | | 142 | | } |
| | | 143 | | |
| | 89 | 144 | | var typeLoader = new TypeLoader<TService>(); |
| | 89 | 145 | | ContractDescription description = typeLoader.LoadContractDescription(contractType, serviceImplementation); |
| | 89 | 146 | | return description; |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | internal void EnsureInvariants() |
| | | 150 | | { |
| | 641 | 151 | | if (string.IsNullOrEmpty(Name)) |
| | | 152 | | { |
| | 0 | 153 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 154 | | SR.AChannelServiceEndpointSContractSNameIsNull0)); |
| | | 155 | | } |
| | 641 | 156 | | if (Namespace == null) |
| | | 157 | | { |
| | 0 | 158 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 159 | | SR.AChannelServiceEndpointSContractSNamespace0)); |
| | | 160 | | } |
| | 641 | 161 | | if (Operations.Count == 0) |
| | | 162 | | { |
| | 0 | 163 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 164 | | SR.Format(SR.SFxContractHasZeroOperations, Name))); |
| | | 165 | | } |
| | 641 | 166 | | bool thereIsAtLeastOneInitiatingOperation = false; |
| | 7338 | 167 | | for (int i = 0; i < Operations.Count; i++) |
| | | 168 | | { |
| | 3028 | 169 | | OperationDescription operationDescription = Operations[i]; |
| | 3028 | 170 | | operationDescription.EnsureInvariants(); |
| | 3028 | 171 | | if (operationDescription.IsInitiating) |
| | | 172 | | { |
| | 3020 | 173 | | thereIsAtLeastOneInitiatingOperation = true; |
| | | 174 | | } |
| | | 175 | | |
| | 3028 | 176 | | if ((!operationDescription.IsInitiating || operationDescription.IsTerminating) |
| | 3028 | 177 | | && (SessionMode != SessionMode.Required)) |
| | | 178 | | { |
| | 0 | 179 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 180 | | SR.Format(SR.ContractIsNotSelfConsistentItHasOneOrMore2, Name))); |
| | | 181 | | } |
| | | 182 | | } |
| | 641 | 183 | | if (!thereIsAtLeastOneInitiatingOperation) |
| | | 184 | | { |
| | 0 | 185 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException( |
| | 0 | 186 | | SR.Format(SR.SFxContractHasZeroInitiatingOperations, Name))); |
| | | 187 | | } |
| | 641 | 188 | | } |
| | | 189 | | |
| | | 190 | | public bool IsDuplex() |
| | | 191 | | { |
| | 758 | 192 | | for (int i = 0; i < Operations.Count; ++i) |
| | | 193 | | { |
| | 308 | 194 | | if (Operations[i].IsServerInitiated()) |
| | | 195 | | { |
| | 0 | 196 | | return true; |
| | | 197 | | } |
| | | 198 | | } |
| | | 199 | | |
| | 71 | 200 | | return false; |
| | | 201 | | } |
| | | 202 | | } |
| | | 203 | | } |