1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------
namespace System.ServiceModel.Routing
{
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime;
using System.ServiceModel;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;
using System.Collections.Generic;
using System.Threading;
abstract class RoutingChannelExtension : IExtension<IContextChannel>
{
static AsyncCallback closeChannelsCallback = Fx.ThunkCallback(CloseChannelsCallback);
static AsyncCallback shutdownCallback = Fx.ThunkCallback(ShutdownCallback);
IContextChannel channel;
bool hasSession;
RoutingBehavior.RoutingEndpointBehavior endpointBehavior;
RoutingService sessionService;
volatile SessionChannels sessionChannels;
[Fx.Tag.SynchronizationObject]
object thisLock;
public RoutingChannelExtension(RoutingBehavior.RoutingEndpointBehavior endpointBehavior)
{
this.ActivityID = Guid.NewGuid();
this.endpointBehavior = endpointBehavior;
this.thisLock = new object();
}
internal Guid ActivityID
{
get;
private set;
}
public string EndpointName
{
get { return this.endpointBehavior.EndpointName; }
}
public bool HasSession
{
get { return this.hasSession; }
}
public bool ImpersonationRequired
{
get { return this.endpointBehavior.ImpersonationRequired; }
}
public TimeSpan OperationTimeout
{
get;
private set;
}
public bool ReceiveContextEnabled
{
get { return this.endpointBehavior.ReceiveContextEnabled; }
}
[SuppressMessage(FxCop.Category.Performance, FxCop.Rule.AvoidUncalledPrivateCode, Justification = "get_SessionChannels is called by RoutingService..ctor")]
public SessionChannels SessionChannels
{
get
{
if (this.sessionChannels == null)
{
lock (this.thisLock)
{
if (this.sessionChannels == null)
{
Fx.AssertAndThrow(!(this.ImpersonationRequired && !this.HasSession), "Shouldn't allocate SessionChannels if session-less and impersonating");
this.sessionChannels = new SessionChannels(this.ActivityID);
}
}
}
return this.sessionChannels;
}
}
public bool TransactedReceiveEnabled
{
get { return this.endpointBehavior.TransactedReceiveEnabled; }
}
[SuppressMessage(FxCop.Category.Performance, FxCop.Rule.AvoidUncalledPrivateCode, Justification = "AttachService is called by RoutingService..ctor")]
public void AttachService(RoutingService service)
{
SessionChannels channelsToClose = null;
lock (this.thisLock)
{
if (!this.hasSession)
{
RoutingConfiguration oldConfig = null;
if (this.sessionService != null)
{
oldConfig = this.sessionService.RoutingConfig;
}
if (oldConfig != null && !object.ReferenceEquals(service.RoutingConfig, oldConfig))
{
//The RoutingConfiguration has changed. We need to release any old channels that are cached.
channelsToClose = this.sessionChannels;
this.sessionChannels = null;
}
}
else
{
Fx.Assert(this.sessionService == null, "There must only be one RoutingService created for a sessionful channel");
}
this.sessionService = service;
}
if (channelsToClose != null)
{
channelsToClose.BeginClose(this.channel.OperationTimeout, closeChannelsCallback, channelsToClose);
}
}
public abstract IAsyncResult BeginShutdown(RoutingService service, TimeSpan timeout, AsyncCallback callback, object state);
static void CloseChannelsCallback(IAsyncResult asyncResult)
{
SessionChannels channelsToClose = (SessionChannels)asyncResult.AsyncState;
Exception exception = null;
try
{
channelsToClose.EndClose(asyncResult);
}
catch (CommunicationException communicationException)
{
exception = communicationException;
}
catch (TimeoutException timeoutException)
{
exception = timeoutException;
}
if (exception != null && TD.RoutingServiceHandledExceptionIsEnabled())
{
TD.RoutingServiceHandledException(null, exception);
}
}
static void ShutdownCallback(IAsyncResult result)
{
if (result.CompletedSynchronously)
{
return;
}
RoutingChannelExtension thisPtr = (RoutingChannelExtension)result.AsyncState;
try
{
thisPtr.ShutdownComplete(result);
}
catch (Exception exception)
{
if (Fx.IsFatal(exception))
{
throw;
}
thisPtr.Fault(exception);
}
}
void ShutdownComplete(IAsyncResult result)
{
this.EndShutdown(result);
this.channel.Close();
}
public static RoutingChannelExtension Create(RoutingBehavior.RoutingEndpointBehavior endpointBehavior)
{
Type contractType = endpointBehavior.Endpoint.Contract.ContractType;
if (contractType == typeof(IDuplexSessionRouter))
{
return new RoutingChannelExtension<IDuplexSessionRouter>(endpointBehavior);
}
else if (contractType == typeof(ISimplexDatagramRouter))
{
return new RoutingChannelExtension<ISimplexDatagramRouter>(endpointBehavior);
}
else if (contractType == typeof(IRequestReplyRouter))
{
return new RoutingChannelExtension<IRequestReplyRouter>(endpointBehavior);
}
else
{
Fx.Assert(contractType == typeof(ISimplexSessionRouter), "Was a new contract added?");
return new RoutingChannelExtension<ISimplexSessionRouter>(endpointBehavior);
}
}
public void DoneReceiving(TimeSpan closeTimeout)
{
FxTrace.Trace.SetAndTraceTransfer(this.ActivityID, true);
try
{
if (this.sessionService != null)
{
IAsyncResult result = this.BeginShutdown(this.sessionService, closeTimeout, shutdownCallback, this);
if (result.CompletedSynchronously)
{
this.ShutdownComplete(result);
}
}
else
{
this.channel.Close();
}
}
catch (Exception exception)
{
if (Fx.IsFatal(exception))
{
throw;
}
this.Fault(exception);
}
}
public abstract void EndShutdown(IAsyncResult result);
public void Fault(Exception exception)
{
FxTrace.Trace.SetAndTraceTransfer(this.ActivityID, true);
//Notify the error handlers that a problem occurred
foreach (IErrorHandler errorHandler in this.endpointBehavior.ChannelDispatcher.ErrorHandlers)
{
if (errorHandler.HandleError(exception))
{
break;
}
}
SessionChannels channelsToAbort;
lock (this.thisLock)
{
channelsToAbort = this.sessionChannels;
this.sessionChannels = null;
}
if (channelsToAbort != null)
{
channelsToAbort.AbortAll();
}
RoutingUtilities.Abort(this.channel, this.channel.LocalAddress);
}
void IExtension<IContextChannel>.Attach(IContextChannel owner)
{
this.channel = owner;
this.hasSession = (owner.InputSession != null);
this.OperationTimeout = owner.OperationTimeout;
}
void IExtension<IContextChannel>.Detach(IContextChannel owner)
{
}
}
sealed class RoutingChannelExtension<T> : RoutingChannelExtension
{
public RoutingChannelExtension(RoutingBehavior.RoutingEndpointBehavior endpointBehavior)
: base(endpointBehavior)
{
}
public override IAsyncResult BeginShutdown(RoutingService service, TimeSpan timeout, AsyncCallback callback, object state)
{
return new ProcessMessagesAsyncResult<T>(null, service, timeout, callback, state);
}
public override void EndShutdown(IAsyncResult result)
{
ProcessMessagesAsyncResult<T>.End(result);
}
}
}
|