public void TestToString() { FaultException <int> e = new FaultException <int> (0); Assert.AreEqual( String.Format("{0}: {1} (Fault Detail is equal to {2}).", e.GetType(), e.Message, e.Detail), e.ToString()); }
void HandleFaultException( FaultException faultException, ref Message fault) { // In a SOAP scenario simply return - the code threw (Web)FaultException and WCF downstream will handle it properly either way. // Also in a WebHttp scenario: if the code threw WebFaultException - return - WCF downstream will handle it properly. if (!_wcfContext.HasWebOperationContext || faultException is WebFaultException) { return; } try { // This is the case of a FaultException in a Web context. var faultExceptionType = faultException.GetType(); if (faultExceptionType.IsGenericType) { // it is FaultException<T> - extract the detail and build the message var faultDetail = faultExceptionType .GetProperty("Detail") .GetGetMethod() .Invoke(faultException, null); BuildHttpResponseMessage( faultDetail, _wcfContext.GetFaultedAction(faultDetail.GetType()) ?? fault.Headers.Action, (faultDetail as Fault)?.HttpStatusCode ?? HttpStatusCode.InternalServerError, ref fault); } else { BuildHttpResponseMessage( faultException.Message, fault.Headers.Action, HttpStatusCode.InternalServerError, ref fault); } } catch (Exception unhandledException) { // There was an error during MessageFault build process, so treat it as an Unhandled Exception // log the exception and send an unhandled server exception var handlingInstanceId = LogServerException(unhandledException); HandleFault(unhandledException, ref fault, handlingInstanceId, null); } }
private static MethodFault GetVIServerFault(FaultException soapEx) { if (soapEx == null) { throw new ArgumentNullException("soapEx"); } PropertyInfo property = soapEx.GetType().GetTypeInfo().GetProperty("Detail"); VimApi_65.MethodFault methodFault; if (property != (PropertyInfo)null) { methodFault = property.GetValue((object)soapEx) as VimApi_65.MethodFault; } else { MessageFault messageFault = soapEx.CreateMessageFault(); XmlSerializer xmlSerializer = FaultUtility.GetFaultSerializer(); if (xmlSerializer == null) { /* XElement xelement = (XElement)XNode.ReadFrom((XmlReader)messageFault.GetReaderAtDetailContents()); * System.Type type = typeof(VimApi_65.MethodFault).Assembly.GetType(string.Format("{0}.{1}", (object)typeof(VimApi_65.MethodFault).Namespace, (object)xelement.Attribute(XName.Get("type", "http://www.w3.org/2001/XMLSchema-instance")).Value)); * XmlRootAttribute xmlRootAttribute = new XmlRootAttribute(xelement.Name.LocalName); * // ISSUE: variable of the null type * __Null local1 = null; * // ISSUE: variable of the null type * __Null local2 = null; * XmlRootAttribute root = xmlRootAttribute; * string defaultNamespace = "urn:vim25"; * xmlSerializer = new XmlSerializer(type, (XmlAttributeOverrides)local1, (System.Type[])local2, root, defaultNamespace); */ XElement xElement = (XElement)XNode.ReadFrom(messageFault.GetReaderAtDetailContents()); XAttribute xAttribute = xElement.Attribute(XName.Get("type", "http://www.w3.org/2001/XMLSchema-instance")); string name = string.Format("{0}.{1}", typeof(VimApi_65.MethodFault).Namespace, xAttribute.Value); Type arg_D0_0 = typeof(VimApi_65.MethodFault).Assembly.GetType(name); XmlRootAttribute root = new XmlRootAttribute(xElement.Name.LocalName); xmlSerializer = new XmlSerializer(arg_D0_0, null, null, root, "urn:vim25"); } StringReader stringReader = new StringReader(messageFault.GetDetail <XElement>().ToString().Replace("xsd:", "")); using (XmlReader xmlReader = XmlReader.Create((TextReader)stringReader, new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Fragment, CloseInput = true })) { int content = (int)xmlReader.MoveToContent(); methodFault = xmlSerializer.Deserialize(xmlReader) as VimApi_65.MethodFault; } } return(VIConvert.ToVim((object)methodFault) as MethodFault); }
protected static Type FaultExceptionType(FaultException fe) { if (fe == null) { throw new ArgumentNullException(nameof(fe)); } Type faultType = fe.GetType(); if (faultType.IsGenericType) { return(faultType.GetGenericArguments()[0]); } else { return(faultType); } }
/// <summary> /// This method will execute whenever any exception will be thrown from WCF /// </summary> /// <param name="error"></param> /// <param name="version"></param> /// <param name="fault"></param> public void ProvideFault(Exception error, MessageVersion version, ref Message fault) { var faultException = new FaultException <Exception>(error); object detail = faultException.GetType().GetProperty("Detail").GetGetMethod().Invoke(faultException, null); fault = Message.CreateMessage(version, "", detail, new DataContractSerializer(detail.GetType())); var webBodyFormatMessageProp = new WebBodyFormatMessageProperty(WebContentFormat.Xml); fault.Properties.Add(WebBodyFormatMessageProperty.Name, webBodyFormatMessageProp); var httpResponseMessageProp = new HttpResponseMessageProperty(); httpResponseMessageProp.Headers[HttpResponseHeader.ContentType] = "application/xml"; httpResponseMessageProp.StatusCode = HttpStatusCode.BadRequest; httpResponseMessageProp.StatusDescription = error.Message.Replace(System.Environment.NewLine, string.Empty); fault.Properties.Add(HttpResponseMessageProperty.Name, httpResponseMessageProp); //IoC.Kernel.Get<IErrorLogManager>().LogError(error, HttpContext.Current, httpResponseMessageProp); }
public MessageFault Serialize(FaultException faultException, out string action) { XmlObjectSerializer serializer = null; Type detailType = null; string faultExceptionAction = action = faultException.Action; Type faultExceptionOfT = null; for (Type faultType = faultException.GetType(); faultType != typeof(FaultException); faultType = faultType.GetTypeInfo().BaseType) { if (faultType.GetTypeInfo().IsGenericType&& (faultType.GetGenericTypeDefinition() == typeof(FaultException <>))) { faultExceptionOfT = faultType; break; } } if (faultExceptionOfT != null) { detailType = faultExceptionOfT.GetGenericArguments()[0]; serializer = GetSerializer(detailType, faultExceptionAction, out action); } return(CreateMessageFault(serializer, faultException, detailType)); }
public MessageFault Serialize(FaultException faultException, out string action) { XmlObjectSerializer serializer = null; System.Type detailType = null; string faultExceptionAction = action = faultException.Action; System.Type type2 = null; for (System.Type type3 = faultException.GetType(); type3 != typeof(FaultException); type3 = type3.BaseType) { if (type3.IsGenericType && (type3.GetGenericTypeDefinition() == typeof(FaultException <>))) { type2 = type3; break; } } if (type2 != null) { detailType = type2.GetGenericArguments()[0]; serializer = this.GetSerializer(detailType, faultExceptionAction, out action); } return(CreateMessageFault(serializer, faultException, detailType)); }
public static object GetFaultDetail(this FaultException faultEx) => faultEx.GetType().GetProperties().FirstOrDefault(p => p.Name == "Detail")?.GetValue(faultEx, null);
/// <summary> /// Initializes a new instance of the <see cref="FaultExceptionViewModel"/> class. /// </summary> /// <param name="faultException"> /// A <see cref="FaultException"/> instance. /// </param> public FaultExceptionViewModel(FaultException faultException) : base(null, faultException.GetType().FullName, true) { this._faultException = faultException; this.IsExpanded = true; }
/// <summary> /// 解析异常类型 /// </summary> /// <param name="innerExp">Exception对象</param> /// <returns>返回详细信息</returns> private static string HandleException(Exception innerExp) { StringBuilder builder = new StringBuilder(); while (innerExp != null) { //服务端异常 FaultException <string> faultEx = innerExp as FaultException <string>; if (faultEx != null) { builder.AppendFormat("-------------------{0}:{1}-----------------\r\n", faultEx.GetType().Name, faultEx.Message); builder.AppendLine(faultEx.Detail); break; } //SOAP 错误 FaultException faultEx1 = innerExp as FaultException; if (faultEx1 != null) { builder.AppendLine(faultEx1.ToString()); break; } //给进程或操作分配的时间过期时引发的异常 TimeoutException timeoutEx = innerExp as TimeoutException; if (timeoutEx != null) { builder.AppendLine("连接服务器发生异常,超时!"); builder.AppendLine(timeoutEx.ToString()); break; } //服务或客户端应用程序中的通信错误。 CommunicationException commuEx = innerExp as CommunicationException; if (commuEx != null) { builder.AppendLine("连接服务器发生异常,通信错误!"); builder.AppendLine(commuEx.ToString()); break; } builder.AppendFormat("-----------------{0} : {1}--------------------\r\n{2}", innerExp.GetType().Name, innerExp.Message, innerExp.StackTrace); innerExp = innerExp.InnerException; } return(builder.ToString()); }