MessageQueueException.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //------------------------------------------------------------------------------
  2. // <copyright file="MessageQueueException.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.Diagnostics.CodeAnalysis;
  9. using System.Globalization;
  10. using System.Runtime.InteropServices;
  11. using System.Runtime.Serialization;
  12. using System.Security;
  13. using System.Text;
  14. namespace Experimental.System.Messaging
  15. {
  16. /// <include file='doc\MessageQueueException.uex' path='docs/doc[@for="MessageQueueException"]/*' />
  17. /// <devdoc>
  18. /// <para>
  19. /// Is thrown if a Microsoft Message
  20. /// Queue Server (MSMQ) internal error occurs.
  21. /// </para>
  22. /// </devdoc>
  23. [Serializable]
  24. [SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors")]
  25. public class MessageQueueException : ExternalException, ISerializable
  26. {
  27. private readonly int nativeErrorCode;
  28. /// <include file='doc\MessageQueueException.uex' path='docs/doc[@for="MessageQueueException.MessageQueueException"]/*' />
  29. /// <internalonly/>
  30. internal MessageQueueException(int error)
  31. {
  32. nativeErrorCode = error;
  33. }
  34. /// <include file='doc\MessageQueueException.uex' path='docs/doc[@for="MessageQueueException.MessageQueueException"]/*' />
  35. /// <internalonly/>
  36. protected MessageQueueException(SerializationInfo info, StreamingContext context)
  37. : base(info, context)
  38. {
  39. nativeErrorCode = info.GetInt32("NativeErrorCode");
  40. }
  41. /// <include file='doc\MessageQueueException.uex' path='docs/doc[@for="MessageQueueException.MessageQueueErrorCode"]/*' />
  42. /// <devdoc>
  43. /// <para>[To be supplied.]</para>
  44. /// </devdoc>
  45. public MessageQueueErrorCode MessageQueueErrorCode
  46. {
  47. get
  48. {
  49. return (MessageQueueErrorCode)nativeErrorCode;
  50. }
  51. }
  52. /// <include file='doc\MessageQueueException.uex' path='docs/doc[@for="MessageQueueException.Message"]/*' />
  53. /// <devdoc>
  54. /// <para>[To be supplied.]</para>
  55. /// </devdoc>
  56. public override string Message
  57. {
  58. get
  59. {
  60. try
  61. {
  62. return Res.GetString(Convert.ToString(nativeErrorCode, 16).ToUpper(CultureInfo.InvariantCulture));
  63. }
  64. catch
  65. {
  66. return GetUnknownErrorMessage(nativeErrorCode);
  67. }
  68. }
  69. }
  70. private static string GetUnknownErrorMessage(int error)
  71. {
  72. //get the system error message...
  73. string errorMsg = "";
  74. StringBuilder sb = new StringBuilder(256);
  75. int result = SafeNativeMethods.FormatMessage(SafeNativeMethods.FORMAT_MESSAGE_IGNORE_INSERTS |
  76. SafeNativeMethods.FORMAT_MESSAGE_FROM_SYSTEM |
  77. SafeNativeMethods.FORMAT_MESSAGE_ARGUMENT_ARRAY,
  78. IntPtr.Zero, error, 0, sb, sb.Capacity + 1, IntPtr.Zero);
  79. if (result != 0)
  80. {
  81. int i = sb.Length;
  82. while (i > 0)
  83. {
  84. char ch = sb[i - 1];
  85. if (ch > 32 && ch != '.') break;
  86. i--;
  87. }
  88. errorMsg = sb.ToString(0, i);
  89. }
  90. else
  91. {
  92. errorMsg = Res.GetString("UnknownError", Convert.ToString(error, 16));
  93. }
  94. return errorMsg;
  95. }
  96. /// <include file='doc\MessageQueueException.uex' path='docs/doc[@for="MessageQueueException.GetObjectData"]/*' />
  97. [SecurityCritical]
  98. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  99. {
  100. if (info == null)
  101. {
  102. throw new ArgumentNullException("info");
  103. }
  104. info.AddValue("NativeErrorCode", nativeErrorCode);
  105. base.GetObjectData(info, context);
  106. }
  107. }
  108. }