ConnectionPoolRegistry.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.Diagnostics;
  7. using System.ServiceModel;
  8. using System.Collections.Generic;
  9. using System.ServiceModel.Diagnostics;
  10. abstract class ConnectionPoolRegistry
  11. {
  12. Dictionary<string, List<ConnectionPool>> registry;
  13. protected ConnectionPoolRegistry()
  14. {
  15. registry = new Dictionary<string, List<ConnectionPool>>();
  16. }
  17. object ThisLock
  18. {
  19. get { return this.registry; }
  20. }
  21. // NOTE: performs the open on the pool for you
  22. public ConnectionPool Lookup(IConnectionOrientedTransportChannelFactorySettings settings)
  23. {
  24. ConnectionPool result = null;
  25. string key = settings.ConnectionPoolGroupName;
  26. lock (ThisLock)
  27. {
  28. List<ConnectionPool> registryEntry = null;
  29. if (registry.TryGetValue(key, out registryEntry))
  30. {
  31. for (int i = 0; i < registryEntry.Count; i++)
  32. {
  33. if (registryEntry[i].IsCompatible(settings) && registryEntry[i].TryOpen())
  34. {
  35. result = registryEntry[i];
  36. break;
  37. }
  38. }
  39. }
  40. else
  41. {
  42. registryEntry = new List<ConnectionPool>();
  43. registry.Add(key, registryEntry);
  44. }
  45. if (result == null)
  46. {
  47. result = CreatePool(settings);
  48. registryEntry.Add(result);
  49. }
  50. }
  51. return result;
  52. }
  53. protected abstract ConnectionPool CreatePool(IConnectionOrientedTransportChannelFactorySettings settings);
  54. public void Release(ConnectionPool pool, TimeSpan timeout)
  55. {
  56. lock (ThisLock)
  57. {
  58. if (pool.Close(timeout))
  59. {
  60. List<ConnectionPool> registryEntry = registry[pool.Name];
  61. for (int i = 0; i < registryEntry.Count; i++)
  62. {
  63. if (object.ReferenceEquals(registryEntry[i], pool))
  64. {
  65. registryEntry.RemoveAt(i);
  66. break;
  67. }
  68. }
  69. if (registryEntry.Count == 0)
  70. {
  71. registry.Remove(pool.Name);
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }