| | | 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.Globalization; |
| | | 7 | | using System.Text; |
| | | 8 | | using CoreWCF.Description; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Channels |
| | | 11 | | { |
| | | 12 | | internal struct ChannelRequirements |
| | | 13 | | { |
| | | 14 | | public bool usesInput; |
| | | 15 | | public bool usesReply; |
| | | 16 | | public bool usesOutput; |
| | | 17 | | public bool usesRequest; |
| | | 18 | | public SessionMode sessionMode; |
| | | 19 | | |
| | | 20 | | public static void ComputeContractRequirements(ContractDescription contractDescription, |
| | | 21 | | out ChannelRequirements requirements) |
| | | 22 | | { |
| | 641 | 23 | | requirements = new ChannelRequirements |
| | 641 | 24 | | { |
| | 641 | 25 | | usesInput = false, |
| | 641 | 26 | | usesReply = false, |
| | 641 | 27 | | usesOutput = false, |
| | 641 | 28 | | usesRequest = false, |
| | 641 | 29 | | sessionMode = contractDescription.SessionMode |
| | 641 | 30 | | }; |
| | | 31 | | |
| | 7338 | 32 | | for (int i = 0; i < contractDescription.Operations.Count; i++) |
| | | 33 | | { |
| | 3028 | 34 | | OperationDescription operation = contractDescription.Operations[i]; |
| | 3028 | 35 | | bool oneWay = (operation.IsOneWay); |
| | 3028 | 36 | | if (!operation.IsServerInitiated()) |
| | | 37 | | { |
| | 3026 | 38 | | if (oneWay) |
| | | 39 | | { |
| | 324 | 40 | | requirements.usesInput = true; |
| | | 41 | | } |
| | | 42 | | else |
| | | 43 | | { |
| | 2702 | 44 | | requirements.usesReply = true; |
| | | 45 | | } |
| | | 46 | | } |
| | | 47 | | else |
| | | 48 | | { |
| | 2 | 49 | | if (oneWay) |
| | | 50 | | { |
| | 0 | 51 | | requirements.usesOutput = true; |
| | | 52 | | } |
| | | 53 | | else |
| | | 54 | | { |
| | 2 | 55 | | requirements.usesRequest = true; |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | } |
| | 641 | 59 | | } |
| | | 60 | | |
| | | 61 | | public static Type[] ComputeRequiredChannels(ref ChannelRequirements requirements) |
| | | 62 | | { |
| | 641 | 63 | | if (requirements.usesOutput || requirements.usesRequest) |
| | | 64 | | { |
| | 2 | 65 | | switch (requirements.sessionMode) |
| | | 66 | | { |
| | | 67 | | case SessionMode.Allowed: |
| | 2 | 68 | | return new[] { |
| | 2 | 69 | | typeof(IDuplexChannel), |
| | 2 | 70 | | typeof(IDuplexSessionChannel), |
| | 2 | 71 | | }; |
| | | 72 | | |
| | | 73 | | case SessionMode.Required: |
| | 0 | 74 | | return new[] { |
| | 0 | 75 | | typeof(IDuplexSessionChannel), |
| | 0 | 76 | | }; |
| | | 77 | | |
| | | 78 | | case SessionMode.NotAllowed: |
| | 0 | 79 | | return new[] { |
| | 0 | 80 | | typeof(IDuplexChannel), |
| | 0 | 81 | | }; |
| | | 82 | | } |
| | | 83 | | } |
| | 639 | 84 | | else if (requirements.usesInput && requirements.usesReply) |
| | | 85 | | { |
| | 15 | 86 | | switch (requirements.sessionMode) |
| | | 87 | | { |
| | | 88 | | case SessionMode.Allowed: |
| | 14 | 89 | | return new[] { |
| | 14 | 90 | | typeof(IRequestChannel), |
| | 14 | 91 | | typeof(IRequestSessionChannel), |
| | 14 | 92 | | typeof(IDuplexChannel), |
| | 14 | 93 | | typeof(IDuplexSessionChannel), |
| | 14 | 94 | | }; |
| | | 95 | | |
| | | 96 | | case SessionMode.Required: |
| | 1 | 97 | | return new[] { |
| | 1 | 98 | | typeof(IRequestSessionChannel), |
| | 1 | 99 | | typeof(IDuplexSessionChannel), |
| | 1 | 100 | | }; |
| | | 101 | | |
| | | 102 | | case SessionMode.NotAllowed: |
| | 0 | 103 | | return new[] { |
| | 0 | 104 | | typeof(IRequestChannel), |
| | 0 | 105 | | typeof(IDuplexChannel), |
| | 0 | 106 | | }; |
| | | 107 | | } |
| | | 108 | | } |
| | 624 | 109 | | else if (requirements.usesInput) |
| | | 110 | | { |
| | 52 | 111 | | switch (requirements.sessionMode) |
| | | 112 | | { |
| | | 113 | | case SessionMode.Allowed: |
| | 52 | 114 | | return new[] { |
| | 52 | 115 | | typeof(IOutputChannel), |
| | 52 | 116 | | typeof(IOutputSessionChannel), |
| | 52 | 117 | | typeof(IRequestChannel), |
| | 52 | 118 | | typeof(IRequestSessionChannel), |
| | 52 | 119 | | typeof(IDuplexChannel), |
| | 52 | 120 | | typeof(IDuplexSessionChannel), |
| | 52 | 121 | | }; |
| | | 122 | | |
| | | 123 | | case SessionMode.Required: |
| | 0 | 124 | | return new[] { |
| | 0 | 125 | | typeof(IOutputSessionChannel), |
| | 0 | 126 | | typeof(IRequestSessionChannel), |
| | 0 | 127 | | typeof(IDuplexSessionChannel), |
| | 0 | 128 | | }; |
| | | 129 | | |
| | | 130 | | case SessionMode.NotAllowed: |
| | 0 | 131 | | return new[] { |
| | 0 | 132 | | typeof(IOutputChannel), |
| | 0 | 133 | | typeof(IRequestChannel), |
| | 0 | 134 | | typeof(IDuplexChannel), |
| | 0 | 135 | | }; |
| | | 136 | | } |
| | | 137 | | } |
| | 572 | 138 | | else if (requirements.usesReply) |
| | | 139 | | { |
| | 572 | 140 | | switch (requirements.sessionMode) |
| | | 141 | | { |
| | | 142 | | case SessionMode.Allowed: |
| | 564 | 143 | | return new[] { |
| | 564 | 144 | | typeof(IRequestChannel), |
| | 564 | 145 | | typeof(IRequestSessionChannel), |
| | 564 | 146 | | typeof(IDuplexChannel), |
| | 564 | 147 | | typeof(IDuplexSessionChannel), |
| | 564 | 148 | | }; |
| | | 149 | | |
| | | 150 | | case SessionMode.Required: |
| | 8 | 151 | | return new[] { |
| | 8 | 152 | | typeof(IRequestSessionChannel), |
| | 8 | 153 | | typeof(IDuplexSessionChannel), |
| | 8 | 154 | | }; |
| | | 155 | | |
| | | 156 | | case SessionMode.NotAllowed: |
| | 0 | 157 | | return new[] { |
| | 0 | 158 | | typeof(IRequestChannel), |
| | 0 | 159 | | typeof(IDuplexChannel), |
| | 0 | 160 | | }; |
| | | 161 | | } |
| | | 162 | | } |
| | | 163 | | else |
| | | 164 | | { |
| | 0 | 165 | | switch (requirements.sessionMode) |
| | | 166 | | { |
| | | 167 | | case SessionMode.Allowed: |
| | 0 | 168 | | return new[] { |
| | 0 | 169 | | typeof(IOutputSessionChannel), |
| | 0 | 170 | | typeof(IOutputChannel), |
| | 0 | 171 | | typeof(IRequestSessionChannel), |
| | 0 | 172 | | typeof(IRequestChannel), |
| | 0 | 173 | | typeof(IDuplexChannel), |
| | 0 | 174 | | typeof(IDuplexSessionChannel), |
| | 0 | 175 | | }; |
| | | 176 | | |
| | | 177 | | case SessionMode.Required: |
| | 0 | 178 | | return new[] { |
| | 0 | 179 | | typeof(IOutputSessionChannel), |
| | 0 | 180 | | typeof(IRequestSessionChannel), |
| | 0 | 181 | | typeof(IDuplexSessionChannel), |
| | 0 | 182 | | }; |
| | | 183 | | |
| | | 184 | | case SessionMode.NotAllowed: |
| | 0 | 185 | | return new[] { |
| | 0 | 186 | | typeof(IOutputChannel), |
| | 0 | 187 | | typeof(IRequestChannel), |
| | 0 | 188 | | typeof(IDuplexChannel), |
| | 0 | 189 | | }; |
| | | 190 | | } |
| | | 191 | | } |
| | 0 | 192 | | return null; |
| | | 193 | | } |
| | | 194 | | |
| | | 195 | | public static bool IsSessionful(Type channelType) |
| | | 196 | | { |
| | 0 | 197 | | return (channelType == typeof(IDuplexSessionChannel) || |
| | 0 | 198 | | channelType == typeof(IOutputSessionChannel) || |
| | 0 | 199 | | channelType == typeof(IInputSessionChannel) || |
| | 0 | 200 | | channelType == typeof(IReplySessionChannel) || |
| | 0 | 201 | | channelType == typeof(IRequestSessionChannel)); |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | public static bool IsOneWay(Type channelType) |
| | | 205 | | { |
| | 0 | 206 | | return (channelType == typeof(IOutputChannel) || |
| | 0 | 207 | | channelType == typeof(IInputChannel) || |
| | 0 | 208 | | channelType == typeof(IInputSessionChannel) || |
| | 0 | 209 | | channelType == typeof(IOutputSessionChannel)); |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | public static bool IsRequestReply(Type channelType) |
| | | 213 | | { |
| | 0 | 214 | | return (channelType == typeof(IRequestChannel) || |
| | 0 | 215 | | channelType == typeof(IReplyChannel) || |
| | 0 | 216 | | channelType == typeof(IReplySessionChannel) || |
| | 0 | 217 | | channelType == typeof(IRequestSessionChannel)); |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | public static bool IsDuplex(Type channelType) |
| | | 221 | | { |
| | 0 | 222 | | return (channelType == typeof(IDuplexChannel) || |
| | 0 | 223 | | channelType == typeof(IDuplexSessionChannel)); |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | public static Exception CantCreateChannelException(IEnumerable<Type> supportedChannels, IEnumerable<Type> requir |
| | | 227 | | { |
| | 0 | 228 | | string contractChannelTypesString = ""; |
| | 0 | 229 | | string bindingChannelTypesString = ""; |
| | | 230 | | |
| | 0 | 231 | | Exception exception = BindingContractMismatchException(supportedChannels, requiredChannels, bindingName, |
| | 0 | 232 | | ref contractChannelTypesString, ref bindingChannelTypesString); |
| | | 233 | | |
| | 0 | 234 | | if (exception == null) |
| | | 235 | | { |
| | | 236 | | // none of the obvious speculations about the failure holds, so we fall back to the generic error messag |
| | 0 | 237 | | exception = new InvalidOperationException(SR.Format(SR.CouldnTCreateChannelForType2, bindingName, contra |
| | | 238 | | } |
| | | 239 | | |
| | 0 | 240 | | return exception; |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | public static Exception BindingContractMismatchException(IEnumerable<Type> supportedChannels, IEnumerable<Type> |
| | | 244 | | string bindingName, ref string contractChannelTypesString, ref string bindingChannelTypesString) |
| | | 245 | | { |
| | 0 | 246 | | StringBuilder contractChannelTypes = new StringBuilder(); |
| | 0 | 247 | | bool contractRequiresOneWay = true; |
| | 0 | 248 | | bool contractRequiresRequestReply = true; |
| | 0 | 249 | | bool contractRequiresDuplex = true; |
| | 0 | 250 | | bool contractRequiresTwoWay = true; // request-reply or duplex |
| | 0 | 251 | | bool contractRequiresSession = true; |
| | 0 | 252 | | bool contractRequiresDatagram = true; |
| | 0 | 253 | | foreach (Type channelType in requiredChannels) |
| | | 254 | | { |
| | 0 | 255 | | if (contractChannelTypes.Length > 0) |
| | | 256 | | { |
| | 0 | 257 | | contractChannelTypes.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator); |
| | 0 | 258 | | contractChannelTypes.Append(" "); |
| | | 259 | | } |
| | 0 | 260 | | string typeString = channelType.ToString(); |
| | 0 | 261 | | contractChannelTypes.Append(typeString.Substring(typeString.LastIndexOf('.') + 1)); |
| | | 262 | | |
| | 0 | 263 | | if (!IsOneWay(channelType)) |
| | | 264 | | { |
| | 0 | 265 | | contractRequiresOneWay = false; |
| | | 266 | | } |
| | 0 | 267 | | if (!IsRequestReply(channelType)) |
| | | 268 | | { |
| | 0 | 269 | | contractRequiresRequestReply = false; |
| | | 270 | | } |
| | 0 | 271 | | if (!IsDuplex(channelType)) |
| | | 272 | | { |
| | 0 | 273 | | contractRequiresDuplex = false; |
| | | 274 | | } |
| | 0 | 275 | | if (!(IsRequestReply(channelType) || IsDuplex(channelType))) |
| | | 276 | | { |
| | 0 | 277 | | contractRequiresTwoWay = false; |
| | | 278 | | } |
| | 0 | 279 | | if (!IsSessionful(channelType)) |
| | | 280 | | { |
| | 0 | 281 | | contractRequiresSession = false; |
| | | 282 | | } |
| | | 283 | | else |
| | | 284 | | { |
| | 0 | 285 | | contractRequiresDatagram = false; |
| | | 286 | | } |
| | | 287 | | } |
| | | 288 | | |
| | 0 | 289 | | StringBuilder bindingChannelTypes = new StringBuilder(); |
| | 0 | 290 | | bool bindingSupportsOneWay = false; |
| | 0 | 291 | | bool bindingSupportsRequestReply = false; |
| | 0 | 292 | | bool bindingSupportsDuplex = false; |
| | 0 | 293 | | bool bindingSupportsSession = false; |
| | 0 | 294 | | bool bindingSupportsDatagram = false; |
| | 0 | 295 | | bool bindingSupportsAtLeastOneChannelType = false; |
| | 0 | 296 | | foreach (Type channelType in supportedChannels) |
| | | 297 | | { |
| | 0 | 298 | | bindingSupportsAtLeastOneChannelType = true; |
| | 0 | 299 | | if (bindingChannelTypes.Length > 0) |
| | | 300 | | { |
| | 0 | 301 | | bindingChannelTypes.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator); |
| | 0 | 302 | | bindingChannelTypes.Append(" "); |
| | | 303 | | } |
| | 0 | 304 | | string typeString = channelType.ToString(); |
| | 0 | 305 | | bindingChannelTypes.Append(typeString.Substring(typeString.LastIndexOf('.') + 1)); |
| | | 306 | | |
| | 0 | 307 | | if (IsOneWay(channelType)) |
| | | 308 | | { |
| | 0 | 309 | | bindingSupportsOneWay = true; |
| | | 310 | | } |
| | 0 | 311 | | if (IsRequestReply(channelType)) |
| | | 312 | | { |
| | 0 | 313 | | bindingSupportsRequestReply = true; |
| | | 314 | | } |
| | 0 | 315 | | if (IsDuplex(channelType)) |
| | | 316 | | { |
| | 0 | 317 | | bindingSupportsDuplex = true; |
| | | 318 | | } |
| | 0 | 319 | | if (IsSessionful(channelType)) |
| | | 320 | | { |
| | 0 | 321 | | bindingSupportsSession = true; |
| | | 322 | | } |
| | | 323 | | else |
| | | 324 | | { |
| | 0 | 325 | | bindingSupportsDatagram = true; |
| | | 326 | | } |
| | | 327 | | } |
| | 0 | 328 | | bool bindingSupportsTwoWay = bindingSupportsRequestReply || bindingSupportsDuplex; |
| | | 329 | | |
| | 0 | 330 | | if (!bindingSupportsAtLeastOneChannelType) |
| | | 331 | | { |
| | 0 | 332 | | return new InvalidOperationException(SR.Format(SR.BindingDoesnTSupportAnyChannelTypes1, bindingName)); |
| | | 333 | | } |
| | 0 | 334 | | if (contractRequiresSession && !bindingSupportsSession) |
| | | 335 | | { |
| | 0 | 336 | | return new InvalidOperationException(SR.Format(SR.BindingDoesnTSupportSessionButContractRequires1, bindi |
| | | 337 | | } |
| | 0 | 338 | | if (contractRequiresDatagram && !bindingSupportsDatagram) |
| | | 339 | | { |
| | 0 | 340 | | return new InvalidOperationException(SR.Format(SR.BindingDoesntSupportDatagramButContractRequires, bindi |
| | | 341 | | } |
| | 0 | 342 | | if (contractRequiresDuplex && !bindingSupportsDuplex) |
| | | 343 | | { |
| | 0 | 344 | | return new InvalidOperationException(SR.Format(SR.BindingDoesnTSupportDuplexButContractRequires1, bindin |
| | | 345 | | } |
| | 0 | 346 | | if (contractRequiresRequestReply && !bindingSupportsRequestReply) |
| | | 347 | | { |
| | 0 | 348 | | return new InvalidOperationException(SR.Format(SR.BindingDoesnTSupportRequestReplyButContract1, bindingN |
| | | 349 | | } |
| | 0 | 350 | | if (contractRequiresOneWay && !bindingSupportsOneWay) |
| | | 351 | | { |
| | 0 | 352 | | return new InvalidOperationException(SR.Format(SR.BindingDoesnTSupportOneWayButContractRequires1, bindin |
| | | 353 | | } |
| | 0 | 354 | | if (contractRequiresTwoWay && !bindingSupportsTwoWay) |
| | | 355 | | { |
| | 0 | 356 | | return new InvalidOperationException(SR.Format(SR.BindingDoesnTSupportTwoWayButContractRequires1, bindin |
| | | 357 | | } |
| | | 358 | | |
| | 0 | 359 | | contractChannelTypesString = contractChannelTypes.ToString(); |
| | 0 | 360 | | bindingChannelTypesString = bindingChannelTypes.ToString(); |
| | | 361 | | |
| | 0 | 362 | | return null; |
| | | 363 | | } |
| | | 364 | | } |
| | | 365 | | } |