| | | 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; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using CoreWCF.Runtime; |
| | | 8 | | using CoreWCF.Security; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Channels |
| | | 11 | | { |
| | | 12 | | public sealed class MessageProperties : IDictionary<string, object>, IDisposable |
| | | 13 | | { |
| | | 14 | | private Property[] _properties; |
| | | 15 | | private int _propertyCount; |
| | | 16 | | private MessageEncoder _encoder; |
| | | 17 | | private Uri _via; |
| | | 18 | | private object _allowOutputBatching; |
| | | 19 | | private SecurityMessageProperty _security; |
| | | 20 | | private bool _disposed; |
| | | 21 | | private const int InitialPropertyCount = 2; |
| | | 22 | | private const int MaxRecycledArrayLength = 8; |
| | | 23 | | private const string ViaKey = "Via"; |
| | | 24 | | private const string AllowOutputBatchingKey = "AllowOutputBatching"; |
| | | 25 | | private const string SecurityKey = "Security"; |
| | | 26 | | private const string EncoderKey = "Encoder"; |
| | | 27 | | private const int NotFoundIndex = -1; |
| | | 28 | | private const int ViaIndex = -2; |
| | | 29 | | private const int AllowOutputBatchingIndex = -3; |
| | | 30 | | private const int SecurityIndex = -4; |
| | | 31 | | private const int EncoderIndex = -5; |
| | 5 | 32 | | private static readonly object s_trueBool = true; |
| | 5 | 33 | | private static readonly object s_falseBool = false; |
| | | 34 | | |
| | 5910 | 35 | | public MessageProperties() |
| | | 36 | | { |
| | 5910 | 37 | | } |
| | | 38 | | |
| | 6 | 39 | | public MessageProperties(MessageProperties properties) |
| | | 40 | | { |
| | 6 | 41 | | CopyProperties(properties); |
| | 6 | 42 | | } |
| | | 43 | | |
| | 268 | 44 | | internal MessageProperties(KeyValuePair<string, object>[] array) |
| | | 45 | | { |
| | 268 | 46 | | if (array == null) |
| | | 47 | | { |
| | 0 | 48 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(array)); |
| | | 49 | | } |
| | | 50 | | |
| | 268 | 51 | | CopyProperties(array); |
| | 268 | 52 | | } |
| | | 53 | | |
| | | 54 | | private void ThrowDisposed() |
| | | 55 | | { |
| | 0 | 56 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(string.Empty, SR.Forma |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | public object this[string name] |
| | | 60 | | { |
| | | 61 | | get |
| | | 62 | | { |
| | 146 | 63 | | if (_disposed) |
| | | 64 | | { |
| | 0 | 65 | | ThrowDisposed(); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | |
| | 146 | 69 | | if (!TryGetValue(name, out object value)) |
| | | 70 | | { |
| | 0 | 71 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.Message |
| | | 72 | | } |
| | | 73 | | |
| | 146 | 74 | | return value; |
| | | 75 | | } |
| | | 76 | | set |
| | | 77 | | { |
| | 1917 | 78 | | if (_disposed) |
| | | 79 | | { |
| | 0 | 80 | | ThrowDisposed(); |
| | | 81 | | } |
| | | 82 | | |
| | 1917 | 83 | | UpdateProperty(name, value, false); |
| | 1917 | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | internal bool CanRecycle |
| | | 88 | | { |
| | | 89 | | get |
| | | 90 | | { |
| | 621 | 91 | | return _properties == null || _properties.Length <= MaxRecycledArrayLength; |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | public int Count |
| | | 96 | | { |
| | | 97 | | get |
| | | 98 | | { |
| | 625 | 99 | | if (_disposed) |
| | | 100 | | { |
| | 0 | 101 | | ThrowDisposed(); |
| | | 102 | | } |
| | | 103 | | |
| | 625 | 104 | | return _propertyCount; |
| | | 105 | | } |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | public MessageEncoder Encoder |
| | | 109 | | { |
| | | 110 | | get |
| | | 111 | | { |
| | 41 | 112 | | if (_disposed) |
| | | 113 | | { |
| | 0 | 114 | | ThrowDisposed(); |
| | | 115 | | } |
| | | 116 | | |
| | 41 | 117 | | return _encoder; |
| | | 118 | | } |
| | | 119 | | set |
| | | 120 | | { |
| | 5361 | 121 | | if (_disposed) |
| | | 122 | | { |
| | 0 | 123 | | ThrowDisposed(); |
| | | 124 | | } |
| | | 125 | | |
| | 5361 | 126 | | AdjustPropertyCount((object)_encoder == null, (object)value == null); |
| | 5361 | 127 | | _encoder = value; |
| | 5361 | 128 | | } |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | public bool AllowOutputBatching |
| | | 132 | | { |
| | | 133 | | get |
| | | 134 | | { |
| | 117 | 135 | | if (_disposed) |
| | | 136 | | { |
| | 0 | 137 | | ThrowDisposed(); |
| | | 138 | | } |
| | | 139 | | |
| | 117 | 140 | | return (object)_allowOutputBatching == s_trueBool; |
| | | 141 | | } |
| | | 142 | | set |
| | | 143 | | { |
| | 703 | 144 | | if (_disposed) |
| | | 145 | | { |
| | 0 | 146 | | ThrowDisposed(); |
| | | 147 | | } |
| | | 148 | | |
| | 703 | 149 | | AdjustPropertyCount((object)_allowOutputBatching == null, false); |
| | | 150 | | |
| | 703 | 151 | | if (value) |
| | | 152 | | { |
| | 0 | 153 | | _allowOutputBatching = s_trueBool; |
| | | 154 | | } |
| | | 155 | | else |
| | | 156 | | { |
| | 703 | 157 | | _allowOutputBatching = s_falseBool; |
| | | 158 | | } |
| | 703 | 159 | | } |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | public bool IsFixedSize |
| | | 163 | | { |
| | | 164 | | get |
| | | 165 | | { |
| | 0 | 166 | | if (_disposed) |
| | | 167 | | { |
| | 0 | 168 | | ThrowDisposed(); |
| | | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | return false; |
| | | 172 | | } |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | public bool IsReadOnly |
| | | 176 | | { |
| | | 177 | | get |
| | | 178 | | { |
| | 0 | 179 | | if (_disposed) |
| | | 180 | | { |
| | 0 | 181 | | ThrowDisposed(); |
| | | 182 | | } |
| | | 183 | | |
| | 0 | 184 | | return false; |
| | | 185 | | } |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | public ICollection<string> Keys |
| | | 189 | | { |
| | | 190 | | get |
| | | 191 | | { |
| | 0 | 192 | | if (_disposed) |
| | | 193 | | { |
| | 0 | 194 | | ThrowDisposed(); |
| | | 195 | | } |
| | | 196 | | |
| | 0 | 197 | | List<string> keys = new List<string>(); |
| | | 198 | | |
| | 0 | 199 | | if ((object)_via != null) |
| | | 200 | | { |
| | 0 | 201 | | keys.Add(ViaKey); |
| | | 202 | | } |
| | | 203 | | |
| | 0 | 204 | | if ((object)_allowOutputBatching != null) |
| | | 205 | | { |
| | 0 | 206 | | keys.Add(AllowOutputBatchingKey); |
| | | 207 | | } |
| | | 208 | | |
| | 0 | 209 | | if ((object)_security != null) |
| | | 210 | | { |
| | 0 | 211 | | keys.Add(SecurityKey); |
| | | 212 | | } |
| | | 213 | | |
| | 0 | 214 | | if ((object)_encoder != null) |
| | | 215 | | { |
| | 0 | 216 | | keys.Add(EncoderKey); |
| | | 217 | | } |
| | | 218 | | |
| | 0 | 219 | | if (_properties != null) |
| | | 220 | | { |
| | 0 | 221 | | for (int i = 0; i < _properties.Length; i++) |
| | | 222 | | { |
| | 0 | 223 | | string propertyName = _properties[i].Name; |
| | | 224 | | |
| | 0 | 225 | | if (propertyName == null) |
| | | 226 | | { |
| | | 227 | | break; |
| | | 228 | | } |
| | | 229 | | |
| | 0 | 230 | | keys.Add(propertyName); |
| | | 231 | | } |
| | | 232 | | } |
| | | 233 | | |
| | 0 | 234 | | return keys; |
| | | 235 | | } |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | public SecurityMessageProperty Security |
| | | 239 | | { |
| | | 240 | | get |
| | | 241 | | { |
| | 2884 | 242 | | if (_disposed) |
| | | 243 | | { |
| | 0 | 244 | | ThrowDisposed(); |
| | | 245 | | } |
| | | 246 | | |
| | 2884 | 247 | | return _security; |
| | | 248 | | } |
| | | 249 | | set |
| | | 250 | | { |
| | 821 | 251 | | if (_disposed) |
| | | 252 | | { |
| | 0 | 253 | | ThrowDisposed(); |
| | | 254 | | } |
| | | 255 | | |
| | 821 | 256 | | AdjustPropertyCount((object)_security == null, (object)value == null); |
| | 821 | 257 | | _security = value; |
| | 821 | 258 | | } |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | public ICollection<object> Values |
| | | 262 | | { |
| | | 263 | | get |
| | | 264 | | { |
| | 0 | 265 | | if (_disposed) |
| | | 266 | | { |
| | 0 | 267 | | ThrowDisposed(); |
| | | 268 | | } |
| | | 269 | | |
| | 0 | 270 | | List<object> values = new List<object>(); |
| | | 271 | | |
| | 0 | 272 | | if ((object)_via != null) |
| | | 273 | | { |
| | 0 | 274 | | values.Add(_via); |
| | | 275 | | } |
| | | 276 | | |
| | 0 | 277 | | if ((object)_allowOutputBatching != null) |
| | | 278 | | { |
| | 0 | 279 | | values.Add(_allowOutputBatching); |
| | | 280 | | } |
| | | 281 | | |
| | 0 | 282 | | if ((object)_security != null) |
| | | 283 | | { |
| | 0 | 284 | | values.Add(_security); |
| | | 285 | | } |
| | | 286 | | |
| | 0 | 287 | | if ((object)_encoder != null) |
| | | 288 | | { |
| | 0 | 289 | | values.Add(_encoder); |
| | | 290 | | } |
| | 0 | 291 | | if (_properties != null) |
| | | 292 | | { |
| | 0 | 293 | | for (int i = 0; i < _properties.Length; i++) |
| | | 294 | | { |
| | 0 | 295 | | if (_properties[i].Name == null) |
| | | 296 | | { |
| | | 297 | | break; |
| | | 298 | | } |
| | | 299 | | |
| | 0 | 300 | | values.Add(_properties[i].Value); |
| | | 301 | | } |
| | | 302 | | } |
| | | 303 | | |
| | 0 | 304 | | return values; |
| | | 305 | | } |
| | | 306 | | } |
| | | 307 | | |
| | | 308 | | public Uri Via |
| | | 309 | | { |
| | | 310 | | get |
| | | 311 | | { |
| | 567 | 312 | | if (_disposed) |
| | | 313 | | { |
| | 0 | 314 | | ThrowDisposed(); |
| | | 315 | | } |
| | | 316 | | |
| | 567 | 317 | | return _via; |
| | | 318 | | } |
| | | 319 | | set |
| | | 320 | | { |
| | 855 | 321 | | if (_disposed) |
| | | 322 | | { |
| | 0 | 323 | | ThrowDisposed(); |
| | | 324 | | } |
| | | 325 | | |
| | 855 | 326 | | AdjustPropertyCount((object)_via == null, (object)value == null); |
| | 855 | 327 | | _via = value; |
| | 855 | 328 | | } |
| | | 329 | | } |
| | | 330 | | |
| | | 331 | | public void Add(string name, object property) |
| | | 332 | | { |
| | 3087 | 333 | | if (_disposed) |
| | | 334 | | { |
| | 0 | 335 | | ThrowDisposed(); |
| | | 336 | | } |
| | | 337 | | |
| | 3087 | 338 | | if (property == null) |
| | | 339 | | { |
| | 0 | 340 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(property)); |
| | | 341 | | } |
| | | 342 | | |
| | 3087 | 343 | | UpdateProperty(name, property, true); |
| | 3087 | 344 | | } |
| | | 345 | | |
| | | 346 | | private void AdjustPropertyCount(bool oldValueIsNull, bool newValueIsNull) |
| | | 347 | | { |
| | 7740 | 348 | | if (newValueIsNull) |
| | | 349 | | { |
| | 773 | 350 | | if (!oldValueIsNull) |
| | | 351 | | { |
| | 0 | 352 | | _propertyCount--; |
| | | 353 | | } |
| | | 354 | | } |
| | | 355 | | else |
| | | 356 | | { |
| | 6967 | 357 | | if (oldValueIsNull) |
| | | 358 | | { |
| | 6819 | 359 | | _propertyCount++; |
| | | 360 | | } |
| | | 361 | | } |
| | 7740 | 362 | | } |
| | | 363 | | |
| | | 364 | | public void Clear() |
| | | 365 | | { |
| | 621 | 366 | | if (_disposed) |
| | | 367 | | { |
| | 0 | 368 | | ThrowDisposed(); |
| | | 369 | | } |
| | | 370 | | |
| | 621 | 371 | | if (_properties != null) |
| | | 372 | | { |
| | 4642 | 373 | | for (int i = 0; i < _properties.Length; i++) |
| | | 374 | | { |
| | 2292 | 375 | | if (_properties[i].Name == null) |
| | | 376 | | { |
| | | 377 | | break; |
| | | 378 | | } |
| | | 379 | | |
| | 1704 | 380 | | _properties[i] = new Property(); |
| | | 381 | | } |
| | | 382 | | } |
| | | 383 | | |
| | 621 | 384 | | _via = null; |
| | 621 | 385 | | _allowOutputBatching = null; |
| | 621 | 386 | | _security = null; |
| | 621 | 387 | | _encoder = null; |
| | 621 | 388 | | _propertyCount = 0; |
| | 621 | 389 | | } |
| | | 390 | | |
| | | 391 | | public void CopyProperties(MessageProperties properties) |
| | | 392 | | { |
| | | 393 | | // CopyProperties behavior should be equivalent to the behavior |
| | | 394 | | // of MergeProperties except that Merge supports property values that |
| | | 395 | | // implement the IMergeEnabledMessageProperty. Any changes to CopyProperties |
| | | 396 | | // should be reflected in MergeProperties as well. |
| | 6 | 397 | | if (properties == null) |
| | | 398 | | { |
| | 0 | 399 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(properties)); |
| | | 400 | | } |
| | | 401 | | |
| | 6 | 402 | | if (_disposed) |
| | | 403 | | { |
| | 0 | 404 | | ThrowDisposed(); |
| | | 405 | | } |
| | | 406 | | |
| | 6 | 407 | | if (properties._properties != null) |
| | | 408 | | { |
| | 0 | 409 | | for (int i = 0; i < properties._properties.Length; i++) |
| | | 410 | | { |
| | 0 | 411 | | if (properties._properties[i].Name == null) |
| | | 412 | | { |
| | | 413 | | break; |
| | | 414 | | } |
| | | 415 | | |
| | 0 | 416 | | Property property = properties._properties[i]; |
| | | 417 | | |
| | | 418 | | // this[string] will call CreateCopyOfPropertyValue, so we don't need to repeat that here |
| | 0 | 419 | | this[property.Name] = property.Value; |
| | | 420 | | } |
| | | 421 | | } |
| | | 422 | | |
| | 6 | 423 | | Via = properties.Via; |
| | 6 | 424 | | AllowOutputBatching = properties.AllowOutputBatching; |
| | 6 | 425 | | Security = (properties.Security != null) ? (SecurityMessageProperty)properties.Security.CreateCopy() : null; |
| | 6 | 426 | | Encoder = properties.Encoder; |
| | 6 | 427 | | } |
| | | 428 | | |
| | | 429 | | internal void MergeProperties(MessageProperties properties) |
| | | 430 | | { |
| | | 431 | | // MergeProperties behavior should be equivalent to the behavior |
| | | 432 | | // of CopyProperties except that Merge supports property values that |
| | | 433 | | // implement the IMergeEnabledMessageProperty. Any changes to CopyProperties |
| | | 434 | | // should be reflected in MergeProperties as well. |
| | 35 | 435 | | if (properties == null) |
| | | 436 | | { |
| | 0 | 437 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(properties)); |
| | | 438 | | } |
| | | 439 | | |
| | 35 | 440 | | if (_disposed) |
| | | 441 | | { |
| | 0 | 442 | | ThrowDisposed(); |
| | | 443 | | } |
| | | 444 | | |
| | 35 | 445 | | if (properties._properties != null) |
| | | 446 | | { |
| | 136 | 447 | | for (int i = 0; i < properties._properties.Length; i++) |
| | | 448 | | { |
| | 68 | 449 | | if (properties._properties[i].Name == null) |
| | | 450 | | { |
| | | 451 | | break; |
| | | 452 | | } |
| | | 453 | | |
| | 34 | 454 | | Property property = properties._properties[i]; |
| | | 455 | | |
| | | 456 | | // Used with Http transport |
| | | 457 | | //IMergeEnabledMessageProperty currentValue; |
| | | 458 | | //if (!this.TryGetValue<IMergeEnabledMessageProperty>(property.Name, out currentValue) || |
| | | 459 | | // !currentValue.TryMergeWithProperty(property.Value)) |
| | | 460 | | //{ |
| | | 461 | | // Merge wasn't possible so copy |
| | | 462 | | // this[string] will call CreateCopyOfPropertyValue, so we don't need to repeat that here |
| | 34 | 463 | | this[property.Name] = property.Value; |
| | | 464 | | //} |
| | | 465 | | } |
| | | 466 | | } |
| | | 467 | | |
| | 35 | 468 | | Via = properties.Via; |
| | 35 | 469 | | AllowOutputBatching = properties.AllowOutputBatching; |
| | 35 | 470 | | Security = (properties.Security != null) ? (SecurityMessageProperty)properties.Security.CreateCopy() : null; |
| | 35 | 471 | | Encoder = properties.Encoder; |
| | 35 | 472 | | } |
| | | 473 | | |
| | | 474 | | internal void CopyProperties(KeyValuePair<string, object>[] array) |
| | | 475 | | { |
| | 624 | 476 | | if (_disposed) |
| | | 477 | | { |
| | 0 | 478 | | ThrowDisposed(); |
| | | 479 | | } |
| | | 480 | | |
| | 1606 | 481 | | for (int i = 0; i < array.Length; i++) |
| | | 482 | | { |
| | 179 | 483 | | KeyValuePair<string, object> property = array[i]; |
| | | 484 | | |
| | | 485 | | // this[string] will call CreateCopyOfPropertyValue, so we don't need to repeat that here |
| | 179 | 486 | | this[property.Key] = property.Value; |
| | | 487 | | } |
| | 624 | 488 | | } |
| | | 489 | | |
| | | 490 | | public bool ContainsKey(string name) |
| | | 491 | | { |
| | 206 | 492 | | if (_disposed) |
| | | 493 | | { |
| | 0 | 494 | | ThrowDisposed(); |
| | | 495 | | } |
| | | 496 | | |
| | 206 | 497 | | if (name == null) |
| | | 498 | | { |
| | 0 | 499 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(name)); |
| | | 500 | | } |
| | | 501 | | |
| | 206 | 502 | | int index = FindProperty(name); |
| | | 503 | | switch (index) |
| | | 504 | | { |
| | | 505 | | case ViaIndex: |
| | 0 | 506 | | return (object)_via != null; |
| | | 507 | | case AllowOutputBatchingIndex: |
| | 0 | 508 | | return (object)_allowOutputBatching != null; |
| | | 509 | | case SecurityIndex: |
| | 0 | 510 | | return (object)_security != null; |
| | | 511 | | case EncoderIndex: |
| | 0 | 512 | | return (object)_encoder != null; |
| | | 513 | | case NotFoundIndex: |
| | 95 | 514 | | return false; |
| | | 515 | | default: |
| | 111 | 516 | | return true; |
| | | 517 | | } |
| | | 518 | | } |
| | | 519 | | |
| | | 520 | | private object CreateCopyOfPropertyValue(object propertyValue) |
| | | 521 | | { |
| | 4827 | 522 | | if (!(propertyValue is IMessageProperty messageProperty)) |
| | | 523 | | { |
| | 3962 | 524 | | return propertyValue; |
| | | 525 | | } |
| | | 526 | | |
| | 865 | 527 | | object copy = messageProperty.CreateCopy(); |
| | 865 | 528 | | if (copy == null) |
| | | 529 | | { |
| | 0 | 530 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.MessagePropertyReturn |
| | | 531 | | } |
| | | 532 | | |
| | 865 | 533 | | return copy; |
| | | 534 | | } |
| | | 535 | | |
| | | 536 | | public void Dispose() |
| | | 537 | | { |
| | 3260 | 538 | | if (_disposed) |
| | | 539 | | { |
| | 0 | 540 | | return; |
| | | 541 | | } |
| | | 542 | | |
| | 3260 | 543 | | _disposed = true; |
| | | 544 | | |
| | 3260 | 545 | | if (_properties != null) |
| | | 546 | | { |
| | 13492 | 547 | | for (int i = 0; i < _properties.Length; i++) |
| | | 548 | | { |
| | 5974 | 549 | | if (_properties[i].Name == null) |
| | | 550 | | { |
| | | 551 | | break; |
| | | 552 | | } |
| | | 553 | | |
| | 4300 | 554 | | _properties[i].Dispose(); |
| | | 555 | | } |
| | | 556 | | } |
| | | 557 | | |
| | 3260 | 558 | | if (_security != null) |
| | | 559 | | { |
| | 107 | 560 | | _security.Dispose(); |
| | | 561 | | } |
| | 3260 | 562 | | } |
| | | 563 | | |
| | | 564 | | private int FindProperty(string name) |
| | | 565 | | { |
| | 9015 | 566 | | if (name == ViaKey) |
| | | 567 | | { |
| | 0 | 568 | | return ViaIndex; |
| | | 569 | | } |
| | 9015 | 570 | | else if (name == AllowOutputBatchingKey) |
| | | 571 | | { |
| | 0 | 572 | | return AllowOutputBatchingIndex; |
| | | 573 | | } |
| | 9015 | 574 | | else if (name == EncoderKey) |
| | | 575 | | { |
| | 178 | 576 | | return EncoderIndex; |
| | | 577 | | } |
| | 8837 | 578 | | else if (name == SecurityKey) |
| | | 579 | | { |
| | 0 | 580 | | return SecurityIndex; |
| | | 581 | | } |
| | | 582 | | |
| | 8837 | 583 | | if (_properties != null) |
| | | 584 | | { |
| | 24452 | 585 | | for (int i = 0; i < _properties.Length; i++) |
| | | 586 | | { |
| | 11769 | 587 | | string propertyName = _properties[i].Name; |
| | | 588 | | |
| | 11769 | 589 | | if (propertyName == null) |
| | | 590 | | { |
| | | 591 | | break; |
| | | 592 | | } |
| | | 593 | | |
| | 8940 | 594 | | if (propertyName == name) |
| | | 595 | | { |
| | 2355 | 596 | | return i; |
| | | 597 | | } |
| | | 598 | | } |
| | | 599 | | } |
| | | 600 | | |
| | 6482 | 601 | | return NotFoundIndex; |
| | | 602 | | } |
| | | 603 | | |
| | | 604 | | internal void Recycle() |
| | | 605 | | { |
| | 621 | 606 | | _disposed = false; |
| | 621 | 607 | | Clear(); |
| | 621 | 608 | | } |
| | | 609 | | |
| | | 610 | | public bool Remove(string name) |
| | | 611 | | { |
| | 20 | 612 | | if (_disposed) |
| | | 613 | | { |
| | 0 | 614 | | ThrowDisposed(); |
| | | 615 | | } |
| | | 616 | | |
| | 20 | 617 | | int originalPropertyCount = _propertyCount; |
| | 20 | 618 | | UpdateProperty(name, null, false); |
| | 20 | 619 | | return originalPropertyCount != _propertyCount; |
| | | 620 | | } |
| | | 621 | | |
| | | 622 | | public bool TryGetValue(string name, out object value) |
| | | 623 | | { |
| | 3785 | 624 | | if (_disposed) |
| | | 625 | | { |
| | 0 | 626 | | ThrowDisposed(); |
| | | 627 | | } |
| | | 628 | | |
| | 3785 | 629 | | if (name == null) |
| | | 630 | | { |
| | 0 | 631 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(name)); |
| | | 632 | | } |
| | | 633 | | |
| | 3785 | 634 | | int index = FindProperty(name); |
| | | 635 | | switch (index) |
| | | 636 | | { |
| | | 637 | | case ViaIndex: |
| | 0 | 638 | | value = _via; |
| | 0 | 639 | | break; |
| | | 640 | | case AllowOutputBatchingIndex: |
| | 0 | 641 | | value = _allowOutputBatching; |
| | 0 | 642 | | break; |
| | | 643 | | case SecurityIndex: |
| | 0 | 644 | | value = _security; |
| | 0 | 645 | | break; |
| | | 646 | | case EncoderIndex: |
| | 0 | 647 | | value = _encoder; |
| | 0 | 648 | | break; |
| | | 649 | | case NotFoundIndex: |
| | 1541 | 650 | | value = null; |
| | 1541 | 651 | | break; |
| | | 652 | | default: |
| | 2244 | 653 | | value = _properties[index].Value; |
| | | 654 | | break; |
| | | 655 | | } |
| | | 656 | | |
| | 3785 | 657 | | return value != null; |
| | | 658 | | } |
| | | 659 | | |
| | | 660 | | public bool TryGetValue<TProperty>(string name, out TProperty property) |
| | | 661 | | { |
| | 14 | 662 | | if (TryGetValue(name, out object o)) |
| | | 663 | | { |
| | 14 | 664 | | if (!(o is TProperty)) |
| | | 665 | | { |
| | 0 | 666 | | property = default; |
| | 0 | 667 | | return false; |
| | | 668 | | } |
| | | 669 | | |
| | 14 | 670 | | property = (TProperty)o; |
| | 14 | 671 | | return true; |
| | | 672 | | } |
| | | 673 | | else |
| | | 674 | | { |
| | 0 | 675 | | property = default; |
| | 0 | 676 | | return false; |
| | | 677 | | } |
| | | 678 | | } |
| | | 679 | | |
| | | 680 | | internal TProperty GetValue<TProperty>(string name) where TProperty : class |
| | | 681 | | { |
| | 0 | 682 | | return GetValue<TProperty>(name, false); |
| | | 683 | | } |
| | | 684 | | |
| | | 685 | | internal TProperty GetValue<TProperty>(string name, bool ensureTypeMatch) where TProperty : class |
| | | 686 | | { |
| | 0 | 687 | | if (!TryGetValue(name, out object obj)) |
| | | 688 | | { |
| | 0 | 689 | | return null; |
| | | 690 | | } |
| | | 691 | | |
| | 0 | 692 | | return ensureTypeMatch ? (TProperty)obj : obj as TProperty; |
| | | 693 | | } |
| | | 694 | | |
| | | 695 | | private void UpdateProperty(string name, object value, bool mustNotExist) |
| | | 696 | | { |
| | 5024 | 697 | | if (name == null) |
| | | 698 | | { |
| | 0 | 699 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(name)); |
| | | 700 | | } |
| | | 701 | | |
| | 5024 | 702 | | int index = FindProperty(name); |
| | 5024 | 703 | | if (index != NotFoundIndex) |
| | | 704 | | { |
| | 178 | 705 | | if (mustNotExist) |
| | | 706 | | { |
| | | 707 | | bool exists; |
| | | 708 | | switch (index) |
| | | 709 | | { |
| | | 710 | | case ViaIndex: |
| | 0 | 711 | | exists = (object)_via != null; |
| | 0 | 712 | | break; |
| | | 713 | | case AllowOutputBatchingIndex: |
| | 0 | 714 | | exists = (object)_allowOutputBatching != null; |
| | 0 | 715 | | break; |
| | | 716 | | case SecurityIndex: |
| | 0 | 717 | | exists = (object)_security != null; |
| | 0 | 718 | | break; |
| | | 719 | | case EncoderIndex: |
| | 0 | 720 | | exists = (object)_encoder != null; |
| | 0 | 721 | | break; |
| | | 722 | | default: |
| | 0 | 723 | | exists = true; |
| | | 724 | | break; |
| | | 725 | | } |
| | 0 | 726 | | if (exists) |
| | | 727 | | { |
| | 0 | 728 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.Dup |
| | | 729 | | } |
| | | 730 | | } |
| | | 731 | | |
| | 178 | 732 | | if (index >= 0) |
| | | 733 | | { |
| | 0 | 734 | | if (value == null) |
| | | 735 | | { |
| | 0 | 736 | | _properties[index].Dispose(); |
| | | 737 | | int shiftIndex; |
| | 0 | 738 | | for (shiftIndex = index + 1; shiftIndex < _properties.Length; shiftIndex++) |
| | | 739 | | { |
| | 0 | 740 | | if (_properties[shiftIndex].Name == null) |
| | | 741 | | { |
| | | 742 | | break; |
| | | 743 | | } |
| | | 744 | | |
| | 0 | 745 | | _properties[shiftIndex - 1] = _properties[shiftIndex]; |
| | | 746 | | } |
| | 0 | 747 | | _properties[shiftIndex - 1] = new Property(); |
| | 0 | 748 | | _propertyCount--; |
| | | 749 | | } |
| | | 750 | | else |
| | | 751 | | { |
| | 0 | 752 | | _properties[index].Value = CreateCopyOfPropertyValue(value); |
| | | 753 | | } |
| | | 754 | | } |
| | | 755 | | else |
| | | 756 | | { |
| | | 757 | | switch (index) |
| | | 758 | | { |
| | | 759 | | case ViaIndex: |
| | 0 | 760 | | Via = (Uri)value; |
| | 0 | 761 | | break; |
| | | 762 | | case AllowOutputBatchingIndex: |
| | 0 | 763 | | AllowOutputBatching = (bool)value; |
| | 0 | 764 | | break; |
| | | 765 | | case SecurityIndex: |
| | 0 | 766 | | if (Security != null) |
| | 0 | 767 | | Security.Dispose(); |
| | 0 | 768 | | Security = (SecurityMessageProperty)CreateCopyOfPropertyValue(value); |
| | 0 | 769 | | break; |
| | | 770 | | case EncoderIndex: |
| | 178 | 771 | | Encoder = (MessageEncoder)value; |
| | 178 | 772 | | break; |
| | | 773 | | default: |
| | | 774 | | Fx.Assert(""); |
| | 0 | 775 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException()); |
| | | 776 | | } |
| | | 777 | | } |
| | | 778 | | } |
| | 4846 | 779 | | else if (value != null) |
| | | 780 | | { |
| | | 781 | | int newIndex; |
| | | 782 | | |
| | 4826 | 783 | | if (_properties == null) |
| | | 784 | | { |
| | 2454 | 785 | | _properties = new Property[InitialPropertyCount]; |
| | 2454 | 786 | | newIndex = 0; |
| | | 787 | | } |
| | | 788 | | else |
| | | 789 | | { |
| | 10946 | 790 | | for (newIndex = 0; newIndex < _properties.Length; newIndex++) |
| | | 791 | | { |
| | 5064 | 792 | | if (_properties[newIndex].Name == null) |
| | | 793 | | { |
| | | 794 | | break; |
| | | 795 | | } |
| | | 796 | | } |
| | | 797 | | |
| | 2372 | 798 | | if (newIndex == _properties.Length) |
| | | 799 | | { |
| | 409 | 800 | | Property[] newProperties = new Property[_properties.Length * 2]; |
| | 409 | 801 | | Array.Copy(_properties, newProperties, _properties.Length); |
| | 409 | 802 | | _properties = newProperties; |
| | | 803 | | } |
| | | 804 | | } |
| | | 805 | | |
| | 4826 | 806 | | object newValue = CreateCopyOfPropertyValue(value); |
| | 4826 | 807 | | _properties[newIndex] = new Property(name, newValue); |
| | 4826 | 808 | | _propertyCount++; |
| | | 809 | | } |
| | 4846 | 810 | | } |
| | | 811 | | |
| | | 812 | | void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int index) |
| | | 813 | | { |
| | 624 | 814 | | if (_disposed) |
| | | 815 | | { |
| | 0 | 816 | | ThrowDisposed(); |
| | | 817 | | } |
| | | 818 | | |
| | 624 | 819 | | if (array == null) |
| | | 820 | | { |
| | 0 | 821 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(array)); |
| | | 822 | | } |
| | | 823 | | |
| | 624 | 824 | | if (array.Length < _propertyCount) |
| | | 825 | | { |
| | 0 | 826 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.MessagePropertiesArra |
| | | 827 | | } |
| | | 828 | | |
| | 624 | 829 | | if (index < 0 || index > array.Length - _propertyCount) |
| | | 830 | | { |
| | 0 | 831 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(index), |
| | 0 | 832 | | SR.Format(SR.ValueMustBeInRange, 0, array.Length - _propertyCount))) |
| | | 833 | | } |
| | | 834 | | |
| | 624 | 835 | | if (_via != null) |
| | | 836 | | { |
| | 0 | 837 | | array[index++] = new KeyValuePair<string, object>(ViaKey, _via); |
| | | 838 | | } |
| | | 839 | | |
| | 624 | 840 | | if (_allowOutputBatching != null) |
| | | 841 | | { |
| | 0 | 842 | | array[index++] = new KeyValuePair<string, object>(AllowOutputBatchingKey, _allowOutputBatching); |
| | | 843 | | } |
| | | 844 | | |
| | 624 | 845 | | if (_security != null) |
| | 0 | 846 | | array[index++] = new KeyValuePair<string, object>(SecurityKey, _security.CreateCopy()); |
| | | 847 | | |
| | 624 | 848 | | if (_encoder != null) |
| | | 849 | | { |
| | 178 | 850 | | array[index++] = new KeyValuePair<string, object>(EncoderKey, _encoder); |
| | | 851 | | } |
| | | 852 | | |
| | 624 | 853 | | if (_properties != null) |
| | | 854 | | { |
| | 4 | 855 | | for (int i = 0; i < _properties.Length; i++) |
| | | 856 | | { |
| | 2 | 857 | | string propertyName = _properties[i].Name; |
| | | 858 | | |
| | 2 | 859 | | if (propertyName == null) |
| | | 860 | | { |
| | | 861 | | break; |
| | | 862 | | } |
| | | 863 | | |
| | 1 | 864 | | array[index++] = new KeyValuePair<string, object>(propertyName, CreateCopyOfPropertyValue(_propertie |
| | | 865 | | } |
| | | 866 | | } |
| | 624 | 867 | | } |
| | | 868 | | |
| | | 869 | | void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> pair) |
| | | 870 | | { |
| | 0 | 871 | | if (_disposed) |
| | | 872 | | { |
| | 0 | 873 | | ThrowDisposed(); |
| | | 874 | | } |
| | | 875 | | |
| | 0 | 876 | | if (pair.Value == null) |
| | | 877 | | { |
| | 0 | 878 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("pair.Value"); |
| | | 879 | | } |
| | | 880 | | |
| | 0 | 881 | | UpdateProperty(pair.Key, pair.Value, true); |
| | 0 | 882 | | } |
| | | 883 | | |
| | | 884 | | bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> pair) |
| | | 885 | | { |
| | 0 | 886 | | if (_disposed) |
| | | 887 | | { |
| | 0 | 888 | | ThrowDisposed(); |
| | | 889 | | } |
| | | 890 | | |
| | 0 | 891 | | if (pair.Value == null) |
| | | 892 | | { |
| | 0 | 893 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("pair.Value"); |
| | | 894 | | } |
| | | 895 | | |
| | 0 | 896 | | if (pair.Key == null) |
| | | 897 | | { |
| | 0 | 898 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("pair.Key"); |
| | | 899 | | } |
| | | 900 | | |
| | 0 | 901 | | if (!TryGetValue(pair.Key, out object value)) |
| | | 902 | | { |
| | 0 | 903 | | return false; |
| | | 904 | | } |
| | 0 | 905 | | return value.Equals(pair.Value); |
| | | 906 | | } |
| | | 907 | | |
| | | 908 | | IEnumerator IEnumerable.GetEnumerator() |
| | | 909 | | { |
| | 0 | 910 | | if (_disposed) |
| | | 911 | | { |
| | 0 | 912 | | ThrowDisposed(); |
| | | 913 | | } |
| | | 914 | | |
| | 0 | 915 | | return ((IEnumerable<KeyValuePair<string, object>>)this).GetEnumerator(); |
| | | 916 | | } |
| | | 917 | | |
| | | 918 | | IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator() |
| | | 919 | | { |
| | 0 | 920 | | if (_disposed) |
| | | 921 | | { |
| | 0 | 922 | | ThrowDisposed(); |
| | | 923 | | } |
| | | 924 | | |
| | 0 | 925 | | List<KeyValuePair<string, object>> pairs = new List<KeyValuePair<string, object>>(_propertyCount); |
| | | 926 | | |
| | 0 | 927 | | if (_via != null) |
| | | 928 | | { |
| | 0 | 929 | | pairs.Add(new KeyValuePair<string, object>(ViaKey, _via)); |
| | | 930 | | } |
| | | 931 | | |
| | 0 | 932 | | if (_allowOutputBatching != null) |
| | | 933 | | { |
| | 0 | 934 | | pairs.Add(new KeyValuePair<string, object>(AllowOutputBatchingKey, _allowOutputBatching)); |
| | | 935 | | } |
| | | 936 | | |
| | 0 | 937 | | if (_security != null) |
| | 0 | 938 | | pairs.Add(new KeyValuePair<string, object>(SecurityKey, _security)); |
| | | 939 | | |
| | 0 | 940 | | if (_encoder != null) |
| | | 941 | | { |
| | 0 | 942 | | pairs.Add(new KeyValuePair<string, object>(EncoderKey, _encoder)); |
| | | 943 | | } |
| | | 944 | | |
| | 0 | 945 | | if (_properties != null) |
| | | 946 | | { |
| | 0 | 947 | | for (int i = 0; i < _properties.Length; i++) |
| | | 948 | | { |
| | 0 | 949 | | string propertyName = _properties[i].Name; |
| | | 950 | | |
| | 0 | 951 | | if (propertyName == null) |
| | | 952 | | { |
| | | 953 | | break; |
| | | 954 | | } |
| | | 955 | | |
| | 0 | 956 | | pairs.Add(new KeyValuePair<string, object>(propertyName, _properties[i].Value)); |
| | | 957 | | } |
| | | 958 | | } |
| | | 959 | | |
| | 0 | 960 | | return pairs.GetEnumerator(); |
| | | 961 | | } |
| | | 962 | | |
| | | 963 | | bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> pair) |
| | | 964 | | { |
| | 0 | 965 | | if (_disposed) |
| | | 966 | | { |
| | 0 | 967 | | ThrowDisposed(); |
| | | 968 | | } |
| | | 969 | | |
| | 0 | 970 | | if (pair.Value == null) |
| | | 971 | | { |
| | 0 | 972 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("pair.Value"); |
| | | 973 | | } |
| | | 974 | | |
| | 0 | 975 | | if (pair.Key == null) |
| | | 976 | | { |
| | 0 | 977 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("pair.Key"); |
| | | 978 | | } |
| | | 979 | | |
| | 0 | 980 | | if (!TryGetValue(pair.Key, out object value)) |
| | | 981 | | { |
| | 0 | 982 | | return false; |
| | | 983 | | } |
| | 0 | 984 | | if (!value.Equals(pair.Value)) |
| | | 985 | | { |
| | 0 | 986 | | return false; |
| | | 987 | | } |
| | 0 | 988 | | Remove(pair.Key); |
| | 0 | 989 | | return true; |
| | | 990 | | } |
| | | 991 | | |
| | | 992 | | private struct Property : IDisposable |
| | | 993 | | { |
| | | 994 | | public Property(string name, object value) |
| | | 995 | | { |
| | 4826 | 996 | | Name = name; |
| | 4826 | 997 | | Value = value; |
| | 4826 | 998 | | } |
| | | 999 | | |
| | 25203 | 1000 | | public string Name { get; } |
| | | 1001 | | |
| | 11405 | 1002 | | public object Value { get; set; } |
| | | 1003 | | |
| | | 1004 | | public void Dispose() |
| | | 1005 | | { |
| | 4300 | 1006 | | if (Value is IDisposable disposable) |
| | | 1007 | | { |
| | 0 | 1008 | | disposable.Dispose(); |
| | | 1009 | | } |
| | 4300 | 1010 | | } |
| | | 1011 | | } |
| | | 1012 | | } |
| | | 1013 | | } |