File: DuplexChannelFactory.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (312 lines) | stat: -rw-r--r-- 14,381 bytes parent folder | download | duplicates (7)
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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------

namespace System.ServiceModel
{
    using System.Collections.Generic;
    using System.ServiceModel.Description;
    using System.ServiceModel.Dispatcher;
    using System.ServiceModel.Channels;
    using System.Collections.ObjectModel;
    using System.Diagnostics;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Threading;
    using System.ServiceModel.Diagnostics;
    using System.ServiceModel.Configuration;

    public class DuplexChannelFactory<TChannel> : ChannelFactory<TChannel>
    {
        //Type overloads
        public DuplexChannelFactory(Type callbackInstanceType)
            : this((object)callbackInstanceType)
        { }
        public DuplexChannelFactory(Type callbackInstanceType, Binding binding, String remoteAddress)
            : this((object)callbackInstanceType, binding, new EndpointAddress(remoteAddress))
        { }
        public DuplexChannelFactory(Type callbackInstanceType, Binding binding, EndpointAddress remoteAddress)
            : this((object)callbackInstanceType, binding, remoteAddress)
        { }
        public DuplexChannelFactory(Type callbackInstanceType, Binding binding)
            : this((object)callbackInstanceType, binding)
        { }
        public DuplexChannelFactory(Type callbackInstanceType, string endpointConfigurationName, EndpointAddress remoteAddress)
            : this((object)callbackInstanceType, endpointConfigurationName, remoteAddress)
        { }
        public DuplexChannelFactory(Type callbackInstanceType, string endpointConfigurationName)
            : this((object)callbackInstanceType, endpointConfigurationName)
        { }
        public DuplexChannelFactory(Type callbackInstanceType, ServiceEndpoint endpoint)
            : this((object)callbackInstanceType, endpoint)
        { }

        //InstanceContext overloads
        public DuplexChannelFactory(InstanceContext callbackInstance)
            : this((object)callbackInstance)
        { }
        public DuplexChannelFactory(InstanceContext callbackInstance, Binding binding, String remoteAddress)
            : this((object)callbackInstance, binding, new EndpointAddress(remoteAddress))
        { }
        public DuplexChannelFactory(InstanceContext callbackInstance, Binding binding, EndpointAddress remoteAddress)
            : this((object)callbackInstance, binding, remoteAddress)
        { }
        public DuplexChannelFactory(InstanceContext callbackInstance, Binding binding)
            : this((object)callbackInstance, binding)
        { }
        public DuplexChannelFactory(InstanceContext callbackInstance, string endpointConfigurationName, EndpointAddress remoteAddress)
            : this((object)callbackInstance, endpointConfigurationName, remoteAddress)
        { }
        public DuplexChannelFactory(InstanceContext callbackInstance, string endpointConfigurationName)
            : this((object)callbackInstance, endpointConfigurationName)
        { }
        public DuplexChannelFactory(InstanceContext callbackInstance, ServiceEndpoint endpoint)
            : this((object)callbackInstance, endpoint)
        { }

        // TChannel provides ContractDescription
        public DuplexChannelFactory(object callbackObject)
            : base(typeof(TChannel))
        {
            using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity() : null)
            {
                if (DiagnosticUtility.ShouldUseActivity)
                {
                    ServiceModelActivity.Start(activity, SR.GetString(SR.ActivityConstructChannelFactory, TraceUtility.CreateSourceString(this)), ActivityType.Construct);
                }
                if (callbackObject == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("callbackObject");
                }

                this.CheckAndAssignCallbackInstance(callbackObject);
                this.InitializeEndpoint((string)null, (EndpointAddress)null);
            }
        }

        // TChannel provides ContractDescription, attr/config [TChannel,name] provides Address,Binding
        public DuplexChannelFactory(object callbackObject, string endpointConfigurationName)
            : this(callbackObject, endpointConfigurationName, null)
        {
        }

