Message.cs 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433
  1. //------------------------------------------------------------------------------
  2. // <copyright file="Message.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. //------------------------------------------------------------------------------
  6. using Experimental.System.Messaging.Interop;
  7. using System;
  8. using System.ComponentModel;
  9. using System.Diagnostics;
  10. using System.Diagnostics.CodeAnalysis;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Runtime.InteropServices;
  14. using System.Text;
  15. namespace Experimental.System.Messaging
  16. {
  17. /// <include file='doc\Message.uex' path='docs/doc[@for="Message"]/*' />
  18. /// <devdoc>
  19. /// <para>
  20. /// Provides access to the properties needed to define a
  21. /// Message Queuing message.
  22. /// </para>
  23. /// </devdoc>
  24. [Designer("System.Messaging.Design.MessageDesigner, " + AssemblyRef.SystemDesign)]
  25. public class Message : Component
  26. {
  27. private const int GenericIdSize = 16;
  28. private const int MessageIdSize = 20;
  29. private const int DefaultQueueNameSize = 255;
  30. private const int DefaultCryptographicProviderNameSize = 255;
  31. private const int DefaultDigitalSignatureSize = 255;
  32. private const int DefaultSenderCertificateSize = 255;
  33. private const int DefaultSenderIdSize = 255;
  34. private const int DefaultSymmetricKeySize = 255;
  35. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.InfiniteTimeout"]/*' />
  36. /// <devdoc>
  37. /// <para>
  38. /// Specifies that there is no timeout.
  39. /// </para>
  40. /// </devdoc>
  41. public static readonly TimeSpan InfiniteTimeout = TimeSpan.FromSeconds(UInt32.MaxValue);
  42. private MessagePropertyFilter filter;
  43. private string machineName;
  44. private bool receiveCreated;
  45. private object cachedBodyObject;
  46. private Stream cachedBodyStream;
  47. private IMessageFormatter cachedFormatter;
  48. private MessageQueue cachedResponseQueue;
  49. private MessageQueue cachedTransactionStatusQueue;
  50. private MessageQueue cachedAdminQueue;
  51. private MessageQueue cachedDestinationQueue;
  52. internal MessagePropertyVariants properties;
  53. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.Message"]/*' />
  54. /// <devdoc>
  55. /// <para>
  56. /// Initializes a new instance of the <see cref='System.Messaging.Message'/> class with an empty body.
  57. /// </para>
  58. /// </devdoc>
  59. public Message()
  60. {
  61. properties = new MessagePropertyVariants();
  62. receiveCreated = false;
  63. this.filter = new MessagePropertyFilter();
  64. //Always add Id
  65. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_MSGID, new byte[MessageIdSize]);
  66. this.filter.Id = true;
  67. }
  68. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.Message1"]/*' />
  69. /// <devdoc>
  70. /// <para>
  71. /// Initializes a new instance of the <see cref='System.Messaging.Message'/>
  72. /// class, serializing the object passed as
  73. /// an argument.
  74. /// </para>
  75. /// </devdoc>
  76. public Message(object body)
  77. : this()
  78. {
  79. this.Body = body;
  80. }
  81. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.Message2"]/*' />
  82. /// <devdoc>
  83. /// <para>[To be supplied.]</para>
  84. /// </devdoc>
  85. public Message(object body, IMessageFormatter formatter)
  86. : this()
  87. {
  88. this.Formatter = formatter;
  89. this.Body = body;
  90. }
  91. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.Message3"]/*' />
  92. /// <internalonly/>
  93. internal Message(MessagePropertyFilter filter)
  94. {
  95. properties = new MessagePropertyVariants();
  96. receiveCreated = true;
  97. this.filter = filter;
  98. if (filter.data1 != 0)
  99. {
  100. int data = filter.data1;
  101. if (0 != (data & MessagePropertyFilter.ACKNOWLEDGEMENT))
  102. properties.SetUI2(NativeMethods.MESSAGE_PROPID_CLASS, (short)0);
  103. if (0 != (data & MessagePropertyFilter.ACKNOWLEDGE_TYPE))
  104. properties.SetUI1(NativeMethods.MESSAGE_PROPID_ACKNOWLEDGE, (byte)0);
  105. if (0 != (data & MessagePropertyFilter.ADMIN_QUEUE))
  106. {
  107. properties.SetString(NativeMethods.MESSAGE_PROPID_ADMIN_QUEUE, new byte[DefaultQueueNameSize * 2]);
  108. properties.SetUI4(NativeMethods.MESSAGE_PROPID_ADMIN_QUEUE_LEN, DefaultQueueNameSize);
  109. }
  110. if (0 != (data & MessagePropertyFilter.BODY))
  111. {
  112. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY, new byte[filter.bodySize]);
  113. properties.SetUI4(NativeMethods.MESSAGE_PROPID_BODY_SIZE, filter.bodySize);
  114. properties.SetUI4(NativeMethods.MESSAGE_PROPID_BODY_TYPE, 0);
  115. }
  116. if (0 != (data & MessagePropertyFilter.LABEL))
  117. {
  118. properties.SetString(NativeMethods.MESSAGE_PROPID_LABEL, new byte[filter.labelSize * 2]);
  119. properties.SetUI4(NativeMethods.MESSAGE_PROPID_LABEL_LEN, filter.labelSize);
  120. }
  121. if (0 != (data & MessagePropertyFilter.ID))
  122. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_MSGID, new byte[MessageIdSize]);
  123. if (0 != (data & MessagePropertyFilter.LOOKUP_ID))
  124. properties.SetUI8(NativeMethods.MESSAGE_PROPID_LOOKUPID, (long)0);
  125. if (0 != (data & MessagePropertyFilter.USE_DEADLETTER_QUEUE))
  126. properties.SetUI1(NativeMethods.MESSAGE_PROPID_JOURNAL, (byte)0);
  127. if (0 != (data & MessagePropertyFilter.RESPONSE_QUEUE))
  128. {
  129. properties.SetString(NativeMethods.MESSAGE_PROPID_RESP_QUEUE, new byte[DefaultQueueNameSize * 2]);
  130. properties.SetUI4(NativeMethods.MESSAGE_PROPID_RESP_QUEUE_LEN, DefaultQueueNameSize);
  131. }
  132. //Acknowledgment and MessageType are overloaded in MQ.
  133. if ((0 == (data & MessagePropertyFilter.ACKNOWLEDGEMENT)) && (0 != (data & MessagePropertyFilter.MESSAGE_TYPE)))
  134. properties.SetUI2(NativeMethods.MESSAGE_PROPID_CLASS, (short)0);
  135. //Journaling and Deadletter are overloaded in MSMQ
  136. if ((0 == (data & MessagePropertyFilter.USE_DEADLETTER_QUEUE)) && (0 != (data & MessagePropertyFilter.USE_JOURNALING)))
  137. properties.SetUI1(NativeMethods.MESSAGE_PROPID_JOURNAL, (byte)0);
  138. }
  139. if (filter.data2 != 0)
  140. {
  141. int data = filter.data2;
  142. if (0 != (data & MessagePropertyFilter.APP_SPECIFIC))
  143. properties.SetUI4(NativeMethods.MESSAGE_PROPID_APPSPECIFIC, 0);
  144. if (0 != (data & MessagePropertyFilter.ARRIVED_TIME))
  145. properties.SetUI4(NativeMethods.MESSAGE_PROPID_ARRIVEDTIME, 0);
  146. if (0 != (data & MessagePropertyFilter.ATTACH_SENDER_ID))
  147. properties.SetUI4(NativeMethods.MESSAGE_PROPID_SENDERID_TYPE, 0);
  148. if (0 != (data & MessagePropertyFilter.AUTHENTICATED))
  149. properties.SetUI1(NativeMethods.MESSAGE_PROPID_AUTHENTICATED, (byte)0);
  150. if (0 != (data & MessagePropertyFilter.CONNECTOR_TYPE))
  151. properties.SetGuid(NativeMethods.MESSAGE_PROPID_CONNECTOR_TYPE, new byte[GenericIdSize]);
  152. if (0 != (data & MessagePropertyFilter.CORRELATION_ID))
  153. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_CORRELATIONID, new byte[MessageIdSize]);
  154. if (0 != (data & MessagePropertyFilter.CRYPTOGRAPHIC_PROVIDER_NAME))
  155. {
  156. properties.SetString(NativeMethods.MESSAGE_PROPID_PROV_NAME, new byte[DefaultCryptographicProviderNameSize * 2]);
  157. properties.SetUI4(NativeMethods.MESSAGE_PROPID_PROV_NAME_LEN, DefaultCryptographicProviderNameSize);
  158. }
  159. if (0 != (data & MessagePropertyFilter.CRYPTOGRAPHIC_PROVIDER_TYPE))
  160. properties.SetUI4(NativeMethods.MESSAGE_PROPID_PROV_TYPE, 0);
  161. if (0 != (data & MessagePropertyFilter.IS_RECOVERABLE))
  162. properties.SetUI1(NativeMethods.MESSAGE_PROPID_DELIVERY, (byte)0);
  163. if (0 != (data & MessagePropertyFilter.DESTINATION_QUEUE))
  164. {
  165. properties.SetString(NativeMethods.MESSAGE_PROPID_DEST_QUEUE, new byte[DefaultQueueNameSize * 2]);
  166. properties.SetUI4(NativeMethods.MESSAGE_PROPID_DEST_QUEUE_LEN, DefaultQueueNameSize);
  167. }
  168. if (0 != (data & MessagePropertyFilter.DIGITAL_SIGNATURE))
  169. {
  170. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_SIGNATURE, new byte[DefaultDigitalSignatureSize]);
  171. properties.SetUI4(NativeMethods.MESSAGE_PROPID_SIGNATURE_LEN, DefaultDigitalSignatureSize);
  172. }
  173. if (0 != (data & MessagePropertyFilter.ENCRYPTION_ALGORITHM))
  174. properties.SetUI4(NativeMethods.MESSAGE_PROPID_ENCRYPTION_ALG, 0);
  175. if (0 != (data & MessagePropertyFilter.EXTENSION))
  176. {
  177. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_EXTENSION, new byte[filter.extensionSize]);
  178. properties.SetUI4(NativeMethods.MESSAGE_PROPID_EXTENSION_LEN, filter.extensionSize);
  179. }
  180. if (0 != (data & MessagePropertyFilter.FOREIGN_ADMIN_QUEUE))
  181. {
  182. properties.SetString(NativeMethods.MESSAGE_PROPID_XACT_STATUS_QUEUE, new byte[DefaultQueueNameSize * 2]);
  183. properties.SetUI4(NativeMethods.MESSAGE_PROPID_XACT_STATUS_QUEUE_LEN, DefaultQueueNameSize);
  184. }
  185. if (0 != (data & MessagePropertyFilter.HASH_ALGORITHM))
  186. properties.SetUI4(NativeMethods.MESSAGE_PROPID_HASH_ALG, 0);
  187. if (0 != (data & MessagePropertyFilter.IS_FIRST_IN_TRANSACTION))
  188. properties.SetUI1(NativeMethods.MESSAGE_PROPID_FIRST_IN_XACT, (byte)0);
  189. if (0 != (data & MessagePropertyFilter.IS_LAST_IN_TRANSACTION))
  190. properties.SetUI1(NativeMethods.MESSAGE_PROPID_LAST_IN_XACT, (byte)0);
  191. if (0 != (data & MessagePropertyFilter.PRIORITY))
  192. properties.SetUI1(NativeMethods.MESSAGE_PROPID_PRIORITY, (byte)0);
  193. if (0 != (data & MessagePropertyFilter.SENDER_CERTIFICATE))
  194. {
  195. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_SENDER_CERT, new byte[DefaultSenderCertificateSize]);
  196. properties.SetUI4(NativeMethods.MESSAGE_PROPID_SENDER_CERT_LEN, DefaultSenderCertificateSize);
  197. }
  198. if (0 != (data & MessagePropertyFilter.SENDER_ID))
  199. {
  200. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_SENDERID, new byte[DefaultSenderIdSize]);
  201. properties.SetUI4(NativeMethods.MESSAGE_PROPID_SENDERID_LEN, DefaultSenderIdSize);
  202. }
  203. if (0 != (data & MessagePropertyFilter.SENT_TIME))
  204. properties.SetUI4(NativeMethods.MESSAGE_PROPID_SENTTIME, 0);
  205. if (0 != (data & MessagePropertyFilter.SOURCE_MACHINE))
  206. properties.SetGuid(NativeMethods.MESSAGE_PROPID_SRC_MACHINE_ID, new byte[GenericIdSize]);
  207. if (0 != (data & MessagePropertyFilter.SYMMETRIC_KEY))
  208. {
  209. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_DEST_SYMM_KEY, new byte[DefaultSymmetricKeySize]);
  210. properties.SetUI4(NativeMethods.MESSAGE_PROPID_DEST_SYMM_KEY_LEN, DefaultSymmetricKeySize);
  211. }
  212. if (0 != (data & MessagePropertyFilter.TIME_TO_BE_RECEIVED))
  213. properties.SetUI4(NativeMethods.MESSAGE_PROPID_TIME_TO_BE_RECEIVED, 0);
  214. if (0 != (data & MessagePropertyFilter.TIME_TO_REACH_QUEUE))
  215. properties.SetUI4(NativeMethods.MESSAGE_PROPID_TIME_TO_REACH_QUEUE, 0);
  216. if (0 != (data & MessagePropertyFilter.TRANSACTION_ID))
  217. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_XACTID, new byte[MessageIdSize]);
  218. if (0 != (data & MessagePropertyFilter.USE_AUTHENTICATION))
  219. properties.SetUI4(NativeMethods.MESSAGE_PROPID_AUTH_LEVEL, 0);
  220. if (0 != (data & MessagePropertyFilter.USE_ENCRYPTION))
  221. properties.SetUI4(NativeMethods.MESSAGE_PROPID_PRIV_LEVEL, 0);
  222. if (0 != (data & MessagePropertyFilter.USE_TRACING))
  223. properties.SetUI1(NativeMethods.MESSAGE_PROPID_TRACE, (byte)0);
  224. if (0 != (data & MessagePropertyFilter.VERSION))
  225. properties.SetUI4(NativeMethods.MESSAGE_PROPID_VERSION, 0);
  226. }
  227. }
  228. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.Acknowledgment"]/*' />
  229. /// <devdoc>
  230. /// <para>Gets the classification
  231. /// of acknowledgment messages that Message Queuing posts.</para>
  232. /// </devdoc>
  233. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgAcknowledgement)]
  234. public Acknowledgment Acknowledgment
  235. {
  236. get
  237. {
  238. if (!this.filter.Acknowledgment)
  239. {
  240. //This message cannot be an acknowledgment, because it has not been sent.
  241. if (!receiveCreated)
  242. throw new InvalidOperationException(Res.GetString(Res.NotAcknowledgement));
  243. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "Acknowledgment"));
  244. }
  245. //Casting unsigned short to int, mask off sign extension.
  246. int res = ((int)properties.GetUI2(NativeMethods.MESSAGE_PROPID_CLASS)) & 0x0000FFFF;
  247. return (Acknowledgment)res;
  248. }
  249. }
  250. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.AcknowledgeType"]/*' />
  251. /// <devdoc>
  252. /// <para>
  253. /// Gets or sets the type of acknowledgment
  254. /// message requested
  255. /// from
  256. /// Message Queuing when a message arrives in the queue.
  257. /// </para>
  258. /// </devdoc>
  259. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgAcknowledgeType)]
  260. public AcknowledgeTypes AcknowledgeType
  261. {
  262. get
  263. {
  264. if (!this.filter.AcknowledgeType)
  265. {
  266. //Return the default.
  267. if (!receiveCreated)
  268. return AcknowledgeTypes.None;
  269. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "AcknowledgeType"));
  270. }
  271. return (AcknowledgeTypes)this.properties.GetUI1(NativeMethods.MESSAGE_PROPID_ACKNOWLEDGE);
  272. }
  273. set
  274. {
  275. //If default
  276. if (value == AcknowledgeTypes.None)
  277. {
  278. this.filter.AcknowledgeType = false;
  279. properties.Remove(NativeMethods.MESSAGE_PROPID_ACKNOWLEDGE);
  280. }
  281. else
  282. {
  283. this.filter.AcknowledgeType = true;
  284. this.properties.SetUI1(NativeMethods.MESSAGE_PROPID_ACKNOWLEDGE, (byte)value);
  285. }
  286. }
  287. }
  288. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.AdministrationQueue"]/*' />
  289. /// <devdoc>
  290. /// <para>
  291. /// Gets or sets the queue used for acknowledgment
  292. /// messages.
  293. /// </para>
  294. /// </devdoc>
  295. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgAdministrationQueue)]
  296. public MessageQueue AdministrationQueue
  297. {
  298. get
  299. {
  300. if (!this.filter.AdministrationQueue)
  301. {
  302. //This property has not been set, lets return an undefined value.
  303. if (!receiveCreated)
  304. return null;
  305. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "AdministrationQueue"));
  306. }
  307. if (this.cachedAdminQueue == null)
  308. {
  309. if (properties.GetUI4(NativeMethods.MESSAGE_PROPID_ADMIN_QUEUE_LEN) != 0)
  310. {
  311. string queueFormatName = StringFromBytes(properties.GetString(NativeMethods.MESSAGE_PROPID_ADMIN_QUEUE),
  312. properties.GetUI4(NativeMethods.MESSAGE_PROPID_ADMIN_QUEUE_LEN));
  313. this.cachedAdminQueue = new MessageQueue("FORMATNAME:" + queueFormatName);
  314. }
  315. }
  316. return this.cachedAdminQueue;
  317. }
  318. set
  319. {
  320. if (value != null)
  321. this.filter.AdministrationQueue = true;
  322. else
  323. {
  324. //If default
  325. if (this.filter.AdministrationQueue)
  326. {
  327. this.filter.AdministrationQueue = false;
  328. properties.Remove(NativeMethods.MESSAGE_PROPID_ADMIN_QUEUE);
  329. properties.Remove(NativeMethods.MESSAGE_PROPID_ADMIN_QUEUE_LEN);
  330. }
  331. }
  332. this.cachedAdminQueue = value;
  333. }
  334. }
  335. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.AppSpecific"]/*' />
  336. /// <devdoc>
  337. /// <para>
  338. /// Gets or sets
  339. /// application-generated information regarding the message.
  340. /// </para>
  341. /// </devdoc>
  342. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgAppSpecific)]
  343. public int AppSpecific
  344. {
  345. get
  346. {
  347. if (!this.filter.AppSpecific)
  348. {
  349. //Return the default.
  350. if (!receiveCreated)
  351. return 0;
  352. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "AppSpecific"));
  353. }
  354. return properties.GetUI4(NativeMethods.MESSAGE_PROPID_APPSPECIFIC);
  355. }
  356. set
  357. {
  358. //If default
  359. if (value == 0)
  360. {
  361. this.filter.AppSpecific = false;
  362. properties.Remove(NativeMethods.MESSAGE_PROPID_APPSPECIFIC);
  363. }
  364. else
  365. {
  366. this.filter.AppSpecific = true;
  367. properties.SetUI4(NativeMethods.MESSAGE_PROPID_APPSPECIFIC, value);
  368. }
  369. }
  370. }
  371. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.ArrivedTime"]/*' />
  372. /// <devdoc>
  373. /// Indicates when the message arrived at the queue.
  374. /// </devdoc>
  375. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgArrivedTime)]
  376. public DateTime ArrivedTime
  377. {
  378. get
  379. {
  380. if (!this.filter.ArrivedTime)
  381. {
  382. //Undefined at this point, throw an exception.
  383. if (!receiveCreated)
  384. throw new InvalidOperationException(Res.GetString(Res.ArrivedTimeNotSet));
  385. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "ArrivedTime"));
  386. }
  387. //Number of seconds ellapsed since 1/1/1970
  388. DateTime time = new DateTime(1970, 1, 1);
  389. time = time.AddSeconds(properties.GetUI4(NativeMethods.MESSAGE_PROPID_ARRIVEDTIME)).ToLocalTime();
  390. return time;
  391. }
  392. }
  393. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.AttachSenderId"]/*' />
  394. /// <devdoc>
  395. /// <para>
  396. /// Gets or sets a value indicating whether the sender ID is to be attached
  397. /// to the message.
  398. /// </para>
  399. /// </devdoc>
  400. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgAttachSenderId)]
  401. public bool AttachSenderId
  402. {
  403. get
  404. {
  405. if (!this.filter.AttachSenderId)
  406. {
  407. //SenderId is attached by default.
  408. if (!receiveCreated)
  409. return true;
  410. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "AttachSenderId"));
  411. }
  412. int type = properties.GetUI4(NativeMethods.MESSAGE_PROPID_SENDERID_TYPE);
  413. if (type == NativeMethods.MESSAGE_SENDERID_TYPE_NONE)
  414. return false;
  415. return true;
  416. }
  417. set
  418. {
  419. //If default.
  420. if (value)
  421. {
  422. this.filter.AttachSenderId = false;
  423. properties.Remove(NativeMethods.MESSAGE_PROPID_SENDERID_TYPE);
  424. }
  425. else
  426. {
  427. this.filter.AttachSenderId = true;
  428. if (value)
  429. properties.SetUI4(NativeMethods.MESSAGE_PROPID_SENDERID_TYPE, NativeMethods.MESSAGE_SENDERID_TYPE_SID);
  430. else
  431. properties.SetUI4(NativeMethods.MESSAGE_PROPID_SENDERID_TYPE, NativeMethods.MESSAGE_SENDERID_TYPE_NONE);
  432. }
  433. }
  434. }
  435. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.Authenticated"]/*' />
  436. /// <devdoc>
  437. /// <para>
  438. /// Gets a value indicating whether the message was
  439. /// authenticated.
  440. /// </para>
  441. /// </devdoc>
  442. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgAuthenticated)]
  443. public bool Authenticated
  444. {
  445. get
  446. {
  447. if (!this.filter.Authenticated)
  448. {
  449. //Authentication is undefined, there is nothing to return here.
  450. if (!receiveCreated)
  451. throw new InvalidOperationException(Res.GetString(Res.AuthenticationNotSet));
  452. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "Authenticated"));
  453. }
  454. return (properties.GetUI1(NativeMethods.MESSAGE_PROPID_AUTHENTICATED) != 0);
  455. }
  456. }
  457. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.AuthenticationProviderName"]/*' />
  458. /// <devdoc>
  459. /// <para>
  460. /// Gets or sets the name of the cryptographic
  461. /// provider used to generate the digital signature of the message.
  462. /// </para>
  463. /// </devdoc>
  464. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgAuthenticationProviderName)]
  465. public string AuthenticationProviderName
  466. {
  467. get
  468. {
  469. if (!this.filter.AuthenticationProviderName)
  470. {
  471. //Return default
  472. if (!receiveCreated)
  473. return "Microsoft Base Cryptographic Provider, Ver. 1.0";
  474. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "AuthenticationProviderName"));
  475. }
  476. if (this.properties.GetUI4(NativeMethods.MESSAGE_PROPID_PROV_NAME_LEN) != 0)
  477. return StringFromBytes(this.properties.GetString(NativeMethods.MESSAGE_PROPID_PROV_NAME),
  478. properties.GetUI4(NativeMethods.MESSAGE_PROPID_PROV_NAME_LEN));
  479. else
  480. return "";
  481. }
  482. set
  483. {
  484. if (value == null)
  485. throw new ArgumentNullException("value");
  486. //Should not remove if default, the default value might change in future MQ clients
  487. //if (value.CompareTo("Microsoft Base Cryptographic Provider, Ver. 1.0") == 0) {
  488. // this.filter.AuthenticationProviderName = false;
  489. // properties.Remove(NativeMethods.MESSAGE_PROPID_PROV_NAME);
  490. // properties.Remove(NativeMethods.MESSAGE_PROPID_PROV_NAME_LEN);
  491. //}
  492. //else {
  493. this.filter.AuthenticationProviderName = true;
  494. properties.SetString(NativeMethods.MESSAGE_PROPID_PROV_NAME, StringToBytes(value));
  495. properties.SetUI4(NativeMethods.MESSAGE_PROPID_PROV_NAME_LEN, value.Length);
  496. //}
  497. }
  498. }
  499. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.AuthenticationProviderType"]/*' />
  500. /// <devdoc>
  501. /// <para>
  502. /// Gets or sets the type of cryptographic provider used to
  503. /// generate the digital signature of the
  504. /// message.
  505. /// </para>
  506. /// </devdoc>
  507. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgAuthenticationProviderType)]
  508. public CryptographicProviderType AuthenticationProviderType
  509. {
  510. get
  511. {
  512. //Return default
  513. if (!this.filter.AuthenticationProviderType)
  514. {
  515. if (!receiveCreated)
  516. return CryptographicProviderType.RsaFull;
  517. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "AuthenticationProviderType"));
  518. }
  519. return (CryptographicProviderType)properties.GetUI4(NativeMethods.MESSAGE_PROPID_PROV_TYPE);
  520. }
  521. set
  522. {
  523. if (!ValidationUtility.ValidateCryptographicProviderType(value))
  524. throw new InvalidEnumArgumentException("value", (int)value, typeof(CryptographicProviderType));
  525. //Should not remove if default, the default value might change in future MQ clients
  526. //if (value == CryptographicProviderType.RsaFull) {
  527. // this.filter.AuthenticationProviderType = false;
  528. // properties.Remove(NativeMethods.MESSAGE_PROPID_PROV_TYPE);
  529. //}
  530. //else {
  531. this.filter.AuthenticationProviderType = true;
  532. properties.SetUI4(NativeMethods.MESSAGE_PROPID_PROV_TYPE, (int)value);
  533. //}
  534. }
  535. }
  536. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.Body"]/*' />
  537. /// <devdoc>
  538. /// <para>
  539. /// Gets
  540. /// or sets the serialized
  541. /// contents of the message.
  542. /// </para>
  543. /// </devdoc>
  544. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  545. public object Body
  546. {
  547. get
  548. {
  549. if (!this.filter.Body)
  550. {
  551. if (!receiveCreated)
  552. return null;
  553. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "Body"));
  554. }
  555. if (this.cachedBodyObject == null)
  556. {
  557. if (this.Formatter == null)
  558. throw new InvalidOperationException(Res.GetString(Res.FormatterMissing));
  559. this.cachedBodyObject = this.Formatter.Read(this);
  560. }
  561. return this.cachedBodyObject;
  562. }
  563. set
  564. {
  565. this.filter.Body = true;
  566. this.cachedBodyObject = value;
  567. }
  568. }
  569. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.BodyStream"]/*' />
  570. /// <devdoc>
  571. /// <para>
  572. /// Gets or sets the information in the body of
  573. /// the message.
  574. /// </para>
  575. /// </devdoc>
  576. [ReadOnly(true),
  577. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
  578. Editor("System.ComponentModel.Design.BinaryEditor, " + AssemblyRef.SystemDesign, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing),
  579. MessagingDescription(Res.MsgBodyStream)]
  580. public Stream BodyStream
  581. {
  582. get
  583. {
  584. if (!this.filter.Body)
  585. {
  586. if (!receiveCreated)
  587. {
  588. this.filter.Body = true;
  589. if (this.cachedBodyStream == null)
  590. this.cachedBodyStream = new MemoryStream();
  591. return this.cachedBodyStream;
  592. }
  593. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "Body"));
  594. }
  595. if (this.cachedBodyStream == null)
  596. this.cachedBodyStream = new MemoryStream(properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY),
  597. 0, properties.GetUI4(NativeMethods.MESSAGE_PROPID_BODY_SIZE));
  598. return this.cachedBodyStream;
  599. }
  600. set
  601. {
  602. if (value != null)
  603. this.filter.Body = true;
  604. else
  605. {
  606. this.filter.Body = false;
  607. properties.Remove(NativeMethods.MESSAGE_PROPID_BODY);
  608. properties.Remove(NativeMethods.MESSAGE_PROPID_BODY_TYPE);
  609. properties.Remove(NativeMethods.MESSAGE_PROPID_BODY_SIZE);
  610. }
  611. this.cachedBodyStream = value;
  612. }
  613. }
  614. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.BodyType"]/*' />
  615. /// <devdoc>
  616. /// <para>
  617. /// Gets
  618. /// or sets the type of data the message body contains.
  619. /// </para>
  620. /// </devdoc>
  621. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgBodyType)]
  622. public int BodyType
  623. {
  624. get
  625. {
  626. if (!this.filter.Body)
  627. {
  628. //Return default.
  629. if (!receiveCreated)
  630. return 0;
  631. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "Body"));
  632. }
  633. return this.properties.GetUI4(NativeMethods.MESSAGE_PROPID_BODY_TYPE);
  634. }
  635. set
  636. {
  637. properties.SetUI4(NativeMethods.MESSAGE_PROPID_BODY_TYPE, value);
  638. }
  639. }
  640. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.ConnectorType"]/*' />
  641. /// <devdoc>
  642. /// <para>
  643. /// Is required whenever an application sets a message property that is
  644. /// typically set by MSMQ. It is typically used in the following two cases.
  645. /// Whenever a message is passed by a connector application, the connector
  646. /// type is required so that the sending and receiving applications know how
  647. /// to interpret the security and acknowledgment properties of the messages.
  648. /// When sending application-encrypted messages, this property tells the
  649. /// MSMQ run time to use the symmetric key.
  650. /// </para>
  651. /// </devdoc>
  652. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgConnectorType)]
  653. public Guid ConnectorType
  654. {
  655. get
  656. {
  657. if (!this.filter.ConnectorType)
  658. {
  659. //Return default.
  660. if (!receiveCreated)
  661. return Guid.Empty;
  662. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "ConnectorType"));
  663. }
  664. return new Guid(this.properties.GetGuid(NativeMethods.MESSAGE_PROPID_CONNECTOR_TYPE));
  665. }
  666. set
  667. {
  668. //If default
  669. if (value.Equals(Guid.Empty))
  670. {
  671. this.filter.ConnectorType = false;
  672. properties.Remove(NativeMethods.MESSAGE_PROPID_CONNECTOR_TYPE);
  673. }
  674. else
  675. {
  676. this.filter.ConnectorType = true;
  677. properties.SetGuid(NativeMethods.MESSAGE_PROPID_CONNECTOR_TYPE, ((Guid)value).ToByteArray());
  678. }
  679. }
  680. }
  681. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.CorrelationId"]/*' />
  682. /// <devdoc>
  683. /// <para>
  684. /// Gets or sets the message identifier used by
  685. /// acknowledgment and report messages to reference the original
  686. /// message.
  687. /// </para>
  688. /// </devdoc>
  689. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgCorrelationId)]
  690. public string CorrelationId
  691. {
  692. get
  693. {
  694. if (!this.filter.CorrelationId)
  695. {
  696. //Return default
  697. if (!receiveCreated)
  698. return String.Empty;
  699. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "CorrelationId"));
  700. }
  701. return IdFromByteArray(this.properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_CORRELATIONID));
  702. }
  703. set
  704. {
  705. if (value == null)
  706. throw new ArgumentNullException("value");
  707. //If default
  708. if (value.Length == 0)
  709. {
  710. this.filter.CorrelationId = false;
  711. properties.Remove(NativeMethods.MESSAGE_PROPID_CORRELATIONID);
  712. }
  713. else
  714. {
  715. this.filter.CorrelationId = true;
  716. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_CORRELATIONID, IdToByteArray(value));
  717. }
  718. }
  719. }
  720. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.DefaultBodySize"]/*' />
  721. /// <devdoc>
  722. /// The default body buffer size to create,
  723. /// when the message is received.
  724. /// </devdoc>
  725. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  726. private int DefaultBodySize
  727. {
  728. get
  729. {
  730. return this.filter.DefaultBodySize;
  731. }
  732. }
  733. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.DefaultExtensionSize"]/*' />
  734. /// <devdoc>
  735. /// The default extension buffer size to create,
  736. /// when the message is received.
  737. /// </devdoc>
  738. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  739. private int DefaultExtensionSize
  740. {
  741. get
  742. {
  743. return this.filter.DefaultExtensionSize;
  744. }
  745. }
  746. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.DefaultLabelSize"]/*' />
  747. /// <devdoc>
  748. /// The default label buffer size to create,
  749. /// when the message is received.
  750. /// </devdoc>
  751. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  752. private int DefaultLabelSize
  753. {
  754. get
  755. {
  756. return this.filter.DefaultLabelSize;
  757. }
  758. }
  759. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.DestinationQueue"]/*' />
  760. /// <devdoc>
  761. /// <para>
  762. /// Identifies the original destination queue for a message. It is typically
  763. /// used to determine the original destination of a message that is in a journal
  764. /// or dead-letter queue, however it can also be used when sending a
  765. /// response message back to a response queue.
  766. /// </para>
  767. /// </devdoc>
  768. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgDestinationQueue)]
  769. public MessageQueue DestinationQueue
  770. {
  771. get
  772. {
  773. if (!this.filter.DestinationQueue)
  774. {
  775. if (!receiveCreated)
  776. throw new InvalidOperationException(Res.GetString(Res.DestinationQueueNotSet));
  777. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "DestinationQueue"));
  778. }
  779. if (this.cachedDestinationQueue == null)
  780. {
  781. if (this.properties.GetUI4(NativeMethods.MESSAGE_PROPID_DEST_QUEUE_LEN) != 0)
  782. {
  783. string queueFormatName = StringFromBytes(properties.GetString(NativeMethods.MESSAGE_PROPID_DEST_QUEUE),
  784. properties.GetUI4(NativeMethods.MESSAGE_PROPID_DEST_QUEUE_LEN));
  785. this.cachedDestinationQueue = new MessageQueue("FORMATNAME:" + queueFormatName);
  786. }
  787. }
  788. return this.cachedDestinationQueue;
  789. }
  790. }
  791. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.DestinationSymmetricKey"]/*' />
  792. /// <devdoc>
  793. /// <para>
  794. /// Gets
  795. /// or sets the symmetric key used to encrypt messages.
  796. /// </para>
  797. /// </devdoc>
  798. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
  799. MessagingDescription(Res.MsgDestinationSymmetricKey)]
  800. [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
  801. [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
  802. public byte[] DestinationSymmetricKey
  803. {
  804. get
  805. {
  806. if (!this.filter.DestinationSymmetricKey)
  807. {
  808. if (!receiveCreated)
  809. return new byte[0];
  810. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "DestinationSymmetricKey"));
  811. }
  812. byte[] bytes = new byte[properties.GetUI4(NativeMethods.MESSAGE_PROPID_DEST_SYMM_KEY_LEN)];
  813. Array.Copy(properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_DEST_SYMM_KEY), bytes, bytes.Length);
  814. return bytes;
  815. }
  816. set
  817. {
  818. if (value == null)
  819. throw new ArgumentNullException("value");
  820. //If default
  821. if (value.Length == 0)
  822. {
  823. this.filter.DestinationSymmetricKey = false;
  824. properties.Remove(NativeMethods.MESSAGE_PROPID_DEST_SYMM_KEY);
  825. properties.Remove(NativeMethods.MESSAGE_PROPID_DEST_SYMM_KEY_LEN);
  826. }
  827. else
  828. {
  829. this.filter.DestinationSymmetricKey = true;
  830. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_DEST_SYMM_KEY, value);
  831. properties.SetUI4(NativeMethods.MESSAGE_PROPID_DEST_SYMM_KEY_LEN, value.Length);
  832. }
  833. }
  834. }
  835. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.DigitalSignature"]/*' />
  836. /// <devdoc>
  837. /// <para>
  838. /// Gets or
  839. /// sets the digital signature used to authenticate
  840. /// the message.
  841. /// </para>
  842. /// </devdoc>
  843. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgDigitalSignature)]
  844. [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
  845. [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
  846. public byte[] DigitalSignature
  847. {
  848. get
  849. {
  850. if (!this.filter.DigitalSignature)
  851. {
  852. if (!receiveCreated)
  853. return new byte[0];
  854. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "DigitalSignature"));
  855. }
  856. byte[] bytes = new byte[properties.GetUI4(NativeMethods.MESSAGE_PROPID_SIGNATURE_LEN)];
  857. Array.Copy(properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_SIGNATURE), bytes, bytes.Length);
  858. return bytes;
  859. }
  860. set
  861. {
  862. if (value == null)
  863. throw new ArgumentNullException("value");
  864. if (value.Length == 0)
  865. {
  866. this.filter.DigitalSignature = false;
  867. properties.Remove(NativeMethods.MESSAGE_PROPID_SIGNATURE);
  868. properties.Remove(NativeMethods.MESSAGE_PROPID_SIGNATURE_LEN);
  869. }
  870. else
  871. {
  872. this.filter.DigitalSignature = true;
  873. this.filter.UseAuthentication = true;
  874. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_SIGNATURE, value);
  875. properties.SetUI4(NativeMethods.MESSAGE_PROPID_SIGNATURE_LEN, value.Length);
  876. }
  877. }
  878. }
  879. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.EncryptionAlgorithm"]/*' />
  880. /// <devdoc>
  881. /// <para>
  882. /// Gets or sets the encryption algorithm used to encrypt the
  883. /// body of a private message.
  884. /// </para>
  885. /// </devdoc>
  886. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgEncryptionAlgorithm)]
  887. public EncryptionAlgorithm EncryptionAlgorithm
  888. {
  889. get
  890. {
  891. if (!this.filter.EncryptionAlgorithm)
  892. {
  893. //Return default.
  894. if (!receiveCreated)
  895. return EncryptionAlgorithm.Rc2;
  896. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "EncryptionAlgorithm"));
  897. }
  898. return (EncryptionAlgorithm)properties.GetUI4(NativeMethods.MESSAGE_PROPID_ENCRYPTION_ALG);
  899. }
  900. set
  901. {
  902. if (!ValidationUtility.ValidateEncryptionAlgorithm(value))
  903. throw new InvalidEnumArgumentException("value", (int)value, typeof(EncryptionAlgorithm));
  904. //Should not remove if default, the default value might change in future MQ clients
  905. //if (value == EncryptionAlgorithm.Rc2) {
  906. // this.filter.EncryptionAlgorithm = false;
  907. // properties.Remove(NativeMethods.MESSAGE_PROPID_ENCRYPTION_ALG);
  908. //}
  909. //else {
  910. this.filter.EncryptionAlgorithm = true;
  911. properties.SetUI4(NativeMethods.MESSAGE_PROPID_ENCRYPTION_ALG, (int)value);
  912. //}
  913. }
  914. }
  915. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.Extension"]/*' />
  916. /// <devdoc>
  917. /// <para>
  918. /// Gets or sets
  919. /// additional information associated with the message.
  920. /// </para>
  921. /// </devdoc>
  922. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
  923. MessagingDescription(Res.MsgExtension)]
  924. [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
  925. [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
  926. public byte[] Extension
  927. {
  928. get
  929. {
  930. if (!this.filter.Extension)
  931. {
  932. //Return default.
  933. if (!receiveCreated)
  934. return new byte[0];
  935. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "Extension"));
  936. }
  937. byte[] bytes = new byte[properties.GetUI4(NativeMethods.MESSAGE_PROPID_EXTENSION_LEN)];
  938. Array.Copy(properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_EXTENSION), bytes, bytes.Length);
  939. return bytes;
  940. }
  941. set
  942. {
  943. if (value == null)
  944. throw new ArgumentNullException("value");
  945. //If default
  946. if (value.Length == 0)
  947. {
  948. this.filter.Extension = false;
  949. properties.Remove(NativeMethods.MESSAGE_PROPID_EXTENSION);
  950. properties.Remove(NativeMethods.MESSAGE_PROPID_EXTENSION_LEN);
  951. }
  952. else
  953. {
  954. this.filter.Extension = true;
  955. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_EXTENSION, value);
  956. properties.SetUI4(NativeMethods.MESSAGE_PROPID_EXTENSION_LEN, value.Length);
  957. }
  958. }
  959. }
  960. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.Formatter"]/*' />
  961. /// <devdoc>
  962. /// <para>
  963. /// Gets or sets
  964. /// the formatter used to read or write an object into the message
  965. /// body.
  966. /// </para>
  967. /// </devdoc>
  968. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  969. public IMessageFormatter Formatter
  970. {
  971. get
  972. {
  973. return this.cachedFormatter;
  974. }
  975. set
  976. {
  977. if (value == null)
  978. throw new ArgumentNullException("value");
  979. this.cachedFormatter = value;
  980. }
  981. }
  982. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.HashAlgorithm"]/*' />
  983. /// <devdoc>
  984. /// <para>
  985. /// Gets or sets the hashing
  986. /// algorithm used when authenticating messages.
  987. /// </para>
  988. /// </devdoc>
  989. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgHashAlgorithm)]
  990. public HashAlgorithm HashAlgorithm
  991. {
  992. get
  993. {
  994. if (!this.filter.HashAlgorithm)
  995. {
  996. //This property has not been set, lets return an empty queue.
  997. if (!receiveCreated)
  998. {
  999. /*
  1000. if (LocalAppContextSwitches.UseMD5ForDefaultHashAlgorithm)
  1001. {
  1002. }
  1003. return HashAlgorithm.Sha512;
  1004. */
  1005. return HashAlgorithm.Md5;
  1006. }
  1007. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "HashAlgorithm"));
  1008. }
  1009. return (HashAlgorithm)properties.GetUI4(NativeMethods.MESSAGE_PROPID_HASH_ALG);
  1010. }
  1011. set
  1012. {
  1013. if (!ValidationUtility.ValidateHashAlgorithm(value))
  1014. throw new InvalidEnumArgumentException("value", (int)value, typeof(HashAlgorithm));
  1015. //Should not remove if default since MQ3.0 changed the default algorithm
  1016. //if (value == HashAlgorithm.Md5) {
  1017. // this.filter.HashAlgorithm = false;
  1018. // properties.Remove(NativeMethods.MESSAGE_PROPID_HASH_ALG);
  1019. //}
  1020. //else {
  1021. this.filter.HashAlgorithm = true;
  1022. properties.SetUI4(NativeMethods.MESSAGE_PROPID_HASH_ALG, (int)value);
  1023. //}
  1024. }
  1025. }
  1026. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.Id"]/*' />
  1027. /// <devdoc>
  1028. /// <para>
  1029. /// Gets
  1030. /// the Message Queuing-generated identifier of the message.
  1031. /// </para>
  1032. /// </devdoc>
  1033. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgId)]
  1034. public string Id
  1035. {
  1036. get
  1037. {
  1038. if (!this.filter.Id)
  1039. {
  1040. //The Id is undefined at this point
  1041. if (!receiveCreated)
  1042. throw new InvalidOperationException(Res.GetString(Res.IdNotSet));
  1043. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "Id"));
  1044. }
  1045. return IdFromByteArray(this.properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_MSGID));
  1046. }
  1047. }
  1048. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.IsFirstInTransaction"]/*' />
  1049. /// <devdoc>
  1050. /// <para>
  1051. /// Gets a value indicating
  1052. /// whether the message was the first message sent in a transaction.
  1053. /// </para>
  1054. /// </devdoc>
  1055. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgIsFirstInTransaction)]
  1056. public bool IsFirstInTransaction
  1057. {
  1058. get
  1059. {
  1060. if (!this.filter.IsFirstInTransaction)
  1061. {
  1062. if (!receiveCreated)
  1063. return false;
  1064. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "IsFirstInTransaction"));
  1065. }
  1066. return (properties.GetUI1(NativeMethods.MESSAGE_PROPID_FIRST_IN_XACT) != 0);
  1067. }
  1068. }
  1069. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.IsLastInTransaction"]/*' />
  1070. /// <devdoc>
  1071. /// <para>
  1072. /// Gets a value indicating whether the message was
  1073. /// the last message sent in a transaction.
  1074. /// </para>
  1075. /// </devdoc>
  1076. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgIsLastInTransaction)]
  1077. public bool IsLastInTransaction
  1078. {
  1079. get
  1080. {
  1081. if (!this.filter.IsLastInTransaction)
  1082. {
  1083. if (!receiveCreated)
  1084. return false;
  1085. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "IsLastInTransaction"));
  1086. }
  1087. return (properties.GetUI1(NativeMethods.MESSAGE_PROPID_LAST_IN_XACT) != 0);
  1088. }
  1089. }
  1090. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.Label"]/*' />
  1091. /// <devdoc>
  1092. /// <para>
  1093. /// Gets or sets the message label.
  1094. /// </para>
  1095. /// </devdoc>
  1096. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgLabel)]
  1097. public string Label
  1098. {
  1099. get
  1100. {
  1101. if (!this.filter.Label)
  1102. {
  1103. //Return default
  1104. if (!receiveCreated)
  1105. return String.Empty;
  1106. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "Label"));
  1107. }
  1108. if (properties.GetUI4(NativeMethods.MESSAGE_PROPID_LABEL_LEN) != 0)
  1109. return StringFromBytes(this.properties.GetString(NativeMethods.MESSAGE_PROPID_LABEL),
  1110. properties.GetUI4(NativeMethods.MESSAGE_PROPID_LABEL_LEN));
  1111. else
  1112. return "";
  1113. }
  1114. set
  1115. {
  1116. if (value == null)
  1117. throw new ArgumentNullException("value");
  1118. //If default
  1119. if (value.Length == 0)
  1120. {
  1121. this.filter.Label = false;
  1122. properties.Remove(NativeMethods.MESSAGE_PROPID_LABEL);
  1123. properties.Remove(NativeMethods.MESSAGE_PROPID_LABEL_LEN);
  1124. }
  1125. else
  1126. {
  1127. this.filter.Label = true;
  1128. properties.SetString(NativeMethods.MESSAGE_PROPID_LABEL, StringToBytes(value));
  1129. properties.SetUI4(NativeMethods.MESSAGE_PROPID_LABEL_LEN, value.Length);
  1130. }
  1131. }
  1132. }
  1133. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.LookupId"]/*' />
  1134. /// <devdoc>
  1135. /// <para>
  1136. /// Gets the Message Queue-generated lookup identifier of the message.
  1137. /// </para>
  1138. /// </devdoc>
  1139. public long LookupId
  1140. {
  1141. get
  1142. {
  1143. if (!MessageQueue.Msmq3OrNewer)
  1144. throw new PlatformNotSupportedException(Res.GetString(Res.PlatformNotSupported));
  1145. if (!this.filter.LookupId)
  1146. {
  1147. //Return default
  1148. if (!receiveCreated)
  1149. throw new InvalidOperationException(Res.GetString(Res.LookupIdNotSet));
  1150. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "LookupId"));
  1151. }
  1152. return this.properties.GetUI8(NativeMethods.MESSAGE_PROPID_LOOKUPID);
  1153. }
  1154. }
  1155. internal void SetLookupId(long value)
  1156. {
  1157. this.filter.LookupId = true;
  1158. this.properties.SetUI8(NativeMethods.MESSAGE_PROPID_LOOKUPID, value);
  1159. }
  1160. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.MessageType"]/*' />
  1161. /// <devdoc>
  1162. /// <para>
  1163. /// Gets the type of the message (normal, acknowledgment, or report).
  1164. /// </para>
  1165. /// </devdoc>
  1166. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgMessageType)]
  1167. public MessageType MessageType
  1168. {
  1169. get
  1170. {
  1171. if (!this.filter.MessageType)
  1172. {
  1173. //Return default
  1174. if (!receiveCreated)
  1175. throw new InvalidOperationException(Res.GetString(Res.MessageTypeNotSet));
  1176. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "MessageType"));
  1177. }
  1178. int cls = properties.GetUI2(NativeMethods.MESSAGE_PROPID_CLASS);
  1179. if (cls == NativeMethods.MESSAGE_CLASS_NORMAL)
  1180. return MessageType.Normal;
  1181. if (cls == NativeMethods.MESSAGE_CLASS_REPORT)
  1182. return MessageType.Report;
  1183. return MessageType.Acknowledgment;
  1184. }
  1185. }
  1186. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.Priority"]/*' />
  1187. /// <devdoc>
  1188. /// <para>
  1189. /// Gets or sets the message priority, used to determine
  1190. /// where the
  1191. /// message is placed in the
  1192. /// queue.
  1193. /// </para>
  1194. /// </devdoc>
  1195. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgPriority)]
  1196. public MessagePriority Priority
  1197. {
  1198. get
  1199. {
  1200. if (!this.filter.Priority)
  1201. {
  1202. //Return default
  1203. if (!receiveCreated)
  1204. return MessagePriority.Normal;
  1205. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "Priority"));
  1206. }
  1207. return (MessagePriority)properties.GetUI1(NativeMethods.MESSAGE_PROPID_PRIORITY);
  1208. }
  1209. set
  1210. {
  1211. if (!ValidationUtility.ValidateMessagePriority(value))
  1212. throw new InvalidEnumArgumentException("value", (int)value, typeof(MessagePriority));
  1213. //If default
  1214. if (value == MessagePriority.Normal)
  1215. {
  1216. this.filter.Priority = false;
  1217. properties.Remove(NativeMethods.MESSAGE_PROPID_PRIORITY);
  1218. }
  1219. else
  1220. {
  1221. this.filter.Priority = true;
  1222. properties.SetUI1(NativeMethods.MESSAGE_PROPID_PRIORITY, (byte)value);
  1223. }
  1224. }
  1225. }
  1226. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.Recoverable"]/*' />
  1227. /// <devdoc>
  1228. /// <para>
  1229. /// Gets or sets a value
  1230. /// indicating whether the message is guaranteed to be delivered in the event of
  1231. /// a computer failure or network problem.
  1232. /// </para>
  1233. /// </devdoc>
  1234. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgRecoverable)]
  1235. public bool Recoverable
  1236. {
  1237. get
  1238. {
  1239. if (!this.filter.Recoverable)
  1240. {
  1241. //Return default
  1242. if (!receiveCreated)
  1243. return false;
  1244. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "Recoverable"));
  1245. }
  1246. return properties.GetUI1(NativeMethods.MESSAGE_PROPID_DELIVERY) == NativeMethods.MESSAGE_DELIVERY_RECOVERABLE;
  1247. }
  1248. set
  1249. {
  1250. //If default
  1251. if (!value)
  1252. {
  1253. this.filter.Recoverable = false;
  1254. properties.Remove(NativeMethods.MESSAGE_PROPID_DELIVERY);
  1255. }
  1256. else
  1257. {
  1258. this.filter.Recoverable = true;
  1259. properties.SetUI1(NativeMethods.MESSAGE_PROPID_DELIVERY, (byte)NativeMethods.MESSAGE_DELIVERY_RECOVERABLE);
  1260. }
  1261. }
  1262. }
  1263. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.ResponseQueue"]/*' />
  1264. /// <devdoc>
  1265. /// <para>
  1266. /// Gets or sets the queue which receives application-generated
  1267. /// response messages.
  1268. /// </para>
  1269. /// </devdoc>
  1270. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgResponseQueue)]
  1271. public MessageQueue ResponseQueue
  1272. {
  1273. get
  1274. {
  1275. if (!this.filter.ResponseQueue)
  1276. {
  1277. //This property has not been set, lets return an undefined value.
  1278. if (!receiveCreated)
  1279. return null;
  1280. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "ResponseQueue"));
  1281. }
  1282. if (this.cachedResponseQueue == null)
  1283. {
  1284. if (properties.GetUI4(NativeMethods.MESSAGE_PROPID_RESP_QUEUE_LEN) != 0)
  1285. {
  1286. string queueFormatName = StringFromBytes(properties.GetString(NativeMethods.MESSAGE_PROPID_RESP_QUEUE),
  1287. properties.GetUI4(NativeMethods.MESSAGE_PROPID_RESP_QUEUE_LEN));
  1288. this.cachedResponseQueue = new MessageQueue("FORMATNAME:" + queueFormatName);
  1289. }
  1290. }
  1291. return this.cachedResponseQueue;
  1292. }
  1293. set
  1294. {
  1295. //If default
  1296. if (value != null)
  1297. this.filter.ResponseQueue = true;
  1298. else
  1299. {
  1300. if (this.filter.ResponseQueue)
  1301. {
  1302. this.filter.ResponseQueue = false;
  1303. properties.Remove(NativeMethods.MESSAGE_PROPID_RESP_QUEUE);
  1304. properties.Remove(NativeMethods.MESSAGE_PROPID_RESP_QUEUE_LEN);
  1305. }
  1306. }
  1307. this.cachedResponseQueue = value;
  1308. }
  1309. }
  1310. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1311. public SecurityContext SecurityContext
  1312. {
  1313. get
  1314. {
  1315. if (!this.filter.SecurityContext)
  1316. return null;
  1317. IntPtr handle = (IntPtr)(int)properties.GetUI4(NativeMethods.MESSAGE_PROPID_SECURITY_CONTEXT);
  1318. return new SecurityContext(new SecurityContextHandle(handle));
  1319. }
  1320. set
  1321. {
  1322. if (value == null)
  1323. {
  1324. this.filter.SecurityContext = false;
  1325. properties.Remove(NativeMethods.MESSAGE_PROPID_SECURITY_CONTEXT);
  1326. }
  1327. else
  1328. {
  1329. this.filter.SecurityContext = true;
  1330. // Can't store IntPtr because property type is UI4, but IntPtr can be 64 bits
  1331. int handle = value.Handle.DangerousGetHandle().ToInt32(); // this is safe because MSMQ always returns 32-bit handle
  1332. properties.SetUI4(NativeMethods.MESSAGE_PROPID_SECURITY_CONTEXT, handle);
  1333. }
  1334. }
  1335. }
  1336. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.SenderCertificate"]/*' />
  1337. /// <devdoc>
  1338. /// <para>
  1339. /// Specifies the security certificate used to authenticate messages.
  1340. /// </para>
  1341. /// </devdoc>
  1342. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgSenderCertificate)]
  1343. [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
  1344. [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
  1345. public byte[] SenderCertificate
  1346. {
  1347. get
  1348. {
  1349. if (!this.filter.SenderCertificate)
  1350. {
  1351. //Return default
  1352. if (!receiveCreated)
  1353. return new byte[0];
  1354. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "SenderCertificate"));
  1355. }
  1356. byte[] bytes = new byte[properties.GetUI4(NativeMethods.MESSAGE_PROPID_SENDER_CERT_LEN)];
  1357. Array.Copy(properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_SENDER_CERT), bytes, bytes.Length);
  1358. return bytes;
  1359. }
  1360. set
  1361. {
  1362. if (value == null)
  1363. throw new ArgumentNullException("value");
  1364. //If default
  1365. if (value.Length == 0)
  1366. {
  1367. this.filter.SenderCertificate = false;
  1368. properties.Remove(NativeMethods.MESSAGE_PROPID_SENDER_CERT);
  1369. properties.Remove(NativeMethods.MESSAGE_PROPID_SENDER_CERT_LEN);
  1370. }
  1371. else
  1372. {
  1373. this.filter.SenderCertificate = true;
  1374. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_SENDER_CERT, value);
  1375. properties.SetUI4(NativeMethods.MESSAGE_PROPID_SENDER_CERT_LEN, value.Length);
  1376. }
  1377. }
  1378. }
  1379. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.SenderId"]/*' />
  1380. /// <devdoc>
  1381. /// <para>
  1382. /// This property is set by MSMQ, and is used primarily by the
  1383. /// receiving Queue Manager when authenticating a message. The receiving
  1384. /// Queue Manager uses the sender identifier in this property to verify where
  1385. /// the message originated and to verify the sender has access rights to a queue.
  1386. /// </para>
  1387. /// </devdoc>
  1388. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgSenderId)]
  1389. [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
  1390. public byte[] SenderId
  1391. {
  1392. get
  1393. {
  1394. if (!this.filter.SenderId)
  1395. {
  1396. if (!receiveCreated)
  1397. throw new InvalidOperationException(Res.GetString(Res.SenderIdNotSet));
  1398. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "SenderId"));
  1399. }
  1400. byte[] bytes = new byte[properties.GetUI4(NativeMethods.MESSAGE_PROPID_SENDERID_LEN)];
  1401. Array.Copy(properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_SENDERID), bytes, bytes.Length);
  1402. return bytes;
  1403. }
  1404. }
  1405. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.SenderVersion"]/*' />
  1406. /// <devdoc>
  1407. /// <para>
  1408. /// Gets the version of Message Queuing used to send the message.
  1409. /// </para>
  1410. /// </devdoc>
  1411. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgSenderVersion)]
  1412. public long SenderVersion
  1413. {
  1414. get
  1415. {
  1416. if (!this.filter.SenderVersion)
  1417. {
  1418. if (!receiveCreated)
  1419. throw new InvalidOperationException(Res.GetString(Res.VersionNotSet));
  1420. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "SenderVersion"));
  1421. }
  1422. return (long)((uint)properties.GetUI4(NativeMethods.MESSAGE_PROPID_VERSION));
  1423. }
  1424. }
  1425. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.SentTime"]/*' />
  1426. /// <devdoc>
  1427. /// Indicates the date and time that the message was sent by
  1428. /// the source Queue Manager.
  1429. /// </devdoc>
  1430. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgSentTime)]
  1431. public DateTime SentTime
  1432. {
  1433. get
  1434. {
  1435. if (!this.filter.SentTime)
  1436. {
  1437. if (!receiveCreated)
  1438. throw new InvalidOperationException(Res.GetString(Res.SentTimeNotSet));
  1439. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "SentTime"));
  1440. }
  1441. //Number of seconds ellapsed since 1/1/1970
  1442. DateTime time = new DateTime(1970, 1, 1);
  1443. time = time.AddSeconds(properties.GetUI4(NativeMethods.MESSAGE_PROPID_SENTTIME)).ToLocalTime();
  1444. return time;
  1445. }
  1446. }
  1447. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.SourceMachine"]/*' />
  1448. /// <devdoc>
  1449. /// Specifies the computer where the message originated.
  1450. /// </devdoc>
  1451. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgSourceMachine)]
  1452. public string SourceMachine
  1453. {
  1454. get
  1455. {
  1456. if (!this.filter.SourceMachine)
  1457. {
  1458. if (!receiveCreated)
  1459. throw new InvalidOperationException(Res.GetString(Res.SourceMachineNotSet));
  1460. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "SourceMachine"));
  1461. }
  1462. if (this.machineName == null)
  1463. {
  1464. byte[] bytes = this.properties.GetGuid(NativeMethods.MESSAGE_PROPID_SRC_MACHINE_ID);
  1465. GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
  1466. MachinePropertyVariants machineProperties = new MachinePropertyVariants();
  1467. machineProperties.SetNull(NativeMethods.MACHINE_PATHNAME);
  1468. int status = UnsafeNativeMethods.MQGetMachineProperties(null, handle.AddrOfPinnedObject(), machineProperties.Lock());
  1469. machineProperties.Unlock();
  1470. handle.Free();
  1471. IntPtr memoryHandle = machineProperties.GetIntPtr(NativeMethods.MACHINE_PATHNAME);
  1472. if (memoryHandle != (IntPtr)0)
  1473. {
  1474. //Using Unicode API even on Win9x
  1475. this.machineName = Marshal.PtrToStringUni(memoryHandle);
  1476. SafeNativeMethods.MQFreeMemory(memoryHandle);
  1477. }
  1478. if (MessageQueue.IsFatalError(status))
  1479. throw new MessageQueueException(status);
  1480. }
  1481. return this.machineName;
  1482. }
  1483. }
  1484. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.TimeToBeReceived"]/*' />
  1485. /// <devdoc>
  1486. /// <para>
  1487. /// Gets or
  1488. /// sets the time limit for the message to be retrieved from the target
  1489. /// queue.
  1490. /// </para>
  1491. /// </devdoc>
  1492. [ReadOnly(true),
  1493. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
  1494. TypeConverter(typeof(System.Messaging.Design.TimeoutConverter)),
  1495. MessagingDescription(Res.MsgTimeToBeReceived)]
  1496. public TimeSpan TimeToBeReceived
  1497. {
  1498. get
  1499. {
  1500. if (!this.filter.TimeToBeReceived)
  1501. {
  1502. //Return default
  1503. if (!receiveCreated)
  1504. return InfiniteTimeout;
  1505. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "TimeToBeReceived"));
  1506. }
  1507. return TimeSpan.FromSeconds((uint)properties.GetUI4(NativeMethods.MESSAGE_PROPID_TIME_TO_BE_RECEIVED));
  1508. }
  1509. set
  1510. {
  1511. long timeoutInSeconds = (long)value.TotalSeconds;
  1512. if (timeoutInSeconds < 0)
  1513. throw new ArgumentException(Res.GetString(Res.InvalidProperty, "TimeToBeReceived", value.ToString()));
  1514. if (timeoutInSeconds > UInt32.MaxValue)
  1515. timeoutInSeconds = UInt32.MaxValue;
  1516. //If default
  1517. if (timeoutInSeconds == UInt32.MaxValue)
  1518. {
  1519. this.filter.TimeToBeReceived = false;
  1520. properties.Remove(NativeMethods.MESSAGE_PROPID_TIME_TO_BE_RECEIVED);
  1521. }
  1522. else
  1523. {
  1524. this.filter.TimeToBeReceived = true;
  1525. properties.SetUI4(NativeMethods.MESSAGE_PROPID_TIME_TO_BE_RECEIVED, (int)((uint)timeoutInSeconds));
  1526. }
  1527. }
  1528. }
  1529. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.TimeToReachQueue"]/*' />
  1530. /// <devdoc>
  1531. /// <para>
  1532. /// Gets or sets the time limit for the message to reach
  1533. /// the queue.
  1534. /// </para>
  1535. /// </devdoc>
  1536. [ReadOnly(true),
  1537. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
  1538. TypeConverter(typeof(System.Messaging.Design.TimeoutConverter)),
  1539. MessagingDescription(Res.MsgTimeToReachQueue)]
  1540. public TimeSpan TimeToReachQueue
  1541. {
  1542. get
  1543. {
  1544. if (!this.filter.TimeToReachQueue)
  1545. {
  1546. //Return default
  1547. if (!receiveCreated)
  1548. return InfiniteTimeout;
  1549. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "TimeToReachQueue"));
  1550. }
  1551. return TimeSpan.FromSeconds((uint)properties.GetUI4(NativeMethods.MESSAGE_PROPID_TIME_TO_REACH_QUEUE));
  1552. }
  1553. set
  1554. {
  1555. long timeoutInSeconds = (long)value.TotalSeconds;
  1556. if (timeoutInSeconds < 0)
  1557. throw new ArgumentException(Res.GetString(Res.InvalidProperty, "TimeToReachQueue", value.ToString()));
  1558. if (timeoutInSeconds > UInt32.MaxValue)
  1559. timeoutInSeconds = UInt32.MaxValue;
  1560. if (timeoutInSeconds == UInt32.MaxValue)
  1561. {
  1562. this.filter.TimeToReachQueue = false;
  1563. properties.Remove(NativeMethods.MESSAGE_PROPID_TIME_TO_REACH_QUEUE);
  1564. }
  1565. else
  1566. {
  1567. this.filter.TimeToReachQueue = true;
  1568. properties.SetUI4(NativeMethods.MESSAGE_PROPID_TIME_TO_REACH_QUEUE, (int)((uint)timeoutInSeconds));
  1569. }
  1570. }
  1571. }
  1572. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.TransactionId"]/*' />
  1573. /// <devdoc>
  1574. /// <para>
  1575. /// Gets the
  1576. /// identifier for the transaction of which the message was a part.
  1577. /// </para>
  1578. /// </devdoc>
  1579. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgTransactionId)]
  1580. public string TransactionId
  1581. {
  1582. get
  1583. {
  1584. if (!this.filter.TransactionId)
  1585. {
  1586. //Return default
  1587. if (!receiveCreated)
  1588. return String.Empty;
  1589. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "TransactionId"));
  1590. }
  1591. return IdFromByteArray(this.properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_XACTID));
  1592. }
  1593. }
  1594. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.TransactionStatusQueue"]/*' />
  1595. /// <devdoc>
  1596. /// <para>
  1597. /// Gets the
  1598. /// transaction status queue on the source computer.
  1599. /// </para>
  1600. /// </devdoc>
  1601. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgTransactionStatusQueue)]
  1602. public MessageQueue TransactionStatusQueue
  1603. {
  1604. get
  1605. {
  1606. if (!this.filter.TransactionStatusQueue)
  1607. {
  1608. //This property has not been set, lets return an undefined value.
  1609. if (!receiveCreated)
  1610. return null;
  1611. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "TransactionStatusQueue"));
  1612. }
  1613. if (this.cachedTransactionStatusQueue == null)
  1614. {
  1615. if (this.properties.GetUI4(NativeMethods.MESSAGE_PROPID_XACT_STATUS_QUEUE_LEN) != 0)
  1616. {
  1617. string queueFormatName = StringFromBytes(properties.GetString(NativeMethods.MESSAGE_PROPID_XACT_STATUS_QUEUE),
  1618. properties.GetUI4(NativeMethods.MESSAGE_PROPID_XACT_STATUS_QUEUE_LEN));
  1619. this.cachedTransactionStatusQueue = new MessageQueue("FORMATNAME:" + queueFormatName);
  1620. }
  1621. }
  1622. return this.cachedTransactionStatusQueue;
  1623. }
  1624. set
  1625. {
  1626. //If default
  1627. if (value != null)
  1628. this.filter.TransactionStatusQueue = true;
  1629. else
  1630. {
  1631. if (this.filter.TransactionStatusQueue)
  1632. {
  1633. this.filter.TransactionStatusQueue = false;
  1634. properties.Remove(NativeMethods.MESSAGE_PROPID_XACT_STATUS_QUEUE);
  1635. properties.Remove(NativeMethods.MESSAGE_PROPID_XACT_STATUS_QUEUE_LEN);
  1636. }
  1637. }
  1638. this.cachedTransactionStatusQueue = value;
  1639. }
  1640. }
  1641. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.UseAuthentication"]/*' />
  1642. /// <devdoc>
  1643. /// <para>
  1644. /// Gets
  1645. /// or sets a value indicating whether a message must be authenticated.
  1646. /// </para>
  1647. /// </devdoc>
  1648. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgUseAuthentication)]
  1649. public bool UseAuthentication
  1650. {
  1651. get
  1652. {
  1653. if (!this.filter.UseAuthentication)
  1654. {
  1655. //Return default
  1656. if (!receiveCreated)
  1657. {
  1658. // Actually, we dont know what default is:
  1659. // Algorithm to determine whether or not messages
  1660. // should be authenticated by default is non-trivial
  1661. // and should not be reproduced in System.Messaging.
  1662. //
  1663. // One idea is to add a new native API,
  1664. // MQGetDefaultPropertyValue, to retrieve default values.
  1665. // (Microsoft, Nov 3 2004)
  1666. return false;
  1667. }
  1668. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "UseAuthentication"));
  1669. }
  1670. return (properties.GetUI4(NativeMethods.MESSAGE_PROPID_AUTH_LEVEL) != NativeMethods.MESSAGE_AUTHENTICATION_LEVEL_NONE);
  1671. }
  1672. set
  1673. {
  1674. //default is different on different versions of MSMQ,
  1675. //so dont make any assumptions and explicitly pass what user requested
  1676. this.filter.UseAuthentication = true;
  1677. if (!value)
  1678. {
  1679. properties.SetUI4(NativeMethods.MESSAGE_PROPID_AUTH_LEVEL, NativeMethods.MESSAGE_AUTHENTICATION_LEVEL_NONE);
  1680. }
  1681. else
  1682. {
  1683. properties.SetUI4(NativeMethods.MESSAGE_PROPID_AUTH_LEVEL, NativeMethods.MESSAGE_AUTHENTICATION_LEVEL_ALWAYS);
  1684. }
  1685. }
  1686. }
  1687. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.UseDeadLetterQueue"]/*' />
  1688. /// <devdoc>
  1689. /// <para>
  1690. /// Gets or sets a value indicating whether
  1691. /// a copy of an undeliverable message should be sent to a dead-letter queue.
  1692. /// </para>
  1693. /// </devdoc>
  1694. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgUseDeadLetterQueue)]
  1695. public bool UseDeadLetterQueue
  1696. {
  1697. get
  1698. {
  1699. if (!this.filter.UseDeadLetterQueue)
  1700. {
  1701. //Return default
  1702. if (!receiveCreated)
  1703. return false;
  1704. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "UseDeadLetterQueue"));
  1705. }
  1706. return ((properties.GetUI1(NativeMethods.MESSAGE_PROPID_JOURNAL) & NativeMethods.MESSAGE_JOURNAL_DEADLETTER) != 0);
  1707. }
  1708. set
  1709. {
  1710. //If Default
  1711. if (!value)
  1712. {
  1713. if (this.filter.UseDeadLetterQueue)
  1714. {
  1715. this.filter.UseDeadLetterQueue = false;
  1716. if (!this.filter.UseJournalQueue)
  1717. properties.Remove(NativeMethods.MESSAGE_PROPID_JOURNAL);
  1718. else
  1719. properties.SetUI1(NativeMethods.MESSAGE_PROPID_JOURNAL, (byte)(properties.GetUI1(NativeMethods.MESSAGE_PROPID_JOURNAL) & ~NativeMethods.MESSAGE_JOURNAL_DEADLETTER));
  1720. }
  1721. }
  1722. else
  1723. {
  1724. if (!this.filter.UseDeadLetterQueue && !this.filter.UseJournalQueue)
  1725. properties.SetUI1(NativeMethods.MESSAGE_PROPID_JOURNAL, (byte)NativeMethods.MESSAGE_JOURNAL_DEADLETTER);
  1726. else
  1727. properties.SetUI1(NativeMethods.MESSAGE_PROPID_JOURNAL, (byte)(properties.GetUI1(NativeMethods.MESSAGE_PROPID_JOURNAL) | NativeMethods.MESSAGE_JOURNAL_DEADLETTER));
  1728. this.filter.UseDeadLetterQueue = true;
  1729. }
  1730. }
  1731. }
  1732. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.UseEncryption"]/*' />
  1733. /// <devdoc>
  1734. /// <para>
  1735. /// Gets or sets a value indicating whether to encrypt messages.
  1736. /// </para>
  1737. /// </devdoc>
  1738. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgUseEncryption)]
  1739. public bool UseEncryption
  1740. {
  1741. get
  1742. {
  1743. if (!this.filter.UseEncryption)
  1744. {
  1745. //Return default
  1746. if (!receiveCreated)
  1747. return false;
  1748. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "UseEncryption"));
  1749. }
  1750. return (properties.GetUI4(NativeMethods.MESSAGE_PROPID_PRIV_LEVEL) != NativeMethods.MESSAGE_PRIVACY_LEVEL_NONE);
  1751. }
  1752. set
  1753. {
  1754. //If default
  1755. if (!value)
  1756. {
  1757. this.filter.UseEncryption = false;
  1758. properties.Remove(NativeMethods.MESSAGE_PROPID_PRIV_LEVEL);
  1759. }
  1760. else
  1761. {
  1762. this.filter.UseEncryption = true;
  1763. properties.SetUI4(NativeMethods.MESSAGE_PROPID_PRIV_LEVEL, NativeMethods.MESSAGE_PRIVACY_LEVEL_BODY);
  1764. }
  1765. }
  1766. }
  1767. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.UseJournalQueue"]/*' />
  1768. /// <devdoc>
  1769. /// <para>
  1770. /// Gets or sets a value indicating whether a copy of the message should be kept in a machine
  1771. /// journal on the originating computer.
  1772. /// </para>
  1773. /// </devdoc>
  1774. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgUseJournalQueue)]
  1775. public bool UseJournalQueue
  1776. {
  1777. get
  1778. {
  1779. if (!this.filter.UseJournalQueue)
  1780. {
  1781. //Return default
  1782. if (!receiveCreated)
  1783. return false;
  1784. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "UseJournalQueue"));
  1785. }
  1786. return ((properties.GetUI1(NativeMethods.MESSAGE_PROPID_JOURNAL) & NativeMethods.MESSAGE_JOURNAL_JOURNAL) != 0);
  1787. }
  1788. set
  1789. {
  1790. //If Default
  1791. if (!value)
  1792. {
  1793. if (this.filter.UseJournalQueue)
  1794. {
  1795. this.filter.UseJournalQueue = false;
  1796. if (!this.filter.UseDeadLetterQueue)
  1797. properties.Remove(NativeMethods.MESSAGE_PROPID_JOURNAL);
  1798. else
  1799. properties.SetUI1(NativeMethods.MESSAGE_PROPID_JOURNAL, (byte)(properties.GetUI1(NativeMethods.MESSAGE_PROPID_JOURNAL) & ~NativeMethods.MESSAGE_JOURNAL_JOURNAL));
  1800. }
  1801. }
  1802. else
  1803. {
  1804. if (!this.filter.UseDeadLetterQueue && !this.filter.UseJournalQueue)
  1805. properties.SetUI1(NativeMethods.MESSAGE_PROPID_JOURNAL, (byte)NativeMethods.MESSAGE_JOURNAL_JOURNAL);
  1806. else
  1807. properties.SetUI1(NativeMethods.MESSAGE_PROPID_JOURNAL, (byte)(properties.GetUI1(NativeMethods.MESSAGE_PROPID_JOURNAL) | NativeMethods.MESSAGE_JOURNAL_JOURNAL));
  1808. this.filter.UseJournalQueue = true;
  1809. }
  1810. }
  1811. }
  1812. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.UseTracing"]/*' />
  1813. /// <devdoc>
  1814. /// <para>
  1815. /// Gets or
  1816. /// sets a value indicating whether to trace a message as
  1817. /// it moves toward its destination queue.
  1818. /// </para>
  1819. /// </devdoc>
  1820. [ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), MessagingDescription(Res.MsgUseTracing)]
  1821. public bool UseTracing
  1822. {
  1823. get
  1824. {
  1825. if (!this.filter.UseTracing)
  1826. {
  1827. //Return default
  1828. if (!receiveCreated)
  1829. return false;
  1830. throw new InvalidOperationException(Res.GetString(Res.MissingProperty, "UseTracing"));
  1831. }
  1832. return (properties.GetUI1(NativeMethods.MESSAGE_PROPID_TRACE) != NativeMethods.MESSAGE_TRACE_NONE);
  1833. }
  1834. set
  1835. {
  1836. //If Default
  1837. if (!value)
  1838. {
  1839. this.filter.UseTracing = false;
  1840. properties.Remove(NativeMethods.MESSAGE_PROPID_TRACE);
  1841. }
  1842. else
  1843. {
  1844. this.filter.UseTracing = true;
  1845. if (!value)
  1846. properties.SetUI1(NativeMethods.MESSAGE_PROPID_TRACE, (byte)NativeMethods.MESSAGE_TRACE_NONE);
  1847. else
  1848. properties.SetUI1(NativeMethods.MESSAGE_PROPID_TRACE, (byte)NativeMethods.MESSAGE_TRACE_SEND_ROUTE_TO_REPORT_QUEUE);
  1849. }
  1850. }
  1851. }
  1852. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.AdjustMemory"]/*' />
  1853. /// <internalonly/>
  1854. internal void AdjustMemory()
  1855. {
  1856. if (filter.AdministrationQueue)
  1857. {
  1858. int size = properties.GetUI4(NativeMethods.MESSAGE_PROPID_ADMIN_QUEUE_LEN);
  1859. if (size > Message.DefaultQueueNameSize)
  1860. properties.SetString(NativeMethods.MESSAGE_PROPID_ADMIN_QUEUE, new byte[size * 2]);
  1861. }
  1862. if (filter.Body)
  1863. {
  1864. int size = properties.GetUI4(NativeMethods.MESSAGE_PROPID_BODY_SIZE);
  1865. if (size > DefaultBodySize)
  1866. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY, new byte[size]);
  1867. }
  1868. if (filter.AuthenticationProviderName)
  1869. {
  1870. int size = properties.GetUI4(NativeMethods.MESSAGE_PROPID_PROV_NAME_LEN);
  1871. if (size > Message.DefaultCryptographicProviderNameSize)
  1872. properties.SetString(NativeMethods.MESSAGE_PROPID_PROV_NAME, new byte[size * 2]);
  1873. }
  1874. if (filter.DestinationQueue)
  1875. {
  1876. int size = properties.GetUI4(NativeMethods.MESSAGE_PROPID_DEST_QUEUE_LEN);
  1877. if (size > Message.DefaultQueueNameSize)
  1878. properties.SetString(NativeMethods.MESSAGE_PROPID_DEST_QUEUE, new byte[size * 2]);
  1879. }
  1880. if (filter.Extension)
  1881. {
  1882. int size = properties.GetUI4(NativeMethods.MESSAGE_PROPID_EXTENSION_LEN);
  1883. if (size > DefaultExtensionSize)
  1884. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_EXTENSION, new byte[size]);
  1885. }
  1886. if (filter.TransactionStatusQueue)
  1887. {
  1888. int size = properties.GetUI4(NativeMethods.MESSAGE_PROPID_XACT_STATUS_QUEUE_LEN);
  1889. if (size > Message.DefaultQueueNameSize)
  1890. properties.SetString(NativeMethods.MESSAGE_PROPID_XACT_STATUS_QUEUE, new byte[size * 2]);
  1891. }
  1892. if (filter.Label)
  1893. {
  1894. int size = properties.GetUI4(NativeMethods.MESSAGE_PROPID_LABEL_LEN);
  1895. if (size > DefaultLabelSize)
  1896. properties.SetString(NativeMethods.MESSAGE_PROPID_LABEL, new byte[size * 2]);
  1897. }
  1898. if (filter.ResponseQueue)
  1899. {
  1900. int size = properties.GetUI4(NativeMethods.MESSAGE_PROPID_RESP_QUEUE_LEN);
  1901. if (size > Message.DefaultQueueNameSize)
  1902. properties.SetString(NativeMethods.MESSAGE_PROPID_RESP_QUEUE, new byte[size * 2]);
  1903. }
  1904. if (filter.SenderCertificate)
  1905. {
  1906. int size = properties.GetUI4(NativeMethods.MESSAGE_PROPID_SENDER_CERT_LEN);
  1907. if (size > Message.DefaultSenderCertificateSize)
  1908. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_SENDER_CERT, new byte[size]);
  1909. }
  1910. if (filter.SenderId)
  1911. {
  1912. int size = properties.GetUI4(NativeMethods.MESSAGE_PROPID_SENDERID_LEN);
  1913. if (size > Message.DefaultSenderIdSize)
  1914. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_SENDERID, new byte[size]);
  1915. }
  1916. if (filter.DestinationSymmetricKey)
  1917. {
  1918. int size = properties.GetUI4(NativeMethods.MESSAGE_PROPID_DEST_SYMM_KEY_LEN);
  1919. if (size > Message.DefaultSymmetricKeySize)
  1920. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_DEST_SYMM_KEY, new byte[size]);
  1921. }
  1922. if (filter.DigitalSignature)
  1923. {
  1924. int size = properties.GetUI4(NativeMethods.MESSAGE_PROPID_SIGNATURE_LEN);
  1925. if (size > Message.DefaultDigitalSignatureSize)
  1926. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_SIGNATURE, new byte[size]);
  1927. }
  1928. }
  1929. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.AdjustToSend"]/*' />
  1930. /// <internalonly/>
  1931. internal void AdjustToSend()
  1932. {
  1933. //Write cached properties
  1934. string queueFormatName;
  1935. if (this.filter.AdministrationQueue && this.cachedAdminQueue != null)
  1936. {
  1937. queueFormatName = this.cachedAdminQueue.FormatName;
  1938. properties.SetString(NativeMethods.MESSAGE_PROPID_ADMIN_QUEUE, StringToBytes(queueFormatName));
  1939. properties.SetUI4(NativeMethods.MESSAGE_PROPID_ADMIN_QUEUE_LEN, queueFormatName.Length);
  1940. }
  1941. if (this.filter.ResponseQueue && this.cachedResponseQueue != null)
  1942. {
  1943. queueFormatName = this.cachedResponseQueue.FormatName;
  1944. properties.SetString(NativeMethods.MESSAGE_PROPID_RESP_QUEUE, StringToBytes(queueFormatName));
  1945. properties.SetUI4(NativeMethods.MESSAGE_PROPID_RESP_QUEUE_LEN, queueFormatName.Length);
  1946. }
  1947. if (this.filter.TransactionStatusQueue && this.cachedTransactionStatusQueue != null)
  1948. {
  1949. queueFormatName = this.cachedTransactionStatusQueue.FormatName;
  1950. properties.SetString(NativeMethods.MESSAGE_PROPID_XACT_STATUS_QUEUE, StringToBytes(queueFormatName));
  1951. properties.SetUI4(NativeMethods.MESSAGE_PROPID_XACT_STATUS_QUEUE_LEN, queueFormatName.Length);
  1952. }
  1953. if (this.filter.Body && this.cachedBodyObject != null)
  1954. {
  1955. if (this.Formatter == null)
  1956. this.Formatter = new XmlMessageFormatter();
  1957. this.Formatter.Write(this, this.cachedBodyObject);
  1958. }
  1959. if (this.filter.Body && this.cachedBodyStream != null)
  1960. {
  1961. this.cachedBodyStream.Position = 0;
  1962. byte[] bytes = new byte[(int)this.cachedBodyStream.Length];
  1963. this.cachedBodyStream.Read(bytes, 0, bytes.Length);
  1964. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY, bytes);
  1965. properties.SetUI4(NativeMethods.MESSAGE_PROPID_BODY_SIZE, bytes.Length);
  1966. }
  1967. if (this.receiveCreated)
  1968. {
  1969. lock (this)
  1970. {
  1971. if (this.receiveCreated)
  1972. {
  1973. //We don't want to send the buffers as they were allocated
  1974. //when receiving, they might be to big.
  1975. //Adjust sizes
  1976. if (this.filter.Body)
  1977. {
  1978. int bodySize = properties.GetUI4(NativeMethods.MESSAGE_PROPID_BODY_SIZE);
  1979. byte[] bodyArray = properties.GetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY);
  1980. Debug.Assert(bodySize <= bodyArray.Length, "Allocated body array size is bigger than BODY_SIZE property");
  1981. if (bodySize < bodyArray.Length)
  1982. { // need to reallocate body array
  1983. byte[] bytes = new byte[bodySize];
  1984. Array.Copy(bodyArray, bytes, bodySize);
  1985. properties.SetUI1Vector(NativeMethods.MESSAGE_PROPID_BODY, bytes);
  1986. }
  1987. }
  1988. if (this.filter.Extension)
  1989. {
  1990. this.properties.AdjustSize(NativeMethods.MESSAGE_PROPID_EXTENSION,
  1991. this.properties.GetUI4(NativeMethods.MESSAGE_PROPID_EXTENSION_LEN));
  1992. }
  1993. if (this.filter.SenderCertificate)
  1994. {
  1995. this.properties.AdjustSize(NativeMethods.MESSAGE_PROPID_SENDER_CERT,
  1996. this.properties.GetUI4(NativeMethods.MESSAGE_PROPID_SENDER_CERT_LEN));
  1997. }
  1998. if (this.filter.DestinationSymmetricKey)
  1999. {
  2000. this.properties.AdjustSize(NativeMethods.MESSAGE_PROPID_DEST_SYMM_KEY,
  2001. this.properties.GetUI4(NativeMethods.MESSAGE_PROPID_DEST_SYMM_KEY_LEN));
  2002. }
  2003. //Ghost properties.
  2004. if (this.filter.Acknowledgment || this.filter.MessageType)
  2005. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_CLASS);
  2006. if (this.filter.ArrivedTime)
  2007. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_ARRIVEDTIME);
  2008. if (this.filter.Authenticated)
  2009. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_AUTHENTICATED);
  2010. if (this.filter.DestinationQueue)
  2011. {
  2012. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_DEST_QUEUE);
  2013. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_DEST_QUEUE_LEN);
  2014. this.cachedDestinationQueue = null;
  2015. }
  2016. if (this.filter.IsFirstInTransaction)
  2017. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_FIRST_IN_XACT);
  2018. if (this.filter.IsLastInTransaction)
  2019. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_LAST_IN_XACT);
  2020. if (this.filter.SenderId)
  2021. {
  2022. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_SENDERID);
  2023. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_SENDERID_LEN);
  2024. }
  2025. if (this.filter.SentTime)
  2026. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_SENTTIME);
  2027. if (this.filter.SourceMachine)
  2028. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_SRC_MACHINE_ID);
  2029. if (this.filter.TransactionId)
  2030. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_XACTID);
  2031. if (this.filter.SenderVersion)
  2032. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_VERSION);
  2033. //Ghost invalid returned properties
  2034. if (this.filter.AdministrationQueue)
  2035. {
  2036. if (properties.GetUI4(NativeMethods.MESSAGE_PROPID_ADMIN_QUEUE_LEN) == 0)
  2037. {
  2038. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_ADMIN_QUEUE);
  2039. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_ADMIN_QUEUE_LEN);
  2040. }
  2041. }
  2042. //Encryption algorithm cannot be set if not using Encryption
  2043. if (this.filter.EncryptionAlgorithm)
  2044. {
  2045. if ((this.filter.UseEncryption && !this.UseEncryption) || !this.filter.UseEncryption)
  2046. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_ENCRYPTION_ALG);
  2047. }
  2048. if (this.filter.DigitalSignature)
  2049. {
  2050. if (properties.GetUI4(NativeMethods.MESSAGE_PROPID_SIGNATURE_LEN) == 0)
  2051. {
  2052. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_SIGNATURE);
  2053. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_SIGNATURE_LEN);
  2054. }
  2055. }
  2056. if (this.filter.DestinationSymmetricKey)
  2057. {
  2058. if (properties.GetUI4(NativeMethods.MESSAGE_PROPID_DEST_SYMM_KEY_LEN) == 0)
  2059. {
  2060. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_DEST_SYMM_KEY);
  2061. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_DEST_SYMM_KEY_LEN);
  2062. }
  2063. }
  2064. if (this.filter.ResponseQueue)
  2065. {
  2066. if (properties.GetUI4(NativeMethods.MESSAGE_PROPID_RESP_QUEUE_LEN) == 0)
  2067. {
  2068. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_RESP_QUEUE);
  2069. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_RESP_QUEUE_LEN);
  2070. }
  2071. }
  2072. if (this.filter.TransactionStatusQueue)
  2073. {
  2074. if (properties.GetUI4(NativeMethods.MESSAGE_PROPID_XACT_STATUS_QUEUE_LEN) == 0)
  2075. {
  2076. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_XACT_STATUS_QUEUE);
  2077. this.properties.Ghost(NativeMethods.MESSAGE_PROPID_XACT_STATUS_QUEUE_LEN);
  2078. }
  2079. }
  2080. this.receiveCreated = false;
  2081. }
  2082. }
  2083. }
  2084. }
  2085. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.IdFromByteArray"]/*' />
  2086. /// <internalonly/>
  2087. private string IdFromByteArray(byte[] bytes)
  2088. {
  2089. StringBuilder result = new StringBuilder();
  2090. byte[] guidBytes = new byte[GenericIdSize];
  2091. Array.Copy(bytes, guidBytes, GenericIdSize);
  2092. int id = BitConverter.ToInt32(bytes, GenericIdSize);
  2093. result.Append((new Guid(guidBytes)).ToString());
  2094. result.Append("\\");
  2095. result.Append(id);
  2096. return result.ToString();
  2097. }
  2098. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.IdToByteArray"]/*' />
  2099. /// <internalonly/>
  2100. private byte[] IdToByteArray(string id)
  2101. {
  2102. string[] pieces = id.Split(new char[] { '\\' });
  2103. if (pieces.Length != 2)
  2104. throw new InvalidOperationException(Res.GetString(Res.InvalidId));
  2105. Guid guid;
  2106. try
  2107. {
  2108. guid = new Guid(pieces[0]);
  2109. }
  2110. catch (FormatException)
  2111. {
  2112. throw new InvalidOperationException(Res.GetString(Res.InvalidId));
  2113. }
  2114. int integerId;
  2115. try
  2116. {
  2117. integerId = Convert.ToInt32(pieces[1], CultureInfo.InvariantCulture);
  2118. }
  2119. catch (FormatException)
  2120. {
  2121. throw new InvalidOperationException(Res.GetString(Res.InvalidId));
  2122. }
  2123. catch (OverflowException)
  2124. {
  2125. throw new InvalidOperationException(Res.GetString(Res.InvalidId));
  2126. }
  2127. byte[] bytes = new byte[MessageIdSize];
  2128. Array.Copy(guid.ToByteArray(), bytes, GenericIdSize);
  2129. Array.Copy(BitConverter.GetBytes(integerId), 0, bytes, GenericIdSize, 4);
  2130. return bytes;
  2131. }
  2132. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.Lock"]/*' />
  2133. /// <internalonly/>
  2134. internal MessagePropertyVariants.MQPROPS Lock()
  2135. {
  2136. return this.properties.Lock();
  2137. }
  2138. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.StringFromBytes"]/*' />
  2139. /// <internalonly/>
  2140. internal static string StringFromBytes(byte[] bytes, int len)
  2141. {
  2142. //If the string ends with 0, lets trim it.
  2143. if (len != 0 && bytes[len * 2 - 1] == 0 && bytes[len * 2 - 2] == 0)
  2144. --len;
  2145. char[] charBuffer = new char[len];
  2146. Encoding.Unicode.GetChars(bytes, 0, len * 2, charBuffer, 0);
  2147. return new String(charBuffer, 0, len);
  2148. }
  2149. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.StringToBytes"]/*' />
  2150. /// <internalonly/>
  2151. internal static byte[] StringToBytes(string value)
  2152. {
  2153. int size = value.Length * 2 + 1;
  2154. byte[] byteBuffer = new byte[size];
  2155. byteBuffer[size - 1] = 0;
  2156. Encoding.Unicode.GetBytes(value.ToCharArray(), 0, value.Length, byteBuffer, 0);
  2157. return byteBuffer;
  2158. }
  2159. /// <include file='doc\Message.uex' path='docs/doc[@for="Message.Unlock"]/*' />
  2160. /// <internalonly/>
  2161. internal void Unlock()
  2162. {
  2163. this.properties.Unlock();
  2164. }
  2165. }
  2166. }