ContextChannelFactory.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System;
  7. using System.Diagnostics;
  8. using System.Collections.Generic;
  9. using System.ServiceModel;
  10. using System.ServiceModel.Diagnostics;
  11. using System.Runtime.Diagnostics;
  12. class ContextChannelFactory<TChannel> : LayeredChannelFactory<TChannel>
  13. {
  14. ContextExchangeMechanism contextExchangeMechanism;
  15. Uri callbackAddress;
  16. bool contextManagementEnabled;
  17. public ContextChannelFactory(BindingContext context, ContextExchangeMechanism contextExchangeMechanism, Uri callbackAddress, bool contextManagementEnabled)
  18. : base(context == null ? null : context.Binding, context == null ? null : context.BuildInnerChannelFactory<TChannel>())
  19. {
  20. if (!ContextExchangeMechanismHelper.IsDefined(contextExchangeMechanism))
  21. {
  22. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("contextExchangeMechanism"));
  23. }
  24. this.contextExchangeMechanism = contextExchangeMechanism;
  25. this.callbackAddress = callbackAddress;
  26. this.contextManagementEnabled = contextManagementEnabled;
  27. }
  28. protected override TChannel OnCreateChannel(EndpointAddress address, Uri via)
  29. {
  30. if (DiagnosticUtility.ShouldTraceInformation)
  31. {
  32. string traceText = SR.GetString(SR.ContextChannelFactoryChannelCreatedDetail, address, via);
  33. TraceUtility.TraceEvent(TraceEventType.Information,
  34. TraceCode.ContextChannelFactoryChannelCreated, SR.GetString(SR.TraceCodeContextChannelFactoryChannelCreated),
  35. new StringTraceRecord("ChannelDetail", traceText),
  36. this, null);
  37. }
  38. if (typeof(TChannel) == typeof(IOutputChannel))
  39. {
  40. return (TChannel)(object)new ContextOutputChannel(this, ((IChannelFactory<IOutputChannel>)this.InnerChannelFactory).CreateChannel(address, via), this.contextExchangeMechanism, this.callbackAddress, this.contextManagementEnabled);
  41. }
  42. else if (typeof(TChannel) == typeof(IOutputSessionChannel))
  43. {
  44. return (TChannel)(object)new ContextOutputSessionChannel(this, ((IChannelFactory<IOutputSessionChannel>)this.InnerChannelFactory).CreateChannel(address, via), this.contextExchangeMechanism, this.callbackAddress, this.contextManagementEnabled);
  45. }
  46. if (typeof(TChannel) == typeof(IRequestChannel))
  47. {
  48. return (TChannel)(object)new ContextRequestChannel(this, ((IChannelFactory<IRequestChannel>)this.InnerChannelFactory).CreateChannel(address, via), this.contextExchangeMechanism, this.callbackAddress, this.contextManagementEnabled);
  49. }
  50. else if (typeof(TChannel) == typeof(IRequestSessionChannel))
  51. {
  52. return (TChannel)(object)new ContextRequestSessionChannel(this, ((IChannelFactory<IRequestSessionChannel>)this.InnerChannelFactory).CreateChannel(address, via), this.contextExchangeMechanism, this.callbackAddress, this.contextManagementEnabled);
  53. }
  54. else // IDuplexSessionChannel
  55. {
  56. return (TChannel)(object)new ContextDuplexSessionChannel(this, ((IChannelFactory<IDuplexSessionChannel>)this.InnerChannelFactory).CreateChannel(address, via), this.contextExchangeMechanism, via, this.callbackAddress, this.contextManagementEnabled);
  57. }
  58. }
  59. }
  60. }