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
|
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------
namespace System.ServiceModel.Routing
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
[Fx.Tag.XamlVisible(false)]
public sealed class RoutingBehavior : IServiceBehavior
{
RoutingConfiguration configuration;
public RoutingBehavior(RoutingConfiguration routingConfiguration)
{
if (routingConfiguration == null)
{
throw FxTrace.Exception.ArgumentNull("routingConfiguration");
}
this.configuration = routingConfiguration;
}
void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
RoutingExtension routingExtension = new RoutingExtension(this.configuration);
serviceHostBase.Extensions.Add(routingExtension);
for (int i = 0; i < serviceHostBase.ChannelDispatchers.Count; i++)
{
ChannelDispatcher channelDispatcher = serviceHostBase.ChannelDispatchers[i] as ChannelDispatcher;
if (channelDispatcher != null)
{
foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints)
{
if (!endpointDispatcher.IsSystemEndpoint &&
RoutingUtilities.IsRoutingServiceNamespace(endpointDispatcher.ContractNamespace))
{
DispatchRuntime dispatchRuntime = endpointDispatcher.DispatchRuntime;
//Since we use PerSession instancing this concurrency only applies to messages
//in the same session, also needed to maintain order.
dispatchRuntime.ConcurrencyMode = ConcurrencyMode.Single;
dispatchRuntime.EnsureOrderedDispatch = this.configuration.EnsureOrderedDispatch;
}
}
}
}
}
void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
HashSet<string> endpoints = new HashSet<string>();
foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)
{
if (!endpoint.IsSystemEndpoint &&
RoutingUtilities.IsRoutingServiceNamespace(endpoint.Contract.Namespace))
{
endpoint.Behaviors.Add(new RoutingEndpointBehavior(endpoint));
endpoints.Add(endpoint.Name);
}
}
EndpointNameMessageFilter.Validate(this.configuration.InternalFilterTable.Keys, endpoints);
}
public static Type GetContractForDescription(ContractDescription description)
{
if (description == null)
{
throw FxTrace.Exception.ArgumentNull("description");
}
if (description.CallbackContractType != null)
{
return typeof(IDuplexSessionRouter);
}
bool allOneWay = true;
foreach (OperationDescription operation in description.Operations)
{
if (!operation.IsOneWay)
{
allOneWay = false;
break;
}
}
if (allOneWay)
{
if (description.SessionMode == SessionMode.Required)
{
return typeof(ISimplexSessionRouter);
}
else
{
return typeof(ISimplexDatagramRouter);
}
}
else
{
return typeof(IRequestReplyRouter);
}
}
internal class RoutingEndpointBehavior : IEndpointBehavior, IChannelInitializer, IInputSessionShutdown
{
public RoutingEndpointBehavior(ServiceEndpoint endpoint)
{
this.Endpoint = endpoint;
this.EndpointName = endpoint.Name;
}
internal ChannelDispatcher ChannelDispatcher
{
get;
private set;
}
internal ServiceEndpoint Endpoint
{
get;
private set;
}
internal string EndpointName
{
get;
private set;
}
internal bool ImpersonationRequired
{
get;
private set;
}
internal bool ReceiveContextEnabled
{
get;
private set;
}
internal bool TransactedReceiveEnabled
{
get;
private set;
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
//Turn on ReceiveContext here if supported
IReceiveContextSettings receiveContextSettings = endpoint.Binding.GetProperty<IReceiveContextSettings>(bindingParameters);
if (receiveContextSettings != null)
{
receiveContextSettings.Enabled = true;
}
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
this.ChannelDispatcher = endpointDispatcher.ChannelDispatcher;
this.ChannelDispatcher.ChannelInitializers.Add(this);
endpointDispatcher.DispatchRuntime.InputSessionShutdownHandlers.Add(this);
endpointDispatcher.DispatchRuntime.AutomaticInputSessionShutdown = false;
if (endpointDispatcher.DispatchRuntime.ImpersonateCallerForAllOperations)
{
this.ImpersonationRequired = true;
}
else if (AspNetEnvironment.Current.AspNetCompatibilityEnabled)
{
this.ImpersonationRequired = true;
}
BindingParameterCollection bindingParams = new BindingParameterCollection();
if (RoutingUtilities.IsTransactedReceive(endpoint.Binding, bindingParams))
{
foreach (OperationDescription operation in endpoint.Contract.Operations)
{
if (operation.Behaviors.Find<TransactedReceiveOperationBehavior>() == null)
{
operation.Behaviors.Add(new TransactedReceiveOperationBehavior());
}
}
this.ChannelDispatcher.IsTransactedReceive = true;
endpointDispatcher.DispatchRuntime.TransactionAutoCompleteOnSessionClose = true;
this.TransactedReceiveEnabled = true;
}
IReceiveContextSettings rcSettings = endpoint.Binding.GetProperty<IReceiveContextSettings>(bindingParams);
if (rcSettings != null && rcSettings.Enabled)
{
foreach (OperationDescription operation in endpoint.Contract.Operations)
{
ReceiveContextEnabledAttribute rcEnabled = new ReceiveContextEnabledAttribute();
rcEnabled.ManualControl = true;
operation.Behaviors.Add(rcEnabled);
}
this.ReceiveContextEnabled = true;
//Switch TransactedReceive off, because we don't want the Dispatcher creating any Transaction
endpointDispatcher.ChannelDispatcher.IsTransactedReceive = false;
endpointDispatcher.DispatchRuntime.TransactionAutoCompleteOnSessionClose = false;
}
}
public void Validate(ServiceEndpoint endpoint)
{
}
void IChannelInitializer.Initialize(IClientChannel channel)
{
RoutingChannelExtension channelState = RoutingChannelExtension.Create(this);
channel.Extensions.Add(channelState);
}
void IInputSessionShutdown.ChannelFaulted(IDuplexContextChannel channel)
{
RoutingChannelExtension channelExtension = channel.Extensions.Find<RoutingChannelExtension>();
if (channelExtension != null)
{
channelExtension.Fault(new CommunicationObjectFaultedException());
}
else
{
RoutingUtilities.Abort(channel, channel.LocalAddress);
}
}
void IInputSessionShutdown.DoneReceiving(IDuplexContextChannel channel)
{
RoutingChannelExtension channelExtension = channel.Extensions.Find<RoutingChannelExtension>();
channelExtension.DoneReceiving(this.Endpoint.Binding.CloseTimeout);
}
}
class TransactedReceiveOperationBehavior : IOperationBehavior
{
public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
if (operationDescription.Behaviors.Find<ReceiveContextEnabledAttribute>() == null)
{
dispatchOperation.TransactionRequired = true;
}
ContractDescription contract = operationDescription.DeclaringContract;
if (dispatchOperation.IsOneWay && contract.SessionMode == SessionMode.Required)
{
dispatchOperation.Parent.ConcurrencyMode = ConcurrencyMode.Single;
dispatchOperation.Parent.ReleaseServiceInstanceOnTransactionComplete = false;
dispatchOperation.TransactionAutoComplete = false;
}
}
public void Validate(OperationDescription operationDescription)
{
}
}
}
}
|