FaultException.cs 15 KB

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