OperationContext.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // OperationContext.cs
  3. //
  4. // Author: Atsushi Enomoto ([email protected])
  5. //
  6. // Copyright (C) 2005,2007 Novell, Inc (http://www.novell.com)
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Collections.ObjectModel;
  30. using System.ServiceModel.Channels;
  31. using System.ServiceModel.Dispatcher;
  32. using System.ServiceModel.Security;
  33. using System.Threading;
  34. namespace System.ServiceModel
  35. {
  36. public sealed class OperationContext : IExtensibleObject<OperationContext>
  37. {
  38. // generated guid (no special meaning)
  39. const string operation_context_name = "c15795e2-bb44-4cfb-a89c-8529feb170cb";
  40. Message incoming_message;
  41. IDefaultCommunicationTimeouts timeouts;
  42. public static OperationContext Current {
  43. get { return Thread.GetData (Thread.GetNamedDataSlot (operation_context_name)) as OperationContext; }
  44. set { Thread.SetData (Thread.GetNamedDataSlot (operation_context_name), value); }
  45. }
  46. #if !NET_2_1
  47. EndpointDispatcher dispatcher;
  48. #endif
  49. IContextChannel channel;
  50. RequestContext request_ctx;
  51. ExtensionCollection<OperationContext> extensions;
  52. MessageHeaders outgoing_headers;
  53. MessageProperties outgoing_properties;
  54. InstanceContext instance_context;
  55. public OperationContext (IContextChannel channel)
  56. {
  57. if (channel == null)
  58. throw new ArgumentNullException ("channel");
  59. this.channel = channel;
  60. }
  61. public event EventHandler OperationCompleted;
  62. public IContextChannel Channel {
  63. get { return channel; }
  64. }
  65. public IExtensionCollection<OperationContext> Extensions {
  66. get {
  67. if (extensions == null)
  68. extensions = new ExtensionCollection<OperationContext> (this);
  69. return extensions;
  70. }
  71. }
  72. #if !NET_2_1
  73. public EndpointDispatcher EndpointDispatcher {
  74. get { return dispatcher; }
  75. set { dispatcher = value; }
  76. }
  77. public bool HasSupportingTokens {
  78. get { return SupportingTokens != null ? SupportingTokens.Count > 0 : false; }
  79. }
  80. public ServiceHostBase Host {
  81. get { return dispatcher != null ? dispatcher.ChannelDispatcher.Host : null; }
  82. }
  83. #endif
  84. public MessageHeaders IncomingMessageHeaders {
  85. get { return request_ctx != null ? request_ctx.RequestMessage.Headers : null; }
  86. }
  87. public MessageProperties IncomingMessageProperties {
  88. get { return request_ctx != null ? request_ctx.RequestMessage.Properties : null; }
  89. }
  90. public MessageVersion IncomingMessageVersion {
  91. get { return request_ctx != null ? request_ctx.RequestMessage.Version : null; }
  92. }
  93. [MonoTODO]
  94. public InstanceContext InstanceContext {
  95. get {
  96. return instance_context;
  97. }
  98. internal set {
  99. instance_context = value;
  100. }
  101. }
  102. [MonoTODO]
  103. public bool IsUserContext {
  104. get { throw new NotImplementedException (); }
  105. }
  106. public MessageHeaders OutgoingMessageHeaders {
  107. get {
  108. if (outgoing_headers == null)
  109. outgoing_headers = new MessageHeaders (MessageVersion.Default);
  110. return outgoing_headers;
  111. }
  112. }
  113. public MessageProperties OutgoingMessageProperties {
  114. get {
  115. if (outgoing_properties == null)
  116. outgoing_properties = new MessageProperties ();
  117. return outgoing_properties;
  118. }
  119. }
  120. public RequestContext RequestContext {
  121. get { return request_ctx; }
  122. set { request_ctx = value; }
  123. }
  124. [MonoTODO]
  125. public string SessionId {
  126. get { throw new NotImplementedException (); }
  127. }
  128. #if !NET_2_1
  129. public ServiceSecurityContext ServiceSecurityContext {
  130. get { return IncomingMessageProperties != null ? IncomingMessageProperties.Security.ServiceSecurityContext : null; }
  131. }
  132. public ICollection<SupportingTokenSpecification> SupportingTokens {
  133. get { return IncomingMessageProperties != null ? IncomingMessageProperties.Security.IncomingSupportingTokens : null; }
  134. }
  135. public T GetCallbackChannel<T> ()
  136. {
  137. if (!(channel is IDuplexContextChannel))
  138. return default (T);
  139. IDuplexContextChannel duplex = (IDuplexContextChannel) channel;
  140. foreach (IChannel ch in duplex.CallbackInstance.IncomingChannels)
  141. if (typeof (T).IsAssignableFrom (ch.GetType ()))
  142. return (T) (object) ch;
  143. foreach (IChannel ch in duplex.CallbackInstance.OutgoingChannels)
  144. if (typeof (T).IsAssignableFrom (ch.GetType ()))
  145. return (T) (object) ch;
  146. return default (T);
  147. }
  148. [MonoTODO]
  149. public void SetTransactionComplete ()
  150. {
  151. throw new NotImplementedException ();
  152. }
  153. #endif
  154. internal Message IncomingMessage {
  155. get {
  156. return incoming_message;
  157. }
  158. set {
  159. incoming_message = value;
  160. }
  161. }
  162. internal IDefaultCommunicationTimeouts CommunicationTimeouts
  163. {
  164. get {
  165. return timeouts;
  166. }
  167. set {
  168. timeouts = value;
  169. }
  170. }
  171. }
  172. }