/
FaultException.cs
428 lines (358 loc) · 13.2 KB
/
FaultException.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Runtime;
using System.Runtime.Serialization;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
namespace System.ServiceModel
{
[Serializable]
[KnownType(typeof(FaultCodeData))]
[KnownType(typeof(FaultCodeData[]))]
[KnownType(typeof(FaultReasonData))]
[KnownType(typeof(FaultReasonData[]))]
public class FaultException : CommunicationException
{
internal const string Namespace = "http://schemas.xmlsoap.org/Microsoft/WindowsCommunicationFoundation/2005/08/Faults/";
public FaultException()
: base(SRP.SFxFaultReason)
{
Code = DefaultCode;
Reason = DefaultReason;
}
public FaultException(string reason)
: base(reason)
{
Code = DefaultCode;
Reason = CreateReason(reason);
}
public FaultException(FaultReason reason)
: base(GetSafeReasonText(reason))
{
Code = DefaultCode;
Reason = EnsureReason(reason);
}
public FaultException(string reason, FaultCode code)
: base(reason)
{
Code = EnsureCode(code);
Reason = CreateReason(reason);
}
public FaultException(FaultReason reason, FaultCode code)
: base(GetSafeReasonText(reason))
{
Code = EnsureCode(code);
Reason = EnsureReason(reason);
}
public FaultException(string reason, FaultCode code, string action)
: base(reason)
{
Code = EnsureCode(code);
Reason = CreateReason(reason);
Action = action;
}
internal FaultException(string reason, FaultCode code, string action, Exception innerException)
: base(reason, innerException)
{
Code = EnsureCode(code);
Reason = CreateReason(reason);
Action = action;
}
public FaultException(FaultReason reason, FaultCode code, string action)
: base(GetSafeReasonText(reason))
{
Code = EnsureCode(code);
Reason = EnsureReason(reason);
Action = action;
}
internal FaultException(FaultReason reason, FaultCode code, string action, Exception innerException)
: base(GetSafeReasonText(reason), innerException)
{
Code = EnsureCode(code);
Reason = EnsureReason(reason);
Action = action;
}
public FaultException(MessageFault fault)
: base(GetSafeReasonText(GetReason(fault)))
{
if (fault == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(fault));
}
Code = EnsureCode(fault.Code);
Reason = EnsureReason(fault.Reason);
Fault = fault;
}
public FaultException(MessageFault fault, string action)
: base(GetSafeReasonText(GetReason(fault)))
{
if (fault == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(fault));
}
Code = fault.Code;
Reason = fault.Reason;
Fault = fault;
Action = action;
}
protected FaultException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Code = ReconstructFaultCode(info, "code");
Reason = ReconstructFaultReason(info, "reason");
Fault = (MessageFault)info.GetValue("messageFault", typeof(MessageFault));
Action = info.GetString("action");
}
public string Action { get; }
public FaultCode Code { get; }
private static FaultReason DefaultReason
{
get { return new FaultReason(SRP.SFxFaultReason); }
}
private static FaultCode DefaultCode
{
get { return new FaultCode("Sender"); }
}
public override string Message
{
get { return GetSafeReasonText(Reason); }
}
public FaultReason Reason { get; }
internal MessageFault Fault { get; }
internal void AddFaultCodeObjectData(SerializationInfo info, string key, FaultCode code)
{
info.AddValue(key, FaultCodeData.GetObjectData(code));
}
internal void AddFaultReasonObjectData(SerializationInfo info, string key, FaultReason reason)
{
info.AddValue(key, FaultReasonData.GetObjectData(reason));
}
public static FaultException CreateFault(MessageFault messageFault, params Type[] faultDetailTypes)
{
return CreateFault(messageFault, null, faultDetailTypes);
}
public static FaultException CreateFault(MessageFault messageFault, string action, params Type[] faultDetailTypes)
{
if (messageFault == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(messageFault));
}
if (faultDetailTypes == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(faultDetailTypes));
}
DataContractSerializerFaultFormatter faultFormatter = new DataContractSerializerFaultFormatter(faultDetailTypes);
return faultFormatter.Deserialize(messageFault, action);
}
public virtual MessageFault CreateMessageFault()
{
if (Fault != null)
{
return Fault;
}
else
{
return MessageFault.CreateFault(Code, Reason);
}
}
private static FaultReason CreateReason(string reason)
{
return (reason != null) ? new FaultReason(reason) : DefaultReason;
}
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
AddFaultCodeObjectData(info, "code", Code);
AddFaultReasonObjectData(info, "reason", Reason);
info.AddValue("messageFault", Fault);
info.AddValue("action", Action);
}
private static FaultReason GetReason(MessageFault fault)
{
if (fault == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(fault));
}
return fault.Reason;
}
internal static string GetSafeReasonText(MessageFault messageFault)
{
if (messageFault == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(messageFault));
}
return GetSafeReasonText(messageFault.Reason);
}
internal static string GetSafeReasonText(FaultReason reason)
{
if (reason == null)
{
return SRP.SFxUnknownFaultNullReason0;
}
try
{
return reason.GetMatchingTranslation(Globalization.CultureInfo.CurrentCulture).Text;
}
catch (ArgumentException)
{
if (reason.Translations.Count == 0)
{
return SRP.SFxUnknownFaultZeroReasons0;
}
else
{
return SRP.Format(SRP.SFxUnknownFaultNoMatchingTranslation1, reason.Translations[0].Text);
}
}
}
private static FaultCode EnsureCode(FaultCode code)
{
return (code != null) ? code : DefaultCode;
}
private static FaultReason EnsureReason(FaultReason reason)
{
return (reason != null) ? reason : DefaultReason;
}
internal FaultCode ReconstructFaultCode(SerializationInfo info, string key)
{
FaultCodeData[] data = (FaultCodeData[])info.GetValue(key, typeof(FaultCodeData[]));
return FaultCodeData.Construct(data);
}
internal FaultReason ReconstructFaultReason(SerializationInfo info, string key)
{
FaultReasonData[] data = (FaultReasonData[])info.GetValue(key, typeof(FaultReasonData[]));
return FaultReasonData.Construct(data);
}
[Serializable]
internal class FaultCodeData
{
private string name;
private string ns;
internal static FaultCode Construct(FaultCodeData[] nodes)
{
FaultCode code = null;
for (int i = nodes.Length - 1; i >= 0; i--)
{
code = new FaultCode(nodes[i].name, nodes[i].ns, code);
}
return code;
}
internal static FaultCodeData[] GetObjectData(FaultCode code)
{
FaultCodeData[] array = new FaultCodeData[GetDepth(code)];
for (int i = 0; i < array.Length; i++)
{
array[i] = new FaultCodeData();
array[i].name = code.Name;
array[i].ns = code.Namespace;
code = code.SubCode;
}
Fx.Assert(code == null, "FaultException.FaultCodeData.GetObjectData: (code != null)");
return array;
}
private static int GetDepth(FaultCode code)
{
int depth = 0;
while (code != null)
{
depth++;
code = code.SubCode;
}
return depth;
}
}
[Serializable]
internal class FaultReasonData
{
private string xmlLang;
private string text;
internal static FaultReason Construct(FaultReasonData[] nodes)
{
FaultReasonText[] reasons = new FaultReasonText[nodes.Length];
for (int i = 0; i < nodes.Length; i++)
{
reasons[i] = new FaultReasonText(nodes[i].text, nodes[i].xmlLang);
}
return new FaultReason(reasons);
}
internal static FaultReasonData[] GetObjectData(FaultReason reason)
{
SynchronizedReadOnlyCollection<FaultReasonText> translations = reason.Translations;
FaultReasonData[] array = new FaultReasonData[translations.Count];
for (int i = 0; i < translations.Count; i++)
{
array[i] = new FaultReasonData();
array[i].xmlLang = translations[i].XmlLang;
array[i].text = translations[i].Text;
}
return array;
}
}
}
[Serializable]
[KnownType("GetKnownTypes")]
public class FaultException<TDetail> : FaultException
{
public FaultException(TDetail detail)
: base()
{
Detail = detail;
}
public FaultException(TDetail detail, string reason)
: base(reason)
{
Detail = detail;
}
public FaultException(TDetail detail, FaultReason reason)
: base(reason)
{
Detail = detail;
}
public FaultException(TDetail detail, string reason, FaultCode code)
: base(reason, code)
{
Detail = detail;
}
public FaultException(TDetail detail, FaultReason reason, FaultCode code)
: base(reason, code)
{
Detail = detail;
}
public FaultException(TDetail detail, string reason, FaultCode code, string action)
: base(reason, code, action)
{
Detail = detail;
}
public FaultException(TDetail detail, FaultReason reason, FaultCode code, string action)
: base(reason, code, action)
{
Detail = detail;
}
protected FaultException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Detail = (TDetail)info.GetValue("detail", typeof(TDetail));
}
public TDetail Detail { get; }
public override MessageFault CreateMessageFault()
{
return MessageFault.CreateFault(Code, Reason, Detail);
}
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("detail", Detail);
}
public override string ToString()
{
return SRP.Format(SRP.SFxFaultExceptionToString3, GetType(), Message, Detail != null ? Detail.ToString() : string.Empty);
}
private static Type[] s_knownTypes = new Type[] { typeof(TDetail) };
internal static IEnumerable<Type> GetKnownTypes()
{
return s_knownTypes;
}
}
}