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