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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------
namespace System.ServiceModel.Channels
{
using System;
using System.Runtime;
using System.ServiceModel.Routing;
using System.Collections.Generic;
using System.Security.Principal;
using SR2 = System.ServiceModel.SR;
class SynchronousSendBindingElement : BindingElement
{
public override BindingElement Clone()
{
return new SynchronousSendBindingElement();
}
public override T GetProperty<T>(BindingContext context)
{
if (context == null)
{
throw FxTrace.Exception.ArgumentNull("context");
}
return context.GetInnerProperty<T>();
}
public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
{
if (context == null)
{
throw FxTrace.Exception.ArgumentNull("context");
}
Type typeofTChannel = typeof(TChannel);
if (typeofTChannel == typeof(IDuplexChannel) ||
typeofTChannel == typeof(IDuplexSessionChannel) ||
typeofTChannel == typeof(IRequestChannel) ||
typeofTChannel == typeof(IRequestSessionChannel) ||
typeofTChannel == typeof(IOutputChannel) ||
typeofTChannel == typeof(IOutputSessionChannel))
{
return context.CanBuildInnerChannelFactory<TChannel>();
}
return false;
}
public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
{
if (context == null)
{
throw FxTrace.Exception.ArgumentNull("context");
}
if (!this.CanBuildChannelFactory<TChannel>(context.Clone()))
{
throw FxTrace.Exception.Argument("TChannel", SR2.GetString(SR2.ChannelTypeNotSupported, typeof(TChannel)));
}
IChannelFactory<TChannel> innerFactory = context.BuildInnerChannelFactory<TChannel>();
if (innerFactory != null)
{
return new SynchronousChannelFactory<TChannel>(context.Binding, innerFactory);
}
return null;
}
class SynchronousChannelFactory<TChannel> : LayeredChannelFactory<TChannel>
{
public SynchronousChannelFactory(IDefaultCommunicationTimeouts timeouts, IChannelFactory<TChannel> innerFactory)
: base(timeouts, innerFactory)
{
}
protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
{
this.InnerChannelFactory.Open(timeout);
return new CompletedAsyncResult(callback, state);
}
protected override void OnEndOpen(IAsyncResult result)
{
CompletedAsyncResult.End(result);
}
protected override TChannel OnCreateChannel(EndpointAddress address, Uri via)
{
IChannelFactory<TChannel> factory = (IChannelFactory<TChannel>)this.InnerChannelFactory;
TChannel channel = factory.CreateChannel(address, via);
if (channel != null)
{
Type channelType = typeof(TChannel);
if (channelType == typeof(IDuplexSessionChannel))
{
channel = (TChannel)(object)new SynchronousDuplexSessionChannel(this, (IDuplexSessionChannel)channel);
}
else if (channelType == typeof(IRequestChannel))
{
channel = (TChannel)(object)new SynchronousRequestChannel(this, (IRequestChannel)channel);
}
else if (channelType == typeof(IRequestSessionChannel))
{
channel = (TChannel)(object)new SynchronousRequestSessionChannel(this, (IRequestSessionChannel)channel);
}
else if (channelType == typeof(IOutputChannel))
{
channel = (TChannel)(object)new SynchronousOutputChannel(this, (IOutputChannel)channel);
}
else if (channelType == typeof(IOutputSessionChannel))
{
channel = (TChannel)(object)new SynchronousOutputSessionChannel(this, (IOutputSessionChannel)channel);
}
else if (channelType == typeof(IDuplexChannel))
{
channel = (TChannel)(object)new SynchronousDuplexChannel(this, (IDuplexChannel)channel);
}
else
{
throw FxTrace.Exception.AsError(base.CreateChannelTypeNotSupportedException(typeof(TChannel)));
}
}
return channel;
}
}
abstract class SynchronousChannelBase<TChannel> : LayeredChannel<TChannel>
where TChannel : class, IChannel
{
protected SynchronousChannelBase(ChannelManagerBase manager, TChannel innerChannel)
: base(manager, innerChannel)
{
}
protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
{
this.InnerChannel.Open(timeout);
return new CompletedAsyncResult(callback, state);
}
protected override void OnEndOpen(IAsyncResult result)
{
CompletedAsyncResult.End(result);
}
}
class SynchronousDuplexChannelBase<TChannel> : SynchronousOutputChannelBase<TChannel>
where TChannel : class, IDuplexChannel
{
public SynchronousDuplexChannelBase(ChannelManagerBase manager, TChannel innerChannel)
: base(manager, innerChannel)
{
}
public IAsyncResult BeginReceive(TimeSpan timeout, AsyncCallback callback, object state)
{
return this.InnerChannel.BeginReceive(timeout, callback, state);
}
public IAsyncResult BeginReceive(AsyncCallback callback, object state)
{
return this.InnerChannel.BeginReceive(callback, state);
}
public IAsyncResult BeginTryReceive(TimeSpan timeout, AsyncCallback callback, object state)
{
return this.InnerChannel.BeginTryReceive(timeout, callback, state);
}
public IAsyncResult BeginWaitForMessage(TimeSpan timeout, AsyncCallback callback, object state)
{
return this.InnerChannel.BeginWaitForMessage(timeout, callback, state);
}
public Message EndReceive(IAsyncResult result)
{
return this.InnerChannel.EndReceive(result);
}
public bool EndTryReceive(IAsyncResult result, out Message message)
{
return this.InnerChannel.EndTryReceive(result, out message);
}
public bool EndWaitForMessage(IAsyncResult result)
{
return this.InnerChannel.EndWaitForMessage(result);
}
public EndpointAddress LocalAddress
{
get { return this.InnerChannel.LocalAddress; }
}
public Message Receive(TimeSpan timeout)
{
return this.InnerChannel.Receive(timeout);
}
public Message Receive()
{
return this.InnerChannel.Receive();
}
public bool TryReceive(TimeSpan timeout, out Message message)
{
return this.InnerChannel.TryReceive(timeout, out message);
}
public bool WaitForMessage(TimeSpan timeout)
{
return this.InnerChannel.WaitForMessage(timeout);
}
}
class SynchronousDuplexChannel : SynchronousDuplexChannelBase<IDuplexChannel>, IDuplexChannel
{
public SynchronousDuplexChannel(ChannelManagerBase manager, IDuplexChannel innerChannel)
: base(manager, innerChannel)
{
}
}
class SynchronousDuplexSessionChannel : SynchronousDuplexChannelBase<IDuplexSessionChannel>, IDuplexSessionChannel
{
public SynchronousDuplexSessionChannel(ChannelManagerBase manager, IDuplexSessionChannel innerChannel)
: base(manager, innerChannel)
{
}
IDuplexSession ISessionChannel<IDuplexSession>.Session
{
get { return this.InnerChannel.Session; }
}
}
abstract class SynchronousOutputChannelBase<TChannel> : SynchronousChannelBase<TChannel>
where TChannel : class, IOutputChannel
{
public SynchronousOutputChannelBase(ChannelManagerBase manager, TChannel innerChannel)
: base(manager, innerChannel)
{
}
public IAsyncResult BeginSend(Message message, TimeSpan timeout, AsyncCallback callback, object state)
{
this.InnerChannel.Send(message, timeout);
return new CompletedAsyncResult(callback, state);
}
public IAsyncResult BeginSend(Message message, AsyncCallback callback, object state)
{
this.InnerChannel.Send(message);
return new CompletedAsyncResult(callback, state);
}
public void EndSend(IAsyncResult result)
{
CompletedAsyncResult.End(result);
}
public EndpointAddress RemoteAddress
{
get { return this.InnerChannel.RemoteAddress; }
}
public void Send(Message message, TimeSpan timeout)
{
this.InnerChannel.Send(message, timeout);
}
public void Send(Message message)
{
this.InnerChannel.Send(message);
}
public Uri Via
{
get { return this.InnerChannel.Via; }
}
}
class SynchronousOutputChannel : SynchronousOutputChannelBase<IOutputChannel>, IOutputChannel
{
public SynchronousOutputChannel(ChannelManagerBase manager, IOutputChannel innerChannel)
: base(manager, innerChannel)
{
}
}
class SynchronousOutputSessionChannel : SynchronousOutputChannelBase<IOutputSessionChannel>, IOutputSessionChannel
{
public SynchronousOutputSessionChannel(ChannelManagerBase manager, IOutputSessionChannel innerChannel)
: base(manager, innerChannel)
{
}
IOutputSession ISessionChannel<IOutputSession>.Session
{
get { return this.InnerChannel.Session; }
}
}
abstract class SynchronousRequestChannelBase<TChannel> : SynchronousChannelBase<TChannel>
where TChannel : class, IRequestChannel
{
public SynchronousRequestChannelBase(ChannelManagerBase manager, TChannel innerChannel)
: base(manager, innerChannel)
{
}
public IAsyncResult BeginRequest(Message message, TimeSpan timeout, AsyncCallback callback, object state)
{
Message reply = this.InnerChannel.Request(message, timeout);
return new CompletedAsyncResult<Message>(reply, callback, state);
}
public IAsyncResult BeginRequest(Message message, AsyncCallback callback, object state)
{
Message reply = this.InnerChannel.Request(message);
return new CompletedAsyncResult<Message>(reply, callback, state);
}
public Message EndRequest(IAsyncResult result)
{
return CompletedAsyncResult<Message>.End(result);
}
public EndpointAddress RemoteAddress
{
get { return this.InnerChannel.RemoteAddress; }
}
public Message Request(Message message, TimeSpan timeout)
{
return this.InnerChannel.Request(message, timeout);
}
public Message Request(Message message)
{
return this.InnerChannel.Request(message);
}
public Uri Via
{
get { return this.InnerChannel.Via; }
}
}
class SynchronousRequestChannel : SynchronousRequestChannelBase<IRequestChannel>, IRequestChannel
{
public SynchronousRequestChannel(ChannelManagerBase manager, IRequestChannel innerChannel)
: base(manager, innerChannel)
{
}
}
class SynchronousRequestSessionChannel : SynchronousRequestChannelBase<IRequestSessionChannel>, IRequestSessionChannel
{
public SynchronousRequestSessionChannel(ChannelManagerBase manager, IRequestSessionChannel innerChannel)
: base(manager, innerChannel)
{
}
public IOutputSession Session
{
get
{
return this.InnerChannel.Session;
}
}
}
}
}
|