        // TChannel provides ContractDescription, attr/config [TChannel,name] provides Binding, provide Address explicitly
        public DuplexChannelFactory(object callbackObject, string endpointConfigurationName, EndpointAddress remoteAddress)
            : base(typeof(TChannel))
        {
            using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity() : null)
            {
                if (DiagnosticUtility.ShouldUseActivity)
                {
                    ServiceModelActivity.Start(activity, SR.GetString(SR.ActivityConstructChannelFactory, TraceUtility.CreateSourceString(this)), ActivityType.Construct);
                }
                if (callbackObject == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("callbackObject");
                }

                if (endpointConfigurationName == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("endpointConfigurationName");
                }

                this.CheckAndAssignCallbackInstance(callbackObject);
                this.InitializeEndpoint(endpointConfigurationName, remoteAddress);
            }
        }

        // TChannel provides ContractDescription, attr/config [TChannel,name] provides Address,Binding
        public DuplexChannelFactory(object callbackObject, Binding binding)
            : this(callbackObject, binding, (EndpointAddress)null)
        {
        }

        // TChannel provides ContractDescription, provide Address,Binding explicitly
        public DuplexChannelFactory(object callbackObject, Binding binding, String remoteAddress)
            : this(callbackObject, binding, new EndpointAddress(remoteAddress))
        {
        }
        // TChannel provides ContractDescription, provide Address,Binding explicitly
        public DuplexChannelFactory(object callbackObject, Binding binding, EndpointAddress remoteAddress)
            : base(typeof(TChannel))
        {
            using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity() : null)
            {
                if (DiagnosticUtility.ShouldUseActivity)
                {
                    ServiceModelActivity.Start(activity, SR.GetString(SR.ActivityConstructChannelFactory, TraceUtility.CreateSourceString(this)), ActivityType.Construct);
                }
                if (callbackObject == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("callbackObject");
                }

                if (binding == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("binding");
                }

                this.CheckAndAssignCallbackInstance(callbackObject);
                this.InitializeEndpoint(binding, remoteAddress);
            }
        }

        // provide ContractDescription,Address,Binding explicitly
        public DuplexChannelFactory(object callbackObject, ServiceEndpoint endpoint)
            : base(typeof(TChannel))
        {
            using (ServiceModelActivity activity = DiagnosticUtility.ShouldUseActivity ? ServiceModelActivity.CreateBoundedActivity() : null)
            {
                if (DiagnosticUtility.ShouldUseActivity)
                {
                    ServiceModelActivity.Start(activity, SR.GetString(SR.ActivityConstructChannelFactory, TraceUtility.CreateSourceString(this)), ActivityType.Construct);
                }
                if (callbackObject == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("callbackObject");
                }

                if (endpoint == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("endpoint");
                }

                this.CheckAndAssignCallbackInstance(callbackObject);
                this.InitializeEndpoint(endpoint);
            }
        }

        internal void CheckAndAssignCallbackInstance(object callbackInstance)
        {
            if (callbackInstance is Type)
            {
                this.CallbackType = (Type)callbackInstance;
            }
            else if (callbackInstance is InstanceContext)
            {
                this.CallbackInstance = (InstanceContext)callbackInstance;
            }
            else
            {
                this.CallbackInstance = new InstanceContext(callbackInstance);
            }
        }

        public TChannel CreateChannel(InstanceContext callbackInstance)
        {
            return CreateChannel(callbackInstance, CreateEndpointAddress(this.Endpoint), null);
        }

        public TChannel CreateChannel(InstanceContext callbackInstance, EndpointAddress address)
        {
            if (address == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("address");
            }

            return CreateChannel(callbackInstance, address, address.Uri);
        }

        public override TChannel CreateChannel(EndpointAddress address, Uri via)
        {
            return CreateChannel(this.CallbackInstance, address, via);
        }

        public virtual TChannel CreateChannel(InstanceContext callbackInstance, EndpointAddress address, Uri via)
        {
            if (address == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("address");
            }

            if (this.CallbackType != null && callbackInstance == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxCreateDuplexChannelNoCallback1)));
            }
            if (callbackInstance == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxCreateDuplexChannelNoCallback)));
            }

            if (callbackInstance.UserObject == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxCreateDuplexChannelNoCallbackUserObject)));
            }

            if (!this.HasDuplexOperations())
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxCreateDuplexChannel1, this.Endpoint.Contract.Name)));
            }

            Type userObjectType = callbackInstance.UserObject.GetType();
            Type callbackType = this.Endpoint.Contract.CallbackContractType;
            if (callbackType != null && !callbackType.IsAssignableFrom(userObjectType))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(
                    SR.SFxCreateDuplexChannelBadCallbackUserObject, callbackType)));
            }

            EnsureOpened();
            TChannel result = (TChannel)this.ServiceChannelFactory.CreateChannel(typeof(TChannel), address, via);

            IDuplexContextChannel duplexChannel = result as IDuplexContextChannel;
            if (duplexChannel != null)
            {
                duplexChannel.CallbackInstance = callbackInstance;
            }
            return result;
        }

        //Static funtions to create channels
        static InstanceContext GetInstanceContextForObject(object callbackObject)
        {
            if (callbackObject is InstanceContext)
            {
                return (InstanceContext)callbackObject;
            }

            return new InstanceContext(callbackObject);
        }

        public static TChannel CreateChannel(object callbackObject, String endpointConfigurationName)
        {
            return CreateChannel(GetInstanceContextForObject(callbackObject), endpointConfigurationName);
        }

        public static TChannel CreateChannel(object callbackObject, Binding binding, EndpointAddress endpointAddress)
        {
            return CreateChannel(GetInstanceContextForObject(callbackObject), binding, endpointAddress);
        }

        public static TChannel CreateChannel(object callbackObject, Binding binding, EndpointAddress endpointAddress, Uri via)
        {
            return CreateChannel(GetInstanceContextForObject(callbackObject), binding, endpointAddress, via);
        }

        public static TChannel CreateChannel(InstanceContext callbackInstance, String endpointConfigurationName)
        {
            DuplexChannelFactory<TChannel> channelFactory = new DuplexChannelFactory<TChannel>(callbackInstance, endpointConfigurationName);
            TChannel channel = channelFactory.CreateChannel();
            SetFactoryToAutoClose(channel);
            return channel;
        }

        public static TChannel CreateChannel(InstanceContext callbackInstance, Binding binding, EndpointAddress endpointAddress)
        {
            DuplexChannelFactory<TChannel> channelFactory = new DuplexChannelFactory<TChannel>(callbackInstance, binding, endpointAddress);
            TChannel channel = channelFactory.CreateChannel();
            SetFactoryToAutoClose(channel);
            return channel;
        }

        public static TChannel CreateChannel(InstanceContext callbackInstance, Binding binding, EndpointAddress endpointAddress, Uri via)
        {
            DuplexChannelFactory<TChannel> channelFactory = new DuplexChannelFactory<TChannel>(callbackInstance, binding);
            TChannel channel = channelFactory.CreateChannel(endpointAddress, via);
            SetFactoryToAutoClose(channel);
            return channel;
        }

    }
}