ContextMessageProperty.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Runtime.CompilerServices;
  9. [TypeForwardedFrom("System.WorkflowServices, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
  10. [Serializable]
  11. public class ContextMessageProperty : IMessageProperty
  12. {
  13. internal const string InstanceIdKey = "instanceId";
  14. const string PropertyName = "ContextMessageProperty";
  15. static ContextMessageProperty empty;
  16. IDictionary<string, string> contextStore;
  17. public ContextMessageProperty()
  18. {
  19. this.contextStore = new ContextDictionary();
  20. }
  21. public ContextMessageProperty(IDictionary<string, string> context)
  22. {
  23. if (context == null)
  24. {
  25. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  26. }
  27. this.contextStore = new ContextDictionary(context);
  28. }
  29. public static string Name
  30. {
  31. get
  32. {
  33. return PropertyName;
  34. }
  35. }
  36. public IDictionary<string, string> Context
  37. {
  38. get
  39. {
  40. return this.contextStore;
  41. }
  42. }
  43. internal static ContextMessageProperty Empty
  44. {
  45. get
  46. {
  47. if (empty == null)
  48. {
  49. ContextMessageProperty context = new ContextMessageProperty();
  50. context.contextStore = ContextDictionary.Empty;
  51. empty = context;
  52. }
  53. return empty;
  54. }
  55. }
  56. public static bool TryCreateFromHttpCookieHeader(string httpCookieHeader, out ContextMessageProperty context)
  57. {
  58. return ContextProtocol.HttpCookieToolbox.TryCreateFromHttpCookieHeader(httpCookieHeader, out context);
  59. }
  60. public static bool TryGet(Message message, out ContextMessageProperty contextMessageProperty)
  61. {
  62. if (message == null)
  63. {
  64. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
  65. }
  66. return TryGet(message.Properties, out contextMessageProperty);
  67. }
  68. public static bool TryGet(MessageProperties properties, out ContextMessageProperty contextMessageProperty)
  69. {
  70. if (properties == null)
  71. {
  72. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("properties");
  73. }
  74. object value = null;
  75. if (properties.TryGetValue(PropertyName, out value))
  76. {
  77. contextMessageProperty = value as ContextMessageProperty;
  78. }
  79. else
  80. {
  81. contextMessageProperty = null;
  82. }
  83. return contextMessageProperty != null;
  84. }
  85. public void AddOrReplaceInMessage(Message message)
  86. {
  87. if (message == null)
  88. {
  89. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
  90. }
  91. this.AddOrReplaceInMessageProperties(message.Properties);
  92. }
  93. public void AddOrReplaceInMessageProperties(MessageProperties properties)
  94. {
  95. if (properties == null)
  96. {
  97. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("properties");
  98. }
  99. properties[PropertyName] = this;
  100. }
  101. public IMessageProperty CreateCopy()
  102. {
  103. return new ContextMessageProperty(this.Context);
  104. }
  105. }
  106. }