UriSchemeKeyedCollection.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Runtime.Serialization;
  9. public class UriSchemeKeyedCollection : SynchronizedKeyedCollection<string, Uri>
  10. {
  11. internal UriSchemeKeyedCollection(object syncRoot)
  12. : base(syncRoot)
  13. {
  14. }
  15. public UriSchemeKeyedCollection(params Uri[] addresses)
  16. {
  17. if (addresses == null)
  18. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("addresses");
  19. for (int i = 0; i < addresses.Length; i++)
  20. {
  21. this.Add(addresses[i]);
  22. }
  23. }
  24. protected override string GetKeyForItem(Uri item)
  25. {
  26. return item.Scheme;
  27. }
  28. protected override void InsertItem(int index, Uri item)
  29. {
  30. ValidateBaseAddress(item, "item");
  31. if (this.Contains(item.Scheme))
  32. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("item", SR.GetString(SR.BaseAddressDuplicateScheme, item.Scheme));
  33. base.InsertItem(index, item);
  34. }
  35. protected override void SetItem(int index, Uri item)
  36. {
  37. ValidateBaseAddress(item, "item");
  38. if (this[index].Scheme != item.Scheme)
  39. {
  40. if (this.Contains(item.Scheme))
  41. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("item", SR.GetString(SR.BaseAddressDuplicateScheme, item.Scheme));
  42. }
  43. base.SetItem(index, item);
  44. }
  45. internal static void ValidateBaseAddress(Uri uri, string argumentName)
  46. {
  47. if (uri == null)
  48. {
  49. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(argumentName);
  50. }
  51. if (!uri.IsAbsoluteUri)
  52. {
  53. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(argumentName, SR.GetString(SR.BaseAddressMustBeAbsolute));
  54. }
  55. if (!string.IsNullOrEmpty(uri.UserInfo))
  56. {
  57. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(argumentName, SR.GetString(SR.BaseAddressCannotHaveUserInfo));
  58. }
  59. if (!string.IsNullOrEmpty(uri.Query))
  60. {
  61. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(argumentName, SR.GetString(SR.BaseAddressCannotHaveQuery));
  62. }
  63. if (!string.IsNullOrEmpty(uri.Fragment))
  64. {
  65. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(argumentName, SR.GetString(SR.BaseAddressCannotHaveFragment));
  66. }
  67. }
  68. }
  69. }