Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [System.ServiceModel.FaultContractAttribute(typeof(FaultClass[]), Action = "http://whatever/", Name = "whateverBusinessFault")]
- try
- {
- // call service here
- }
- catch (FaultException<FaultClass[]> ex)
- {
- if (ex.Detail != null && ex.Detail.Length > 0)
- {
- throw new CustomException(ex.Detail[0].description);
- }
- else
- {
- throw;
- }
- }
- var sb = new StringBuilder();
- using (XmlReader reader = fault.GetReaderAtDetailContents())
- {
- while (reader.Read())
- sb.AppendLine(reader.ReadOuterXml());
- }
- var detail = sb.ToString();
- try
- {
- proxy.CallSomeMethod();
- }
- catch (FaultException ex)
- {
- var fault = ex.CreateMessageFault();
- using (XmlReader reader = fault.GetReaderAtDetailContents())
- {
- // TODO: read the XML fault and extract the necessary information.
- }
- }
- [ServiceContract]
- public interface IService1
- {
- [OperationContract]
- [FaultContract(typeof(FaultClass[]))]
- string Crash();
- }
- public class Service1 : IService1
- {
- public string Crash()
- {
- var exception = new FaultException<FaultClass[]>(new FaultClass[] { new FaultClass { Data = "TEST" } }, new FaultReason("Boom"));
- throw exception;
- }
- }
- [DataContract]
- public class FaultClass
- {
- [DataMember]
- public string Data { get; set; }
- }
- try
- {
- using (var client = new Service1Client())
- {
- client.Crash();
- }
- }
- catch(FaultException<FaultClass[]> e)
- {
- //Break here
- }
- public static string GetDetail(this FaultException faultException)
- {
- if (faultException == null)
- throw new ArgumentNullException(nameof(faultException));
- MessageFault messageFault = faultException.CreateMessageFault();
- if (messageFault.HasDetail) {
- using (XmlDictionaryReader reader = messageFault.GetReaderAtDetailContents()) {
- return reader.ReadContentAsString();
- }
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement