1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel
{
using System.Text;
using System.Globalization;
using System.Runtime.Serialization;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.ServiceModel.Channels;
[Serializable]
public class MsmqException : ExternalException
{
[NonSerialized]
bool? faultSender = null;
[NonSerialized]
bool? faultReceiver = null;
[NonSerialized]
Type outerExceptionType = null;
public MsmqException()
{
}
public MsmqException(string message)
: base(message)
{
}
public MsmqException(string message, int error)
: base(message, error)
{
}
public MsmqException(string message, Exception inner)
: base(message, inner)
{
}
protected MsmqException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
internal bool FaultSender
{
get
{
TuneBehavior();
return this.faultSender.Value;
}
}
internal bool FaultReceiver
{
get
{
TuneBehavior();
return this.faultReceiver.Value;
}
}
void TuneBehavior()
{
if (this.faultSender.HasValue && this.faultReceiver.HasValue)
return;
switch (this.ErrorCode)
{
// configuration erors
case UnsafeNativeMethods.MQ_ERROR_ACCESS_DENIED:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(AddressAccessDeniedException); break;
case UnsafeNativeMethods.MQ_ERROR_NO_INTERNAL_USER_CERT:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(CommunicationException); break;
case UnsafeNativeMethods.MQ_ERROR_QUEUE_DELETED:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(EndpointNotFoundException); break;
case UnsafeNativeMethods.MQ_ERROR_QUEUE_NOT_FOUND:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(EndpointNotFoundException); break;
case UnsafeNativeMethods.MQ_ERROR_CERTIFICATE_NOT_PROVIDED:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(CommunicationException); break;
case UnsafeNativeMethods.MQ_ERROR_INVALID_CERTIFICATE:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(CommunicationException); break;
case UnsafeNativeMethods.MQ_ERROR_CANNOT_CREATE_CERT_STORE:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(CommunicationException); break;
case UnsafeNativeMethods.MQ_ERROR_CORRUPTED_PERSONAL_CERT_STORE:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(CommunicationException); break;
case UnsafeNativeMethods.MQ_ERROR_COULD_NOT_GET_USER_SID:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(CommunicationException); break;
case UnsafeNativeMethods.MQ_ERROR_ILLEGAL_FORMATNAME:
faultSender = false; faultReceiver = false; outerExceptionType = typeof(ArgumentException); break;
case UnsafeNativeMethods.MQ_ERROR_ILLEGAL_QUEUE_PATHNAME:
faultSender = false; faultReceiver = false; outerExceptionType = typeof(ArgumentException); break;
case UnsafeNativeMethods.MQ_ERROR_UNSUPPORTED_FORMATNAME_OPERATION:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(ArgumentException); break;
case UnsafeNativeMethods.MQ_ERROR_CANNOT_HASH_DATA_EX:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(CommunicationException); break;
case UnsafeNativeMethods.MQ_ERROR_CANNOT_SIGN_DATA_EX:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(CommunicationException); break;
case UnsafeNativeMethods.MQ_ERROR_FAIL_VERIFY_SIGNATURE_EX:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(CommunicationException); break;
case UnsafeNativeMethods.MQ_ERROR_BAD_SECURITY_CONTEXT:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(CommunicationException); break;
case UnsafeNativeMethods.MQ_ERROR_PRIVILEGE_NOT_HELD:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(CommunicationException); break;
case UnsafeNativeMethods.MQ_ERROR_SHARING_VIOLATION:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(AddressAccessDeniedException); break;
// transient errors
case UnsafeNativeMethods.MQ_ERROR_DTC_CONNECT:
faultSender = false; faultReceiver = true; outerExceptionType = typeof(CommunicationException); break;
case UnsafeNativeMethods.MQ_ERROR_IO_TIMEOUT:
faultSender = false; faultReceiver = false; outerExceptionType = typeof(TimeoutException); break;
case UnsafeNativeMethods.MQ_ERROR_QUEUE_NOT_AVAILABLE:
faultSender = false; faultReceiver = true; outerExceptionType = typeof(EndpointNotFoundException); break;
case UnsafeNativeMethods.MQ_ERROR_REMOTE_MACHINE_NOT_AVAILABLE:
faultSender = false; faultReceiver = true; outerExceptionType = typeof(EndpointNotFoundException); break;
case UnsafeNativeMethods.MQ_ERROR_SERVICE_NOT_AVAILABLE:
faultSender = false; faultReceiver = true; outerExceptionType = typeof(EndpointNotFoundException); break;
case UnsafeNativeMethods.MQ_ERROR_INSUFFICIENT_RESOURCES:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(CommunicationException); break;
case UnsafeNativeMethods.MQ_ERROR_MESSAGE_STORAGE_FAILED:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(CommunicationException); break;
case UnsafeNativeMethods.MQ_ERROR_TRANSACTION_ENLIST:
faultSender = false; faultReceiver = true; outerExceptionType = typeof(CommunicationException); break;
case UnsafeNativeMethods.MQ_ERROR_TRANSACTION_IMPORT:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(CommunicationException); break;
case UnsafeNativeMethods.MQ_ERROR_TRANSACTION_USAGE:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(InvalidOperationException); break;
case UnsafeNativeMethods.MQ_ERROR_STALE_HANDLE:
faultSender = false; faultReceiver = false; outerExceptionType = typeof(InvalidOperationException); break;
// malformed messages
case UnsafeNativeMethods.MQ_ERROR_ILLEGAL_MQQMPROPS:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(CommunicationException); break;
case UnsafeNativeMethods.MQ_ERROR_INSUFFICIENT_PROPERTIES:
faultSender = true; faultReceiver = true; outerExceptionType = typeof(CommunicationException); break;
default:
faultSender = true; faultReceiver = true; outerExceptionType = null; break;
}
}
internal Exception Normalized
{
get
{
TuneBehavior();
if (null != this.outerExceptionType)
return Activator.CreateInstance(this.outerExceptionType, new object[] { this.Message, this }) as Exception;
else
return this;
}
}
}
static class MsmqError
{
[ResourceConsumption(ResourceScope.Process)]
public static string GetErrorString(int error)
{
StringBuilder stringBuilder = new StringBuilder(512);
bool result = false;
if ((error & 0x0FFF0000) == 0x000E0000)
{
int formatFlags = UnsafeNativeMethods.FORMAT_MESSAGE_IGNORE_INSERTS
| UnsafeNativeMethods.FORMAT_MESSAGE_ARGUMENT_ARRAY
| UnsafeNativeMethods.FORMAT_MESSAGE_FROM_HMODULE;
result = (0 != UnsafeNativeMethods.FormatMessage(
formatFlags,
Msmq.ErrorStrings,
error,
CultureInfo.CurrentCulture.LCID,
stringBuilder,
stringBuilder.Capacity,
IntPtr.Zero));
}
else
{
int formatFlags = UnsafeNativeMethods.FORMAT_MESSAGE_IGNORE_INSERTS
| UnsafeNativeMethods.FORMAT_MESSAGE_ARGUMENT_ARRAY
| UnsafeNativeMethods.FORMAT_MESSAGE_FROM_SYSTEM;
result = (0 != UnsafeNativeMethods.FormatMessage(
formatFlags,
IntPtr.Zero,
error,
CultureInfo.CurrentCulture.LCID,
stringBuilder,
stringBuilder.Capacity,
IntPtr.Zero));
}
if (result)
{
stringBuilder = stringBuilder.Replace("\n", "");
stringBuilder = stringBuilder.Replace("\r", "");
return SR.GetString(
SR.MsmqKnownWin32Error,
stringBuilder.ToString(),
error.ToString(CultureInfo.InvariantCulture),
Convert.ToString(error, 16));
}
else
{
return SR.GetString(
SR.MsmqUnknownWin32Error,
error.ToString(CultureInfo.InvariantCulture),
Convert.ToString(error, 16));
}
}
}
}
|