| | | 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.Security.Claims; |
| | | 6 | | using System.Security.Principal; |
| | | 7 | | using System.Threading; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | using CoreWCF.Dispatcher; |
| | | 10 | | using CoreWCF.Runtime; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF |
| | | 13 | | { |
| | | 14 | | public sealed class OperationContext : IExtensibleObject<OperationContext> |
| | | 15 | | { |
| | 8 | 16 | | private static readonly AsyncLocal<OperationContext> s_currentContext = new AsyncLocal<OperationContext>(); |
| | | 17 | | private Message _clientReply; |
| | | 18 | | private bool _closeClientReply; |
| | | 19 | | private ExtensionCollection<OperationContext> _extensions; |
| | | 20 | | private Message _request; |
| | | 21 | | internal IPrincipal _threadPrincipal; |
| | | 22 | | private MessageProperties _outgoingMessageProperties; |
| | | 23 | | private MessageHeaders _outgoingMessageHeaders; |
| | | 24 | | |
| | | 25 | | public event EventHandler OperationCompleted; |
| | | 26 | | |
| | 0 | 27 | | public OperationContext(IContextChannel channel) |
| | | 28 | | { |
| | 0 | 29 | | if (channel == null) |
| | | 30 | | { |
| | 0 | 31 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(channel)); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | |
| | | 35 | | //Could be a TransparentProxy |
| | 0 | 36 | | if (!(channel is ServiceChannel serviceChannel)) |
| | | 37 | | { |
| | 0 | 38 | | serviceChannel = ServiceChannelFactory.GetServiceChannel(channel); |
| | | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | if (serviceChannel != null) |
| | | 42 | | { |
| | 0 | 43 | | OutgoingMessageVersion = serviceChannel.MessageVersion; |
| | 0 | 44 | | InternalServiceChannel = serviceChannel; |
| | | 45 | | } |
| | | 46 | | else |
| | | 47 | | { |
| | 0 | 48 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SFxInvalidCha |
| | | 49 | | } |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | internal OperationContext(ServiceHostBase host) |
| | 0 | 53 | | : this(host, MessageVersion.Soap12WSAddressing10) |
| | | 54 | | { |
| | 0 | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | internal OperationContext(ServiceHostBase host, MessageVersion outgoingMessageVersion) |
| | | 58 | | { |
| | 0 | 59 | | Host = host; |
| | 0 | 60 | | OutgoingMessageVersion = outgoingMessageVersion ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgum |
| | 0 | 61 | | } |
| | | 62 | | |
| | 2538 | 63 | | internal OperationContext(RequestContext requestContext, Message request, ServiceChannel channel, ServiceHostBas |
| | | 64 | | { |
| | 2538 | 65 | | InternalServiceChannel = channel; |
| | 2538 | 66 | | Host = host; |
| | 2538 | 67 | | RequestContext = requestContext; |
| | 2538 | 68 | | _request = request; |
| | 2538 | 69 | | OutgoingMessageVersion = channel.MessageVersion; |
| | 2538 | 70 | | } |
| | | 71 | | |
| | | 72 | | // TODO: Probably want to revert this to public |
| | | 73 | | internal IContextChannel Channel |
| | | 74 | | { |
| | 0 | 75 | | get { return GetCallbackChannel<IContextChannel>(); } |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | public static OperationContext Current |
| | | 79 | | { |
| | | 80 | | get |
| | | 81 | | { |
| | 5492 | 82 | | return s_currentContext.Value; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | set |
| | | 86 | | { |
| | 5076 | 87 | | s_currentContext.Value = value; |
| | 5076 | 88 | | } |
| | | 89 | | } |
| | | 90 | | |
| | 6 | 91 | | public ServiceHostBase Host { get; } |
| | | 92 | | |
| | 5076 | 93 | | public EndpointDispatcher EndpointDispatcher { get; set; } |
| | | 94 | | public bool IsUserContext |
| | | 95 | | { |
| | | 96 | | get |
| | | 97 | | { |
| | 2 | 98 | | return (_request == null); |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | public IExtensionCollection<OperationContext> Extensions |
| | | 103 | | { |
| | | 104 | | get |
| | | 105 | | { |
| | 124 | 106 | | if (_extensions == null) |
| | | 107 | | { |
| | 34 | 108 | | _extensions = new ExtensionCollection<OperationContext>(this); |
| | | 109 | | } |
| | 124 | 110 | | return _extensions; |
| | | 111 | | } |
| | | 112 | | } |
| | | 113 | | |
| | 2 | 114 | | internal bool IsServiceReentrant { get; set; } = false; |
| | | 115 | | |
| | | 116 | | internal Message IncomingMessage |
| | | 117 | | { |
| | 2391 | 118 | | get { return _clientReply ?? _request; } |
| | | 119 | | } |
| | | 120 | | |
| | 8051 | 121 | | internal ServiceChannel InternalServiceChannel { get; set; } |
| | | 122 | | |
| | | 123 | | internal bool HasOutgoingMessageHeaders |
| | | 124 | | { |
| | 729 | 125 | | get { return (_outgoingMessageHeaders != null); } |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | public MessageHeaders OutgoingMessageHeaders |
| | | 129 | | { |
| | | 130 | | get |
| | | 131 | | { |
| | 0 | 132 | | if (_outgoingMessageHeaders == null) |
| | | 133 | | { |
| | 0 | 134 | | _outgoingMessageHeaders = new MessageHeaders(OutgoingMessageVersion); |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | return _outgoingMessageHeaders; |
| | | 138 | | } |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | internal bool HasOutgoingMessageProperties |
| | | 142 | | { |
| | 729 | 143 | | get { return (_outgoingMessageProperties != null); } |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | public MessageProperties OutgoingMessageProperties |
| | | 147 | | { |
| | | 148 | | get |
| | | 149 | | { |
| | 259 | 150 | | if (_outgoingMessageProperties == null) |
| | | 151 | | { |
| | 35 | 152 | | _outgoingMessageProperties = new MessageProperties(); |
| | | 153 | | } |
| | | 154 | | |
| | 259 | 155 | | return _outgoingMessageProperties; |
| | | 156 | | } |
| | | 157 | | } |
| | | 158 | | |
| | 1 | 159 | | internal MessageVersion OutgoingMessageVersion { get; } |
| | | 160 | | |
| | | 161 | | public MessageHeaders IncomingMessageHeaders |
| | | 162 | | { |
| | | 163 | | get |
| | | 164 | | { |
| | 4 | 165 | | Message message = _clientReply ?? _request; |
| | 4 | 166 | | if (message != null) |
| | | 167 | | { |
| | 4 | 168 | | return message.Headers; |
| | | 169 | | } |
| | | 170 | | else |
| | | 171 | | { |
| | 0 | 172 | | return null; |
| | | 173 | | } |
| | | 174 | | } |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | public MessageProperties IncomingMessageProperties |
| | | 178 | | { |
| | | 179 | | get |
| | | 180 | | { |
| | 134 | 181 | | Message message = _clientReply ?? _request; |
| | 134 | 182 | | if (message != null) |
| | | 183 | | { |
| | 134 | 184 | | return message.Properties; |
| | | 185 | | } |
| | | 186 | | else |
| | | 187 | | { |
| | 0 | 188 | | return null; |
| | | 189 | | } |
| | | 190 | | } |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | public MessageVersion IncomingMessageVersion |
| | | 194 | | { |
| | | 195 | | get |
| | | 196 | | { |
| | 0 | 197 | | Message message = _clientReply ?? _request; |
| | 0 | 198 | | if (message != null) |
| | | 199 | | { |
| | 0 | 200 | | return message.Version; |
| | | 201 | | } |
| | | 202 | | else |
| | | 203 | | { |
| | 0 | 204 | | return null; |
| | | 205 | | } |
| | | 206 | | } |
| | | 207 | | } |
| | | 208 | | |
| | 2641 | 209 | | public InstanceContext InstanceContext { get; private set; } |
| | | 210 | | |
| | 12703 | 211 | | public RequestContext RequestContext { get; set; } |
| | | 212 | | |
| | | 213 | | public ServiceSecurityContext ServiceSecurityContext |
| | | 214 | | { |
| | | 215 | | get |
| | | 216 | | { |
| | 25 | 217 | | MessageProperties properties = IncomingMessageProperties; |
| | 25 | 218 | | if (properties != null && properties.Security != null) |
| | | 219 | | { |
| | 25 | 220 | | return properties.Security.ServiceSecurityContext; |
| | | 221 | | } |
| | 0 | 222 | | return null; |
| | | 223 | | } |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | public string SessionId |
| | | 227 | | { |
| | | 228 | | get |
| | | 229 | | { |
| | 0 | 230 | | if (InternalServiceChannel != null) |
| | | 231 | | { |
| | 0 | 232 | | IChannel inner = InternalServiceChannel.InnerChannel; |
| | 0 | 233 | | if (inner != null) |
| | | 234 | | { |
| | 0 | 235 | | if ((inner is ISessionChannel<IDuplexSession> duplex) && (duplex.Session != null)) |
| | 0 | 236 | | return duplex.Session.Id; |
| | | 237 | | |
| | 0 | 238 | | if ((inner is ISessionChannel<IInputSession> input) && (input.Session != null)) |
| | 0 | 239 | | return input.Session.Id; |
| | | 240 | | |
| | 0 | 241 | | if ((inner is ISessionChannel<IOutputSession> output) && (output.Session != null)) |
| | 0 | 242 | | return output.Session.Id; |
| | | 243 | | } |
| | | 244 | | } |
| | 0 | 245 | | return null; |
| | | 246 | | } |
| | | 247 | | } |
| | | 248 | | |
| | | 249 | | internal IPrincipal ThreadPrincipal |
| | | 250 | | { |
| | 0 | 251 | | get { return _threadPrincipal; } |
| | 0 | 252 | | set { _threadPrincipal = value; } |
| | | 253 | | } |
| | | 254 | | |
| | | 255 | | public ClaimsPrincipal ClaimsPrincipal |
| | | 256 | | { |
| | 134 | 257 | | get; |
| | 80 | 258 | | internal set; |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | internal void ClearClientReplyNoThrow() |
| | | 262 | | { |
| | 2538 | 263 | | _clientReply = null; |
| | 2538 | 264 | | } |
| | | 265 | | |
| | | 266 | | internal void FireOperationCompleted() |
| | | 267 | | { |
| | | 268 | | try |
| | | 269 | | { |
| | 2538 | 270 | | EventHandler handler = OperationCompleted; |
| | | 271 | | |
| | 2538 | 272 | | if (handler != null) |
| | | 273 | | { |
| | 0 | 274 | | handler(this, EventArgs.Empty); |
| | | 275 | | } |
| | 2538 | 276 | | } |
| | 0 | 277 | | catch (Exception e) |
| | | 278 | | { |
| | 0 | 279 | | if (Fx.IsFatal(e)) |
| | | 280 | | { |
| | 0 | 281 | | throw; |
| | | 282 | | } |
| | | 283 | | |
| | 0 | 284 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e); |
| | | 285 | | } |
| | 2538 | 286 | | } |
| | | 287 | | |
| | | 288 | | public T GetCallbackChannel<T>() |
| | | 289 | | { |
| | 1 | 290 | | if (InternalServiceChannel == null || IsUserContext) |
| | | 291 | | { |
| | 0 | 292 | | return default; |
| | | 293 | | } |
| | | 294 | | |
| | | 295 | | // yes, we might throw InvalidCastException here. Is it really |
| | | 296 | | // better to check and throw something else instead? |
| | 1 | 297 | | return (T)InternalServiceChannel.Proxy; |
| | | 298 | | } |
| | | 299 | | |
| | | 300 | | internal void ReInit(RequestContext requestContext, Message request, ServiceChannel channel) |
| | | 301 | | { |
| | 0 | 302 | | RequestContext = requestContext; |
| | 0 | 303 | | _request = request; |
| | 0 | 304 | | InternalServiceChannel = channel; |
| | 0 | 305 | | } |
| | | 306 | | |
| | | 307 | | internal void Recycle() |
| | | 308 | | { |
| | 0 | 309 | | RequestContext = null; |
| | 0 | 310 | | _request = null; |
| | 0 | 311 | | _extensions = null; |
| | 0 | 312 | | InstanceContext = null; |
| | 0 | 313 | | _threadPrincipal = null; |
| | 0 | 314 | | SetClientReply(null, false); |
| | 0 | 315 | | } |
| | | 316 | | |
| | | 317 | | internal void SetClientReply(Message message, bool closeMessage) |
| | | 318 | | { |
| | 2375 | 319 | | Message oldClientReply = null; |
| | | 320 | | |
| | 2375 | 321 | | if (!Equals(message, _clientReply)) |
| | | 322 | | { |
| | 0 | 323 | | if (_closeClientReply && (_clientReply != null)) |
| | | 324 | | { |
| | 0 | 325 | | oldClientReply = _clientReply; |
| | | 326 | | } |
| | | 327 | | |
| | 0 | 328 | | _clientReply = message; |
| | | 329 | | } |
| | | 330 | | |
| | 2375 | 331 | | _closeClientReply = closeMessage; |
| | | 332 | | |
| | 2375 | 333 | | if (oldClientReply != null) |
| | | 334 | | { |
| | 0 | 335 | | oldClientReply.Close(); |
| | | 336 | | } |
| | 2375 | 337 | | } |
| | | 338 | | |
| | | 339 | | internal void SetInstanceContext(InstanceContext instanceContext) |
| | | 340 | | { |
| | 2536 | 341 | | InstanceContext = instanceContext; |
| | 2536 | 342 | | } |
| | | 343 | | } |
| | | 344 | | } |