Advertisement
Guest User

Untitled

a guest
Aug 24th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. [System.ServiceModel.FaultContractAttribute(typeof(FaultClass[]), Action = "http://whatever/", Name = "whateverBusinessFault")]
  2.  
  3. try
  4. {
  5. // call service here
  6. }
  7. catch (FaultException<FaultClass[]> ex)
  8. {
  9. if (ex.Detail != null && ex.Detail.Length > 0)
  10. {
  11. throw new CustomException(ex.Detail[0].description);
  12. }
  13. else
  14. {
  15. throw;
  16. }
  17. }
  18.  
  19. var sb = new StringBuilder();
  20.  
  21. using (XmlReader reader = fault.GetReaderAtDetailContents())
  22. {
  23. while (reader.Read())
  24. sb.AppendLine(reader.ReadOuterXml());
  25. }
  26.  
  27. var detail = sb.ToString();
  28.  
  29. try
  30. {
  31. proxy.CallSomeMethod();
  32. }
  33. catch (FaultException ex)
  34. {
  35. var fault = ex.CreateMessageFault();
  36. using (XmlReader reader = fault.GetReaderAtDetailContents())
  37. {
  38. // TODO: read the XML fault and extract the necessary information.
  39. }
  40. }
  41.  
  42. [ServiceContract]
  43. public interface IService1
  44. {
  45. [OperationContract]
  46. [FaultContract(typeof(FaultClass[]))]
  47. string Crash();
  48. }
  49.  
  50. public class Service1 : IService1
  51. {
  52. public string Crash()
  53. {
  54. var exception = new FaultException<FaultClass[]>(new FaultClass[] { new FaultClass { Data = "TEST" } }, new FaultReason("Boom"));
  55.  
  56. throw exception;
  57. }
  58. }
  59.  
  60. [DataContract]
  61. public class FaultClass
  62. {
  63. [DataMember]
  64. public string Data { get; set; }
  65. }
  66.  
  67. try
  68. {
  69. using (var client = new Service1Client())
  70. {
  71. client.Crash();
  72. }
  73. }
  74. catch(FaultException<FaultClass[]> e)
  75. {
  76. //Break here
  77. }
  78.  
  79. public static string GetDetail(this FaultException faultException)
  80. {
  81. if (faultException == null)
  82. throw new ArgumentNullException(nameof(faultException));
  83.  
  84. MessageFault messageFault = faultException.CreateMessageFault();
  85. if (messageFault.HasDetail) {
  86. using (XmlDictionaryReader reader = messageFault.GetReaderAtDetailContents()) {
  87. return reader.ReadContentAsString();
  88. }
  89. }
  90. return null;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement