Repository navigation
Expand file tree
/
Copy pathMessageQueue.cs
More file actions
4590 lines (3975 loc) · 189 KB
/
Copy pathMessageQueue.cs
File metadata and controls
4590 lines (3975 loc) · 189 KB
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// <copyright file="MessageQueue.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Messaging
{
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Diagnostics;
using System;
using System.Configuration.Install;
using System.Messaging.Interop;
using Microsoft.Win32;
using System.ComponentModel.Design;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Security.Permissions;
using System.DirectoryServices;
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue"]/*' />
/// <devdoc>
/// <para>
/// Provides
/// access to a Message Queuing backend queue resource.
/// </para>
/// </devdoc>
[DefaultEvent("ReceiveCompleted"),
TypeConverterAttribute(typeof(System.Messaging.Design.MessageQueueConverter)),
Editor("System.Messaging.Design.QueuePathEditor", "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing),
InstallerTypeAttribute(typeof(MessageQueueInstaller)),
MessagingDescription(Res.MessageQueueDesc)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")]
public class MessageQueue : Component, IEnumerable
{
//Public constants
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.InfiniteTimeout"]/*' />
/// <devdoc>
/// <para>
/// Specifies that
/// there is no
/// timeout period for calls to peek or receive messages.
/// </para>
/// </devdoc>
public static readonly TimeSpan InfiniteTimeout = TimeSpan.FromMilliseconds(UInt32.MaxValue);
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.InfiniteQueueSize"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public static readonly long InfiniteQueueSize = UInt32.MaxValue;
//Internal members
private DefaultPropertiesToSend defaultProperties;
private MessagePropertyFilter receiveFilter;
private QueueAccessMode accessMode;
private int sharedMode;
private string formatName;
private string queuePath;
private string path;
private bool enableCache;
private QueuePropertyVariants properties;
private IMessageFormatter formatter;
// Double-checked locking pattern requires volatile for read/write synchronization
private static volatile string computerName;
internal static readonly Version OSVersion = Environment.OSVersion.Version;
internal static readonly Version WinXP = new Version(5, 1);
internal static readonly bool Msmq3OrNewer = OSVersion >= WinXP;
//Cached properties
private QueuePropertyFilter filter;
private bool authenticate;
private short basePriority;
private DateTime createTime;
private int encryptionLevel;
private Guid id;
private string label;
private string multicastAddress;
private DateTime lastModifyTime;
private long journalSize;
private long queueSize;
private Guid queueType;
private bool useJournaling;
private MQCacheableInfo mqInfo;
// Double-checked locking pattern requires volatile for read/write synchronization
//Async IO support
private volatile bool attached;
private bool useThreadPool;
private AsyncCallback onRequestCompleted;
private PeekCompletedEventHandler onPeekCompleted;
private ReceiveCompletedEventHandler onReceiveCompleted;
private ISynchronizeInvoke synchronizingObject;
// Double-checked locking pattern requires volatile for read/write synchronization
private volatile Hashtable outstandingAsyncRequests;
//Path sufixes
private static readonly string SUFIX_PRIVATE = "\\PRIVATE$";
private static readonly string SUFIX_JOURNAL = "\\JOURNAL$";
private static readonly string SUFIX_DEADLETTER = "\\DEADLETTER$";
private static readonly string SUFIX_DEADXACT = "\\XACTDEADLETTER$";
//Path prefixes
private static readonly string PREFIX_LABEL = "LABEL:";
private static readonly string PREFIX_FORMAT_NAME = "FORMATNAME:";
//Connection pooling support
private static CacheTable<string, string> formatNameCache =
new CacheTable<string, string>("formatNameCache", 4, new TimeSpan(0, 0, 100)); // path -> formatname
private static CacheTable<QueueInfoKeyHolder, MQCacheableInfo> queueInfoCache =
new CacheTable<QueueInfoKeyHolder, MQCacheableInfo>("queue info", 4, new TimeSpan(0, 0, 100)); // <formatname, accessMode> -> <readHandle. writeHandle, isTrans>
// Whidbey Beta 2 SECREVIEW (Dec 2004 eugenesh):
// Connection Cache can be a security vulnerability (see bug 422227)
// Therefore, disable it by default
private static bool enableConnectionCache = false;
// Double-checked locking pattern requires volatile for read/write synchronization
private volatile QueueInfoKeyHolder queueInfoKey = null;
//Code Acess Security support
private bool administerGranted;
private bool browseGranted;
private bool sendGranted;
private bool receiveGranted;
private bool peekGranted;
private object syncRoot = new object();
private static object staticSyncRoot = new object();
static MessageQueue()
{
try
{
using (TelemetryEventSource eventSource = new TelemetryEventSource())
{
eventSource.MessageQueue();
}
}
catch
{
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.MessageQueue"]/*' />
/// <devdoc>
/// <para>
/// Initializes a new instance of the <see cref='System.Messaging.MessageQueue'/> class. To use the object instantiated by the default
/// constructor, the <see cref='System.Messaging.MessageQueue.Path'/>
/// property must be set.
/// </para>
/// </devdoc>
//
public MessageQueue()
{
this.path = String.Empty;
this.accessMode = QueueAccessMode.SendAndReceive;
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.MessageQueue1"]/*' />
/// <devdoc>
/// <para>
/// Initializes a new instance of the <see cref='System.Messaging.MessageQueue'/>
/// class that references the Message Queuing application resource specified by the
/// <paramref name="path"/>
/// parameter.
/// </para>
/// </devdoc>
public MessageQueue(string path)
: this(path, false, enableConnectionCache)
{
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.MessageQueue5"]/*' />
/// <devdoc>
/// <para>
/// Initializes a new instance of the <see cref='System.Messaging.MessageQueue'/>
/// class that references the Message Queuing application resource specified by the
/// <paramref name="path"/> parameter and having the specifed access mode.
/// </para>
/// </devdoc>
public MessageQueue(string path, QueueAccessMode accessMode)
: this(path, false, enableConnectionCache, accessMode)
{
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.MessageQueue2"]/*' />
/// <devdoc>
/// <para>
/// Initializes a new instance of the <see cref='System.Messaging.MessageQueue'/> class that references the
/// Message Queuing application resource specified by the <paramref name="path"/> parameter,
/// and has the specified queue read access restriction.
/// </para>
/// </devdoc>
public MessageQueue(string path, bool sharedModeDenyReceive)
: this(path, sharedModeDenyReceive, enableConnectionCache)
{
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.MessageQueue3"]/*' />
/// <devdoc>
/// <para>
/// Initializes a new instance of the <see cref='System.Messaging.MessageQueue'/> class that references the
/// Message Queuing application resource specified by the <paramref name="path"/> parameter,
/// has the specified queue read access restriction and whether to cache handles
/// </para>
/// </devdoc>
public MessageQueue(string path, bool sharedModeDenyReceive, bool enableCache)
{
this.path = path;
this.enableCache = enableCache;
if (sharedModeDenyReceive)
{
this.sharedMode = NativeMethods.QUEUE_SHARED_MODE_DENY_RECEIVE;
}
this.accessMode = QueueAccessMode.SendAndReceive;
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.MessageQueue4"]/*' />
/// <devdoc>
/// <para>
/// Initializes a new instance of the <see cref='System.Messaging.MessageQueue'/> class that references the
/// Message Queuing application resource specified by the <paramref name="path"/> parameter,
/// has the specified queue read access restriction, whether to cache handles,
/// and specified access mode.
/// </para>
/// </devdoc>
public MessageQueue(string path, bool sharedModeDenyReceive,
bool enableCache, QueueAccessMode accessMode)
{
this.path = path;
this.enableCache = enableCache;
if (sharedModeDenyReceive)
{
this.sharedMode = NativeMethods.QUEUE_SHARED_MODE_DENY_RECEIVE;
}
SetAccessMode(accessMode);
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.MessageQueue3"]/*' />
/// <internalonly/>
internal MessageQueue(string path, Guid id)
{
PropertyFilter.Id = true;
this.id = id;
this.path = path;
this.accessMode = QueueAccessMode.SendAndReceive;
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.AccessMode"]/*' />
/// <devdoc>
/// <para>
/// Gets value specifying access mode of the queue
/// </para>
/// </devdoc>
public QueueAccessMode AccessMode
{
get
{
return this.accessMode;
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.Authenticate"]/*' />
/// <devdoc>
/// <para>
/// Gets or sets a value specifying whether the queue only accepts authenticated
/// messages.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MQ_Authenticate)]
public bool Authenticate
{
get
{
if (!PropertyFilter.Authenticate)
{
Properties.SetUI1(NativeMethods.QUEUE_PROPID_AUTHENTICATE, (byte)0);
GenerateQueueProperties();
this.authenticate = (Properties.GetUI1(NativeMethods.QUEUE_PROPID_AUTHENTICATE) != NativeMethods.QUEUE_AUTHENTICATE_NONE);
PropertyFilter.Authenticate = true;
Properties.Remove(NativeMethods.QUEUE_PROPID_AUTHENTICATE);
}
return this.authenticate;
}
set
{
if (value)
Properties.SetUI1(NativeMethods.QUEUE_PROPID_AUTHENTICATE, (byte)NativeMethods.QUEUE_AUTHENTICATE_AUTHENTICATE);
else
Properties.SetUI1(NativeMethods.QUEUE_PROPID_AUTHENTICATE, (byte)NativeMethods.QUEUE_AUTHENTICATE_NONE);
SaveQueueProperties();
this.authenticate = value;
PropertyFilter.Authenticate = true;
Properties.Remove(NativeMethods.QUEUE_PROPID_AUTHENTICATE);
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.BasePriority"]/*' />
/// <devdoc>
/// <para>Gets or sets a value indicating the base
/// priority used to route a public queue's messages over the network.</para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MQ_BasePriority)]
public short BasePriority
{
get
{
if (!PropertyFilter.BasePriority)
{
Properties.SetI2(NativeMethods.QUEUE_PROPID_BASEPRIORITY, (short)0);
GenerateQueueProperties();
this.basePriority = properties.GetI2(NativeMethods.QUEUE_PROPID_BASEPRIORITY);
PropertyFilter.BasePriority = true;
Properties.Remove(NativeMethods.QUEUE_PROPID_BASEPRIORITY);
}
return this.basePriority;
}
set
{
Properties.SetI2(NativeMethods.QUEUE_PROPID_BASEPRIORITY, value);
SaveQueueProperties();
this.basePriority = value;
PropertyFilter.BasePriority = true;
Properties.Remove(NativeMethods.QUEUE_PROPID_BASEPRIORITY);
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.CanRead"]/*' />
/// <devdoc>
/// <para>
/// Gets a value indicating whether the <see cref='System.Messaging.MessageQueue'/>
/// has read permission.
/// </para>
/// </devdoc>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MQ_CanRead)]
public bool CanRead
{
get
{
if (!browseGranted)
{
MessageQueuePermission permission = new MessageQueuePermission(MessageQueuePermissionAccess.Browse, PREFIX_FORMAT_NAME + this.FormatName);
permission.Demand();
browseGranted = true;
}
return MQInfo.CanRead;
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.CanWrite"]/*' />
/// <devdoc>
/// <para>
/// Gets a value indicating whether the <see cref='System.Messaging.MessageQueue'/>
/// has write permission.
/// </para>
/// </devdoc>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MQ_CanWrite)]
public bool CanWrite
{
get
{
if (!browseGranted)
{
MessageQueuePermission permission = new MessageQueuePermission(MessageQueuePermissionAccess.Browse, PREFIX_FORMAT_NAME + this.FormatName);
permission.Demand();
browseGranted = true;
}
return MQInfo.CanWrite;
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.Category"]/*' />
/// <devdoc>
/// <para>Gets or sets the queue type.</para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MQ_Category)]
public Guid Category
{
get
{
if (!PropertyFilter.Category)
{
Properties.SetNull(NativeMethods.QUEUE_PROPID_TYPE);
GenerateQueueProperties();
byte[] bytes = new byte[16];
IntPtr handle = Properties.GetIntPtr(NativeMethods.QUEUE_PROPID_TYPE);
if (handle != IntPtr.Zero)
{
Marshal.Copy(handle, bytes, 0, 16);
//MSMQ allocated memory for this operation, needs to be freed
SafeNativeMethods.MQFreeMemory(handle);
}
this.queueType = new Guid(bytes);
PropertyFilter.Category = true;
Properties.Remove(NativeMethods.QUEUE_PROPID_TYPE);
}
return this.queueType;
}
set
{
Properties.SetGuid(NativeMethods.QUEUE_PROPID_TYPE, value.ToByteArray());
SaveQueueProperties();
this.queueType = value;
PropertyFilter.Category = true;
Properties.Remove(NativeMethods.QUEUE_PROPID_TYPE);
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.ComputerName"]/*' />
/// <internalonly/>
internal static string ComputerName
{
get
{
if (computerName == null)
{
lock (MessageQueue.staticSyncRoot)
{
if (computerName == null)
{
StringBuilder sb = new StringBuilder(256);
SafeNativeMethods.GetComputerName(sb, new int[] { sb.Capacity });
computerName = sb.ToString();
}
}
}
return computerName;
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.CreateTime"]/*' />
/// <devdoc>
/// <para>
/// Gets the time and date of the queue's creation.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MQ_CreateTime)]
public DateTime CreateTime
{
get
{
if (!PropertyFilter.CreateTime)
{
DateTime time = new DateTime(1970, 1, 1);
Properties.SetI4(NativeMethods.QUEUE_PROPID_CREATE_TIME, 0);
GenerateQueueProperties();
this.createTime = time.AddSeconds(properties.GetI4(NativeMethods.QUEUE_PROPID_CREATE_TIME)).ToLocalTime();
PropertyFilter.CreateTime = true;
Properties.Remove(NativeMethods.QUEUE_PROPID_CREATE_TIME);
}
return this.createTime;
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.DefaultPropertiesToSend"]/*' />
/// <devdoc>
/// <para>
/// Gets or sets the properties to be used by
/// default when sending messages to the queue referenced by this <see cref='System.Messaging.MessageQueue'/>
/// .
/// </para>
/// </devdoc>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), MessagingDescription(Res.MQ_DefaultPropertiesToSend)]
public DefaultPropertiesToSend DefaultPropertiesToSend
{
get
{
if (this.defaultProperties == null)
{
if (this.DesignMode)
this.defaultProperties = new DefaultPropertiesToSend(true);
else
this.defaultProperties = new DefaultPropertiesToSend();
}
return this.defaultProperties;
}
set
{
this.defaultProperties = value;
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.DenySharedReceive"]/*' />
/// <devdoc>
/// <para>
/// Specifies the shared mode for the queue that this object
/// references. If <see langword='true'/> ,
/// no other queue object will be able to receive messages from the queue resource.
/// </para>
/// </devdoc>
[Browsable(false), DefaultValueAttribute(false), MessagingDescription(Res.MQ_DenySharedReceive)]
public bool DenySharedReceive
{
get
{
return (this.sharedMode == NativeMethods.QUEUE_SHARED_MODE_DENY_RECEIVE);
}
set
{
if (value && (this.sharedMode != NativeMethods.QUEUE_SHARED_MODE_DENY_RECEIVE))
{
this.Close();
this.sharedMode = NativeMethods.QUEUE_SHARED_MODE_DENY_RECEIVE;
}
else if (!value && (this.sharedMode == NativeMethods.QUEUE_SHARED_MODE_DENY_RECEIVE))
{
this.Close();
this.sharedMode = NativeMethods.QUEUE_SHARED_MODE_DENY_NONE;
}
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.EnableConnectionCache"]/*' />
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
[Browsable(false)]
public static bool EnableConnectionCache
{
get
{
return enableConnectionCache;
}
set
{
enableConnectionCache = value;
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.EncryptionRequired"]/*' />
/// <devdoc>
/// <para>
/// Gets or sets a value indicating whether the queue only accepts non-private
/// (non-encrypted) messages.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MQ_EncryptionRequired)]
public EncryptionRequired EncryptionRequired
{
get
{
if (!PropertyFilter.EncryptionLevel)
{
Properties.SetUI4(NativeMethods.QUEUE_PROPID_PRIV_LEVEL, 0);
GenerateQueueProperties();
this.encryptionLevel = Properties.GetUI4(NativeMethods.QUEUE_PROPID_PRIV_LEVEL);
PropertyFilter.EncryptionLevel = true;
Properties.Remove(NativeMethods.QUEUE_PROPID_PRIV_LEVEL);
}
return (EncryptionRequired)this.encryptionLevel;
}
set
{
if (!ValidationUtility.ValidateEncryptionRequired(value))
throw new InvalidEnumArgumentException("value", (int)value, typeof(EncryptionRequired));
Properties.SetUI4(NativeMethods.QUEUE_PROPID_PRIV_LEVEL, (int)value);
SaveQueueProperties();
this.encryptionLevel = properties.GetUI4(NativeMethods.QUEUE_PROPID_PRIV_LEVEL);
PropertyFilter.EncryptionLevel = true;
Properties.Remove(NativeMethods.QUEUE_PROPID_PRIV_LEVEL);
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.FormatName"]/*' />
/// <devdoc>
/// <para>
/// Gets the unique name that was generated for the queue when the queue was created.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MQ_FormatName)]
public string FormatName
{
get
{
if (this.formatName == null)
{
if (this.path == null || path.Length == 0)
{
return string.Empty;
}
string pathUpper = this.path.ToUpper(CultureInfo.InvariantCulture);
// see if we already have this cached
if (enableCache)
this.formatName = MessageQueue.formatNameCache.Get(pathUpper);
// not in the cache? keep working.
if (formatName == null)
{
if (PropertyFilter.Id)
{
//Improves performance when enumerating queues.
//This codepath will only be executed when accessing
//a queue returned by MessageQueueEnumerator.
int result;
int status = 0;
StringBuilder newFormatName = new StringBuilder(NativeMethods.MAX_LABEL_LEN);
result = NativeMethods.MAX_LABEL_LEN;
status = SafeNativeMethods.MQInstanceToFormatName(this.id.ToByteArray(), newFormatName, ref result);
if (status != 0)
throw new MessageQueueException(status);
this.formatName = newFormatName.ToString();
return this.formatName;
}
if (pathUpper.StartsWith(PREFIX_FORMAT_NAME))
{
this.formatName = this.path.Substring(PREFIX_FORMAT_NAME.Length);
}
else if (pathUpper.StartsWith(PREFIX_LABEL))
{
MessageQueue labeledQueue = ResolveQueueFromLabel(this.path, true);
this.formatName = labeledQueue.FormatName;
this.queuePath = labeledQueue.QueuePath;
}
else
{
this.queuePath = this.path;
this.formatName = ResolveFormatNameFromQueuePath(this.queuePath, true);
}
MessageQueue.formatNameCache.Put(pathUpper, formatName);
}
}
return this.formatName;
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.Formatter"]/*' />
/// <devdoc>
/// <para>
/// Gets or
/// sets a
/// formatter class used to serialize messages read or written to
/// the message body.
/// </para>
/// </devdoc>
[DefaultValueAttribute(null),
TypeConverterAttribute(typeof(System.Messaging.Design.MessageFormatterConverter)),
Browsable(false),
MessagingDescription(Res.MQ_Formatter)]
public IMessageFormatter Formatter
{
get
{
if (this.formatter == null && !DesignMode)
this.formatter = new XmlMessageFormatter();
return this.formatter;
}
set
{
this.formatter = value;
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.Id"]/*' />
/// <devdoc>
/// <para>
/// Gets the Message Queuing unique identifier for the queue.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MQ_GuidId)]
public Guid Id
{
get
{
if (!PropertyFilter.Id)
{
Properties.SetNull(NativeMethods.QUEUE_PROPID_INSTANCE);
GenerateQueueProperties();
byte[] bytes = new byte[16];
IntPtr handle = Properties.GetIntPtr(NativeMethods.QUEUE_PROPID_INSTANCE);
if (handle != IntPtr.Zero)
{
Marshal.Copy(handle, bytes, 0, 16);
//MSMQ allocated memory for this operation, needs to be freed
SafeNativeMethods.MQFreeMemory(handle);
}
this.id = new Guid(bytes);
PropertyFilter.Id = true;
Properties.Remove(NativeMethods.QUEUE_PROPID_INSTANCE);
}
return this.id;
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.Label"]/*' />
/// <devdoc>
/// <para>Gets or sets the queue description.</para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MQ_Label)]
public string Label
{
get
{
if (!PropertyFilter.Label)
{
Properties.SetNull(NativeMethods.QUEUE_PROPID_LABEL);
GenerateQueueProperties();
string description = null;
IntPtr handle = Properties.GetIntPtr(NativeMethods.QUEUE_PROPID_LABEL);
if (handle != IntPtr.Zero)
{
//Using Unicode API even on Win9x
description = Marshal.PtrToStringUni(handle);
//MSMQ allocated memory for this operation, needs to be freed
SafeNativeMethods.MQFreeMemory(handle);
}
this.label = description;
PropertyFilter.Label = true;
Properties.Remove(NativeMethods.QUEUE_PROPID_LABEL);
}
return this.label;
}
set
{
if (value == null)
throw new ArgumentNullException("value");
//Borrow this function from message
Properties.SetString(NativeMethods.QUEUE_PROPID_LABEL, Message.StringToBytes(value));
SaveQueueProperties();
this.label = value;
PropertyFilter.Label = true;
Properties.Remove(NativeMethods.QUEUE_PROPID_LABEL);
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.LastModifyTime"]/*' />
/// <devdoc>
/// <para>
/// Indicates the last time the properties of a queue were modified.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MQ_LastModifyTime)]
public DateTime LastModifyTime
{
get
{
if (!PropertyFilter.LastModifyTime)
{
DateTime time = new DateTime(1970, 1, 1);
Properties.SetI4(NativeMethods.QUEUE_PROPID_MODIFY_TIME, 0);
GenerateQueueProperties();
this.lastModifyTime = time.AddSeconds(properties.GetI4(NativeMethods.QUEUE_PROPID_MODIFY_TIME)).ToLocalTime();
PropertyFilter.LastModifyTime = true;
Properties.Remove(NativeMethods.QUEUE_PROPID_MODIFY_TIME);
}
return this.lastModifyTime;
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.MachineName"]/*' />
/// <devdoc>
/// <para>
/// Gets or sets the name of the computer where
/// the queue referenced by this <see cref='System.Messaging.MessageQueue'/>
/// is located.
/// </para>
/// </devdoc>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MQ_MachineName)]
public string MachineName
{
get
{
string queuePath = QueuePath;
if (queuePath.Length == 0)
{
return queuePath;
}
return queuePath.Substring(0, queuePath.IndexOf('\\'));
}
set
{
if (value == null)
throw new ArgumentNullException("value");
if (!SyntaxCheck.CheckMachineName(value))
throw new ArgumentException(Res.GetString(Res.InvalidProperty, "MachineName", value));
StringBuilder newPath = new StringBuilder();
if ((this.path == null || this.path.Length == 0) && this.formatName == null)
{
//Need to default to an existing queue, for instance Journal.
newPath.Append(value);
newPath.Append(SUFIX_JOURNAL);
}
else
{
newPath.Append(value);
newPath.Append("\\");
newPath.Append(QueueName);
}
Path = newPath.ToString();
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.MaximumJournalSize"]/*' />
/// <devdoc>
/// <para>
/// Gets or sets the maximum size of the journal queue.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
MessagingDescription(Res.MQ_MaximumJournalSize),
TypeConverterAttribute(typeof(System.Messaging.Design.SizeConverter))]
public long MaximumJournalSize
{
get
{
if (!PropertyFilter.MaximumJournalSize)
{
Properties.SetUI4(NativeMethods.QUEUE_PROPID_JOURNAL_QUOTA, 0);
GenerateQueueProperties();
this.journalSize = (long)((uint)properties.GetUI4(NativeMethods.QUEUE_PROPID_JOURNAL_QUOTA));
PropertyFilter.MaximumJournalSize = true;
Properties.Remove(NativeMethods.QUEUE_PROPID_JOURNAL_QUOTA);
}
return this.journalSize;
}
set
{
if (value > InfiniteQueueSize || value < 0)
throw new ArgumentException(Res.GetString(Res.InvalidProperty, "MaximumJournalSize", value));
Properties.SetUI4(NativeMethods.QUEUE_PROPID_JOURNAL_QUOTA, (int)((uint)value));
SaveQueueProperties();
this.journalSize = value;
PropertyFilter.MaximumJournalSize = true;
Properties.Remove(NativeMethods.QUEUE_PROPID_JOURNAL_QUOTA);
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.MaximumQueueSize"]/*' />
/// <devdoc>
/// <para>
/// Gets or sets the maximum size of the queue.
/// </para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
MessagingDescription(Res.MQ_MaximumQueueSize),
TypeConverterAttribute(typeof(System.Messaging.Design.SizeConverter))]
public long MaximumQueueSize
{
get
{
if (!PropertyFilter.MaximumQueueSize)
{
Properties.SetUI4(NativeMethods.QUEUE_PROPID_QUOTA, 0);
GenerateQueueProperties();
this.queueSize = (long)((uint)properties.GetUI4(NativeMethods.QUEUE_PROPID_QUOTA));
PropertyFilter.MaximumQueueSize = true;
Properties.Remove(NativeMethods.QUEUE_PROPID_QUOTA);
}
return this.queueSize;
}
set
{
if (value > InfiniteQueueSize || value < 0)
throw new ArgumentException(Res.GetString(Res.InvalidProperty, "MaximumQueueSize", value));
Properties.SetUI4(NativeMethods.QUEUE_PROPID_QUOTA, (int)((uint)value));
SaveQueueProperties();
this.queueSize = value;
PropertyFilter.MaximumQueueSize = true;
Properties.Remove(NativeMethods.QUEUE_PROPID_QUOTA);
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.MessageReadPropertyFilter"]/*' />
/// <devdoc>
/// <para>
/// Gets or sets the property filter for
/// receiving messages.
/// </para>
/// </devdoc>
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), MessagingDescription(Res.MQ_MessageReadPropertyFilter)]
public MessagePropertyFilter MessageReadPropertyFilter
{
get
{
if (this.receiveFilter == null)
{
this.receiveFilter = new MessagePropertyFilter();
this.receiveFilter.SetDefaults();
}
return this.receiveFilter;
}
set
{
if (value == null)
throw new ArgumentNullException("value");
this.receiveFilter = value;
}
}
internal MQCacheableInfo MQInfo
{
get
{
if (mqInfo == null)
{
MQCacheableInfo cachedInfo = queueInfoCache.Get(QueueInfoKey);
if (sharedMode == NativeMethods.QUEUE_SHARED_MODE_DENY_RECEIVE || !enableCache)
{
if (cachedInfo != null)
cachedInfo.CloseIfNotReferenced();
// don't use the cache
mqInfo = new MQCacheableInfo(this.FormatName, accessMode, sharedMode);
mqInfo.AddRef();
}
else
{
// use the cache
if (cachedInfo != null)
{
cachedInfo.AddRef();
mqInfo = cachedInfo;
}
else
{
mqInfo = new MQCacheableInfo(this.FormatName, accessMode, sharedMode);
mqInfo.AddRef();
queueInfoCache.Put(QueueInfoKey, mqInfo);
}
}
}
return mqInfo;
}
}
/// <include file='doc\MessageQueue.uex' path='docs/doc[@for="MessageQueue.MulticastAddress"]/*' />
/// <devdoc>
/// <para>Gets or sets the IP multicast address associated with the queue.</para>
/// </devdoc>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
DefaultValue(""),
MessagingDescription(Res.MQ_MulticastAddress)]