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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
//----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
namespace System.ServiceModel.Channels
{
using System.Collections.Generic;
using System.Runtime;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Security;
using SR = System.ServiceModel.SR;
sealed class TransactionChannelFactory<TChannel> : LayeredChannelFactory<TChannel>, ITransactionChannelManager
{
TransactionFlowOption flowIssuedTokens;
SecurityStandardsManager standardsManager;
Dictionary<DirectionalAction, TransactionFlowOption> dictionary;
TransactionProtocol transactionProtocol;
bool allowWildcardAction;
public TransactionChannelFactory(
TransactionProtocol transactionProtocol,
BindingContext context,
Dictionary<DirectionalAction, TransactionFlowOption> dictionary,
bool allowWildcardAction)
: base(context.Binding, context.BuildInnerChannelFactory<TChannel>())
{
this.dictionary = dictionary;
this.TransactionProtocol = transactionProtocol;
this.allowWildcardAction = allowWildcardAction;
this.standardsManager = SecurityStandardsHelper.CreateStandardsManager(this.TransactionProtocol);
}
public TransactionProtocol TransactionProtocol
{
get
{
return this.transactionProtocol;
}
set
{
if (!TransactionProtocol.IsDefined(value))
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new ArgumentException(SR.GetString(SR.SFxBadTransactionProtocols)));
this.transactionProtocol = value;
}
}
public TransactionFlowOption FlowIssuedTokens
{
get { return this.flowIssuedTokens; }
set { this.flowIssuedTokens = value; }
}
public SecurityStandardsManager StandardsManager
{
get { return this.standardsManager; }
set
{
this.standardsManager = (value != null ? value : SecurityStandardsHelper.CreateStandardsManager(this.transactionProtocol));
}
}
public IDictionary<DirectionalAction, TransactionFlowOption> Dictionary
{
get { return this.dictionary; }
}
public TransactionFlowOption GetTransaction(MessageDirection direction, string action)
{
TransactionFlowOption txOption;
if (!dictionary.TryGetValue(new DirectionalAction(direction, action), out txOption))
{
//Fixinng this for clients that opted in for lesser validation before flowing out a transaction
if (this.allowWildcardAction && dictionary.TryGetValue(new DirectionalAction(direction, MessageHeaders.WildcardAction), out txOption))
{
return txOption;
}
else
return TransactionFlowOption.NotAllowed;
}
else
return txOption;
}
protected override TChannel OnCreateChannel(EndpointAddress remoteAddress, Uri via)
{
TChannel innerChannel = ((IChannelFactory<TChannel>)InnerChannelFactory).CreateChannel(remoteAddress, via);
return CreateTransactionChannel(innerChannel);
}
TChannel CreateTransactionChannel(TChannel innerChannel)
{
if (typeof(TChannel) == typeof(IDuplexSessionChannel))
{
return (TChannel)(object)new TransactionDuplexSessionChannel(this, (IDuplexSessionChannel)(object)innerChannel);
}
else if (typeof(TChannel) == typeof(IRequestSessionChannel))
{
return (TChannel)(object)new TransactionRequestSessionChannel(this, (IRequestSessionChannel)(object)innerChannel);
}
else if (typeof(TChannel) == typeof(IOutputSessionChannel))
{
return (TChannel)(object)new TransactionOutputSessionChannel(this, (IOutputSessionChannel)(object)innerChannel);
}
else if (typeof(TChannel) == typeof(IOutputChannel))
{
return (TChannel)(object)new TransactionOutputChannel(this, (IOutputChannel)(object)innerChannel);
}
else if (typeof(TChannel) == typeof(IRequestChannel))
{
return (TChannel)(object)new TransactionRequestChannel(this, (IRequestChannel)(object)innerChannel);
}
else if (typeof(TChannel) == typeof(IDuplexChannel))
{
return (TChannel)(object)new TransactionDuplexChannel(this, (IDuplexChannel)(object)innerChannel);
}
else
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateChannelTypeNotSupportedException(typeof(TChannel)));
}
}
//===========================================================
// Transaction Output Channel classes
//===========================================================
sealed class TransactionOutputChannel : TransactionOutputChannelGeneric<IOutputChannel>
{
public TransactionOutputChannel(ChannelManagerBase channelManager, IOutputChannel innerChannel)
: base(channelManager, innerChannel)
{
}
}
sealed class TransactionRequestChannel : TransactionRequestChannelGeneric<IRequestChannel>
{
public TransactionRequestChannel(ChannelManagerBase channelManager, IRequestChannel innerChannel)
: base(channelManager, innerChannel)
{
}
}
sealed class TransactionDuplexChannel : TransactionOutputDuplexChannelGeneric<IDuplexChannel>
{
public TransactionDuplexChannel(ChannelManagerBase channelManager, IDuplexChannel innerChannel)
: base(channelManager, innerChannel)
{
}
}
sealed class TransactionOutputSessionChannel : TransactionOutputChannelGeneric<IOutputSessionChannel>, IOutputSessionChannel
{
public TransactionOutputSessionChannel(ChannelManagerBase channelManager, IOutputSessionChannel innerChannel)
: base(channelManager, innerChannel)
{
}
public IOutputSession Session { get { return InnerChannel.Session; } }
}
sealed class TransactionRequestSessionChannel : TransactionRequestChannelGeneric<IRequestSessionChannel>, IRequestSessionChannel
{
public TransactionRequestSessionChannel(ChannelManagerBase channelManager, IRequestSessionChannel innerChannel)
: base(channelManager, innerChannel)
{
}
public IOutputSession Session { get { return InnerChannel.Session; } }
}
sealed class TransactionDuplexSessionChannel : TransactionOutputDuplexChannelGeneric<IDuplexSessionChannel>, IDuplexSessionChannel
{
public TransactionDuplexSessionChannel(ChannelManagerBase channelManager, IDuplexSessionChannel innerChannel)
: base(channelManager, innerChannel)
{
}
public IDuplexSession Session { get { return InnerChannel.Session; } }
}
}
static class SecurityStandardsHelper
{
static SecurityStandardsManager SecurityStandardsManager2007 =
CreateStandardsManager(MessageSecurityVersion.WSSecurity11WSTrust13WSSecureConversation13WSSecurityPolicy12);
static SecurityStandardsManager CreateStandardsManager(MessageSecurityVersion securityVersion)
{
return new SecurityStandardsManager(
securityVersion,
new WSSecurityTokenSerializer(securityVersion.SecurityVersion, securityVersion.TrustVersion, securityVersion.SecureConversationVersion, false, null, null, null));
}
public static SecurityStandardsManager CreateStandardsManager(TransactionProtocol transactionProtocol)
{
if (transactionProtocol == TransactionProtocol.WSAtomicTransactionOctober2004 ||
transactionProtocol == TransactionProtocol.OleTransactions)
{
return SecurityStandardsManager.DefaultInstance;
}
else
{
return SecurityStandardsHelper.SecurityStandardsManager2007;
}
}
}
//==============================================================
// Transaction channel base generic classes
//==============================================================
class TransactionOutputChannelGeneric<TChannel> : TransactionChannel<TChannel>, IOutputChannel
where TChannel : class, IOutputChannel
{
public TransactionOutputChannelGeneric(ChannelManagerBase channelManager, TChannel innerChannel)
: base(channelManager, innerChannel)
{
}
public EndpointAddress RemoteAddress
{
get
{
return InnerChannel.RemoteAddress;
}
}
public Uri Via
{
get
{
return InnerChannel.Via;
}
}
public IAsyncResult BeginSend(Message message, AsyncCallback callback, object state)
{
return this.BeginSend(message, this.DefaultSendTimeout, callback, state);
}
public IAsyncResult BeginSend(Message message, TimeSpan timeout, AsyncCallback asyncCallback, object state)
{
TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
WriteTransactionDataToMessage(message, MessageDirection.Input);
return InnerChannel.BeginSend(message, timeoutHelper.RemainingTime(), asyncCallback, state);
}
public void EndSend(IAsyncResult result)
{
InnerChannel.EndSend(result);
}
public void Send(Message message)
{
this.Send(message, this.DefaultSendTimeout);
}
public void Send(Message message, TimeSpan timeout)
{
TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
WriteTransactionDataToMessage(message, MessageDirection.Input);
InnerChannel.Send(message, timeoutHelper.RemainingTime());
}
}
class TransactionRequestChannelGeneric<TChannel> : TransactionChannel<TChannel>, IRequestChannel
where TChannel : class, IRequestChannel
{
public TransactionRequestChannelGeneric(ChannelManagerBase channelManager, TChannel innerChannel)
: base(channelManager, innerChannel)
{
}
public EndpointAddress RemoteAddress
{
get
{
return InnerChannel.RemoteAddress;
}
}
public Uri Via
{
get
{
return InnerChannel.Via;
}
}
public IAsyncResult BeginRequest(Message message, AsyncCallback callback, object state)
{
return this.BeginRequest(message, this.DefaultSendTimeout, callback, state);
}
public IAsyncResult BeginRequest(Message message, TimeSpan timeout, AsyncCallback asyncCallback, object state)
{
TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
WriteTransactionDataToMessage(message, MessageDirection.Input);
return InnerChannel.BeginRequest(message, timeoutHelper.RemainingTime(), asyncCallback, state);
}
public Message EndRequest(IAsyncResult result)
{
Message reply = InnerChannel.EndRequest(result);
if (reply != null)
this.ReadIssuedTokens(reply, MessageDirection.Output);
return reply;
}
public Message Request(Message message)
{
return this.Request(message, this.DefaultSendTimeout);
}
public Message Request(Message message, TimeSpan timeout)
{
TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
WriteTransactionDataToMessage(message, MessageDirection.Input);
Message reply = InnerChannel.Request(message, timeoutHelper.RemainingTime());
if (reply != null)
this.ReadIssuedTokens(reply, MessageDirection.Output);
return reply;
}
}
class TransactionOutputDuplexChannelGeneric<TChannel> : TransactionDuplexChannelGeneric<TChannel>
where TChannel : class, IDuplexChannel
{
public TransactionOutputDuplexChannelGeneric(ChannelManagerBase channelManager, TChannel innerChannel)
: base(channelManager, innerChannel, MessageDirection.Output)
{
}
}
}
|