File: Send.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (447 lines) | stat: -rw-r--r-- 16,598 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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.ServiceModel.Activities
{
    using System.Activities;
    using System.Activities.Expressions;
    using System.Activities.Statements;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Net.Security;
    using System.Runtime;
    using System.Security.Principal;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Description;
    using System.ServiceModel.Dispatcher;
    using System.ServiceModel.XamlIntegration;
    using System.Windows.Markup;
    using System.Xml.Linq;

    [ContentProperty("Content")]
    public sealed class Send : Activity
    {
        ToRequest requestFormatter;
        InternalSendMessage internalSend;
        Collection<Type> knownTypes;

        Collection<CorrelationInitializer> correlationInitializers;

        bool isOneWay;
        IClientMessageFormatter lazyFormatter;
        IList<CorrelationQuery> lazyCorrelationQueries;

        bool? channelCacheEnabled;

        public Send()
        {
            this.isOneWay = true; // ReceiveReply if defined by user, will set this to false
            this.TokenImpersonationLevel = TokenImpersonationLevel.Identification;
            base.Implementation = () =>
            {
                // if CacheMetadata isn't called, bail early
                if (this.internalSend == null)
                {
                    return null;
                }

                if (this.requestFormatter == null) 
                {
                    return this.internalSend;
                }
                else
                {
                    Variable<Message> request = new Variable<Message> { Name = "RequestMessage" };
                    this.requestFormatter.Message = new OutArgument<Message>(request);
                    this.requestFormatter.Send = this;
                    this.internalSend.Message = new InArgument<Message>(request);
                    this.internalSend.MessageOut = new OutArgument<Message>(request);

                    return new MessagingNoPersistScope
                    {
                        Body = new Sequence
                        {
                            Variables = { request },
                            Activities = 
                            { 
                                this.requestFormatter, 
                                this.internalSend, 
                            }
                        }
                    };
                }
            };
        }

        // the content to send (either message or parameters-based) declared by the user
        [DefaultValue(null)]
        public SendContent Content
        {
            get;
            set;
        }

        // Internally, we should always use InternalContent since this property may have default content that we added
        internal SendContent InternalContent
        {
            get
            {
                return this.Content ?? SendContent.DefaultSendContent;
            }
        }

        // Additional correlations allow situations where a "session" involves multiple
        // messages between two workflow instances.
        public Collection<CorrelationInitializer> CorrelationInitializers
        {
            get
            {
                if (this.correlationInitializers == null)
                {
                    this.correlationInitializers = new Collection<CorrelationInitializer>();
                }
                return this.correlationInitializers;
            }
        }

        // Allows the action for the message to be specified by the user/designer.
        // If not set, the value is constructed from the Name of the activity.
        // If specified on the Send side of a message, the Receive side needs to have the same value in order
        // for the message to be delivered correctly.
        [DefaultValue(null)]
        public string Action
        {
            get;
            set;
        }

        [DefaultValue(null)]
        public InArgument<CorrelationHandle> CorrelatesWith
        {
            get;
            set;
        }

        // Specifies the endpoint of the service we are sending to. If not specified, it must come
        // from the configuration file.
        [DefaultValue(null)]
        public Endpoint Endpoint
        {
            get;
            set;
        }

        // This is used to load the client Endpoint from configuration. This should be mutually exclusive
        // with the Endpoint property.
        // optional defaults to ServiceContractName.LocalName
        [DefaultValue(null)]
        public string EndpointConfigurationName
        {
            get;
            set;
        }

        // Allows the address portion of the endpoint to be overridden at runtime by the
        // workflow instance.
        [DefaultValue(null)]
        public InArgument<Uri> EndpointAddress
        {
            get;
            set;
        }

        // Do we need this?
        public Collection<Type> KnownTypes
        {
            get
            {
                if (this.knownTypes == null)
                {
                    this.knownTypes = new Collection<Type>();
                }
                return this.knownTypes;
            }
        }

        // this will be used to construct the default value for Action if the Action property is
        // not specifically set.
        // There is validation to make sure either this or Action has a value.
        [DefaultValue(null)]
        public string OperationName
        {
            get;
            set;
        }

        // The protection level for the message.
        // If ValueType or Value.Expression.Type is MessageContract, the MessageContract definition may have additional settings.
        // Default if this is not specified is Sign.
        [DefaultValue(null)]
        public ProtectionLevel? ProtectionLevel
        {
            get;
            set;
        }

        // Maybe an enum to limit possibilities
        // I am still a little fuzzy on this property.
        [DefaultValue(SerializerOption.DataContractSerializer)]
        public SerializerOption SerializerOption
        {
            get;
            set;
        }

        // The service contract name. This allows the same workflow instance to implement multiple
        // servce "contracts". If not specified and this is the first Receive* activity in the
        // workflow, contract inference uses this activity's Name as the service contract name.
        [DefaultValue(null)]
        [TypeConverter(typeof(ServiceXNameTypeConverter))]
        public XName ServiceContractName
        {
            get;
            set;
        }
        // The token impersonation level that is allowed for the receiver of the message.
        [DefaultValue(TokenImpersonationLevel.Identification)]
        public TokenImpersonationLevel TokenImpersonationLevel
        {
            get;
            set;
        }

        internal bool ChannelCacheEnabled
        {
            get
            {
                Fx.Assert(this.channelCacheEnabled.HasValue, "The value of channelCacheEnabled must be initialized!");
                return this.channelCacheEnabled.Value;
            }
        }

        internal bool OperationUsesMessageContract
        {
            get;
            set;
        }

        internal OperationDescription OperationDescription
        {
            get;
            set;
        }

        protected override void CacheMetadata(ActivityMetadata metadata)
        {
            if (string.IsNullOrEmpty(this.OperationName))
            {
                metadata.AddValidationError(SR.MissingOperationName(this.DisplayName));
            }
            if (this.ServiceContractName == null)
            {
                string errorOperationName = ContractValidationHelper.GetErrorMessageOperationName(this.OperationName);
                metadata.AddValidationError(SR.MissingServiceContractName(this.DisplayName, errorOperationName));
            }

            if (this.Endpoint == null)
            {
                if (string.IsNullOrEmpty(this.EndpointConfigurationName))
                {
                    metadata.AddValidationError(SR.EndpointNotSet(this.DisplayName, this.OperationName));
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(this.EndpointConfigurationName))
                {
                    metadata.AddValidationError(SR.EndpointIncorrectlySet(this.DisplayName, this.OperationName));
                }
                if (this.Endpoint.Binding == null)
                {
                    metadata.AddValidationError(SR.MissingBindingInEndpoint(this.Endpoint.Name, this.ServiceContractName));
                }
            }

            // validate Correlation Initializers
            MessagingActivityHelper.ValidateCorrelationInitializer(metadata, this.correlationInitializers, false, this.DisplayName, this.OperationName);
            
            // Add runtime arguments
            MessagingActivityHelper.AddRuntimeArgument(this.CorrelatesWith, "CorrelatesWith", Constants.CorrelationHandleType, ArgumentDirection.In, metadata);
            MessagingActivityHelper.AddRuntimeArgument(this.EndpointAddress, "EndpointAddress", Constants.UriType, ArgumentDirection.In, metadata);

            // Validate Content
            this.InternalContent.CacheMetadata(metadata, this, this.OperationName);

            if (this.correlationInitializers != null)
            {
                for (int i = 0; i < this.correlationInitializers.Count; i++)
                {
                    CorrelationInitializer initializer = this.correlationInitializers[i];
                    initializer.ArgumentName = Constants.Parameter + i;
                    RuntimeArgument initializerArgument = new RuntimeArgument(initializer.ArgumentName, Constants.CorrelationHandleType, ArgumentDirection.In);
                    metadata.Bind(initializer.CorrelationHandle, initializerArgument);
                    metadata.AddArgument(initializerArgument);
                }
            }

            if (!metadata.HasViolations)
            {
                if (this.InternalContent is SendMessageContent
                    && MessageBuilder.IsMessageContract(((SendMessageContent)this.InternalContent).InternalDeclaredMessageType))
                {
                    this.OperationUsesMessageContract = true;
                }

                this.internalSend = CreateInternalSend();
                this.InternalContent.ConfigureInternalSend(this.internalSend, out this.requestFormatter);

                if (this.requestFormatter != null && this.lazyFormatter != null)
                {
                    this.requestFormatter.Formatter = this.lazyFormatter;
                }
            }
            else
            {
                this.internalSend = null;
                this.requestFormatter = null;
            }
        }

        InternalSendMessage CreateInternalSend()
        {
            InternalSendMessage result = new InternalSendMessage
            {
                OwnerDisplayName = this.DisplayName,
                OperationName = this.OperationName,
                CorrelatesWith = new InArgument<CorrelationHandle>(new ArgumentValue<CorrelationHandle> { ArgumentName = "CorrelatesWith" }),
                Endpoint = this.Endpoint,
                EndpointConfigurationName = this.EndpointConfigurationName,
                IsOneWay = this.isOneWay,
                IsSendReply = false,
                TokenImpersonationLevel = this.TokenImpersonationLevel,
                ServiceContractName = this.ServiceContractName,
                Action = this.Action,
                Parent = this
            };

            if (this.correlationInitializers != null)
            {
                foreach (CorrelationInitializer correlation in this.correlationInitializers)
                {
                    result.CorrelationInitializers.Add(correlation.Clone());
                }

                Collection<CorrelationQuery> internalCorrelationQueryCollection = ContractInferenceHelper.CreateClientCorrelationQueries(null, this.correlationInitializers,
                        this.Action, this.ServiceContractName, this.OperationName, false);
                Fx.Assert(internalCorrelationQueryCollection.Count <= 1, "Querycollection for send cannot have more than one correlation query");
                if (internalCorrelationQueryCollection.Count == 1)
                {
                    result.CorrelationQuery = internalCorrelationQueryCollection[0];
                }
            }

            if (this.EndpointAddress != null)
            {
                result.EndpointAddress = new InArgument<Uri>(context => ((InArgument<Uri>)this.EndpointAddress).Get(context));
            }

            if (this.lazyCorrelationQueries != null)
            {
                foreach (CorrelationQuery correlationQuery in this.lazyCorrelationQueries)
                {
                    result.ReplyCorrelationQueries.Add(correlationQuery);
                }
            }

            return result;
        }

        // Acccessed by ReceiveReply.CreateBody to set OneWay to false
        internal void SetIsOneWay(bool value)
        {
            this.isOneWay = value;
            if (this.internalSend != null)
            {
                this.internalSend.IsOneWay = this.isOneWay;
            }
        }

        internal MessageVersion GetMessageVersion()
        {
            return this.internalSend.GetMessageVersion();
        }

        internal void SetFormatter(IClientMessageFormatter formatter)
        {
            if (this.requestFormatter != null)
            {
                this.requestFormatter.Formatter = formatter;
            }
            else
            {
                // we save the formatter and set the requestFormatter.Formatter later
                this.lazyFormatter = formatter;
            }
        }

        internal void SetReplyCorrelationQuery(CorrelationQuery replyQuery)
        {
            Fx.Assert(replyQuery != null, "replyQuery cannot be null!");

            if (this.internalSend != null && !this.internalSend.ReplyCorrelationQueries.Contains(replyQuery))
            {
                this.internalSend.ReplyCorrelationQueries.Add(replyQuery);
            }
            else
            {
                // we save the CorrelationQuery and add it to InternalSendMessage later
                if (this.lazyCorrelationQueries == null)
                {
                    this.lazyCorrelationQueries = new List<CorrelationQuery>();
                }
                this.lazyCorrelationQueries.Add(replyQuery);
            }
        }

        internal void InitializeChannelCacheEnabledSetting(ActivityContext context)
        {
            if (!this.channelCacheEnabled.HasValue)
            {
                SendMessageChannelCache channelCacheExtension = context.GetExtension<SendMessageChannelCache>();
                Fx.Assert(channelCacheExtension != null, "channelCacheExtension must exist!");

                InitializeChannelCacheEnabledSetting(channelCacheExtension);
            }
        }

        internal void InitializeChannelCacheEnabledSetting(SendMessageChannelCache channelCacheExtension)
        {
            Fx.Assert(channelCacheExtension != null, "channelCacheExtension cannot be null!");

            ChannelCacheSettings factorySettings = channelCacheExtension.FactorySettings;
            Fx.Assert(factorySettings != null, "FactorySettings cannot be null!");

            bool enabled;

            if (factorySettings.IdleTimeout == TimeSpan.Zero || factorySettings.LeaseTimeout == TimeSpan.Zero || factorySettings.MaxItemsInCache == 0)
            {
                enabled = false;
            }
            else
            {
                enabled = true;
            }

            if (!this.channelCacheEnabled.HasValue)
            {
                this.channelCacheEnabled = enabled;
            }
            else
            {
                Fx.Assert(this.channelCacheEnabled.Value == enabled, "Once ChannelCacheEnabled is set, it cannot be changed!");
            }
        }
    }
}