예제 #1
0
        public void TestCreateMessageFault()
        {
            FaultException <int> e = new FaultException <int> (0);                            Assert.IsFalse(
                (object)MessageFault.CreateFault(e.Code, e.Reason, e.Detail)
                == e.CreateMessageFault(), "#1");

            AreMessageFaultEqual(
                MessageFault.CreateFault(e.Code, e.Reason, e.Detail),
                e.CreateMessageFault(), "#2");
        }
예제 #2
0
        public void ShouldGetFaultExceptionWithoutPolicy()
        {
            ExceptionShieldingErrorHandler shielding = new ExceptionShieldingErrorHandler();
            FaultException faultException            = GetFaultException("test", SoapException.ServerFaultCode.Name);
            Message        message = Message.CreateMessage(MessageVersion.Default, faultException.CreateMessageFault(), "");

            shielding.ProvideFault(faultException, MessageVersion.Default, ref message);

            MessageFault actualFault   = GetFaultFromMessage(message);
            MessageFault expectedFault = faultException.CreateMessageFault();

            Assert.AreEqual(expectedFault.Reason.ToString(), actualFault.Reason.ToString());
            Assert.AreEqual(expectedFault.HasDetail, actualFault.HasDetail);
            Assert.AreEqual(expectedFault.Code.IsReceiverFault, actualFault.Code.IsReceiverFault);
        }
예제 #3
0
 public static void Main(string [] args)
 {
     try {
         if (args.Length > 1)
         {
             Run(args [0], args [1]);
         }
         else
         {
             Run(null, null);
         }
     } catch (MessageSecurityException ex) {
         Console.WriteLine("MessageSecurityException::::::");
         FaultException f = ex.InnerException as FaultException;
         if (f != null)
         {
             using (XmlWriter w = XmlWriter.Create(Console.Out)) {
                 f.CreateMessageFault().WriteTo(w, EnvelopeVersion.Soap12);
             }
         }
         else
         {
             Console.WriteLine(ex);
         }
     } catch (WebException ex) {
         Console.WriteLine(ex);
         using (StreamReader sr = new StreamReader(ex.Response.GetResponseStream()))
             Console.WriteLine(sr.ReadToEnd());
     }
 }
예제 #4
0
        public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
        {
            var faultEx = new FaultException <ApiFault>(new ApiFault(error.Message), new FaultReason(error.Message));

            fault = Message.CreateMessage(version, faultEx.CreateMessageFault(), "error");

            fault.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Xml));

            WebOperationContext.Current.OutgoingResponse.StatusDescription = error.Message;
            //WebOperationContext.Current.OutgoingResponse.SuppressEntityBody = true;

            // specal hack to prevent forms auth redirection
            var code = error is ApiException ? ((ApiException)error).StatusCode : System.Net.HttpStatusCode.InternalServerError;

            if (error is System.Runtime.Serialization.SerializationException)
            {
                code = HttpStatusCode.BadRequest;
            }

            WebOperationContext.Current.OutgoingResponse.StatusCode = code == HttpStatusCode.Unauthorized ? HttpStatusCode.Unauthorized : code;
            if (error is ApiException)
            {
                WebOperationContext.Current.OutgoingResponse.Headers["error_id"] = ((ApiException)error).ErrorId;
            }
        }
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            FaultException fe      = new FaultException();
            MessageFault   message = fe.CreateMessageFault();

            fault = Message.CreateMessage(version, message, null);
        }
예제 #6
0
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            FaultException faultException = new FaultException(error.Message);
            MessageFault   messageFault   = faultException.CreateMessageFault();

            fault = Message.CreateMessage(version, messageFault, faultException.Action);
        }
예제 #7
0
        public void ProvideFault(Exception error, MessageVersion version, ref Message msg)
        {
            var faultException = new FaultException(error.Message);
            var messageFault   = faultException.CreateMessageFault();

            msg = Message.CreateMessage(version, messageFault, faultException.Action);
        }
예제 #8
0
        /// <summary>
        /// Generates the fault message from fault code
        /// </summary>
        /// <param name="requestMessage">indicating the request message</param>
        /// <param name="version">message version</param>
        /// <param name="faultException">indicating the fault exception</param>
        /// <returns>fault message</returns>
        public static Message GenerateFaultMessage(Message requestMessage, MessageVersion version, FaultException faultException)
        {
            MessageFault fault        = faultException.CreateMessageFault();
            UniqueId     relatesTo    = null;
            Message      faultMessage = Message.CreateMessage(version, fault, faultException.Action);

            if (requestMessage != null)
            {
                // add action
                faultMessage.Headers.Add(MessageHeader.CreateHeader(Constant.ActionHeaderName, Constant.HpcHeaderNS, requestMessage.Headers.Action));

                // add user data
                int index = requestMessage.Headers.FindHeader(Constant.UserDataHeaderName, Constant.HpcHeaderNS);
                if (index >= 0)
                {
                    faultMessage.Headers.CopyHeaderFrom(requestMessage, index);
                }

                relatesTo = requestMessage.Headers.MessageId;
            }

            // Only add relatesTo header to WSAddressing messages
            if (relatesTo != null && version.Addressing == AddressingVersion.WSAddressing10)
            {
                faultMessage.Headers.RelatesTo = relatesTo;
            }

            return(faultMessage);
        }
        internal static ReportServerException FromException(Exception e)
        {
            ReportServerException ex = e as ReportServerException;

            if (ex != null)
            {
                return(ex);
            }
            FaultException ex2 = e as FaultException;

            if (ex2 != null)
            {
                var fault                 = ex2.CreateMessageFault();
                var detail                = fault.GetReaderAtDetailContents();
                var detailDocument        = new XmlDocument();
                var detailNode            = detailDocument.ReadNode(detail);
                ReportServerException ex3 = FromMoreInformationNode(GetNestedMoreInformationNode(detailNode));
                if (ex3 != null)
                {
                    return(ex3);
                }
            }
            else
            {
                if (e is RSExecutionConnection.MissingEndpointException)
                {
                    return(new ReportServerException(e.Message, null, new MissingEndpointException(e.Message, e.InnerException)));
                }
                if (e is RSExecutionConnection.SoapVersionMismatchException)
                {
                    return(new ReportServerException(e.Message, null, new SoapVersionMismatchException(e.Message, e.InnerException)));
                }
            }
            return(new ReportServerException(e.Message, null, e));
        }
예제 #10
0
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            FaultException <ServiceException> faultException = GetFaultException(error);
            MessageFault messageFault = faultException.CreateMessageFault();

            fault = Message.CreateMessage(version, messageFault, faultException.Action);
        }
        private void HandleFaultException(FaultException ex, SoapContext <TService> context)
        {
            _logger.LogWarning(ex, $"Fault exception: {ex.Message}");
            var messageFault = ex.CreateMessageFault();

            context.Response = FaultMessage.CreateFaultMessage(context.MessageVersion, messageFault, context.Request.Headers.Action);
        }
예제 #12
0
        /// <summary>
        ///    Enables the creation of a custom System.ServiceModel.FaultException{TDetail}
        ///    that is returned from an exception in the course of a service method.
        /// </summary>
        /// <param name="error">The System.Exception object thrown in the course of the service operation.</param>
        /// <param name="version">The SOAP version of the message.</param>
        /// <param name="fault">
        ///    The System.ServiceModel.Channels.Message object that is returned to the client, or service in
        ///    duplex case
        /// </param>
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            if (error is FaultException <ApplicationServiceError> )
            {
                var messageFault = ((FaultException <ApplicationServiceError>)error).CreateMessageFault();

                //propagate FaultException
                fault = Message.CreateMessage(
                    version,
                    messageFault,
                    ((FaultException <ApplicationServiceError>)error).Action);
            }
            else
            {
                //create service error
                var defaultError = new ApplicationServiceError()
                {
                    ErrorMessage = Messages.message_DefaultErrorMessage
                };

                //Create fault exception and message fault
                var defaultFaultException = new FaultException <ApplicationServiceError>(defaultError);
                var defaultMessageFault   = defaultFaultException.CreateMessageFault();

                //propagate FaultException
                fault = Message.CreateMessage(version, defaultMessageFault, defaultFaultException.Action);
            }
        }
예제 #13
0
        void IErrorHandler.ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            if (error == null)
            {
                throw new ArgumentNullException("error");
            }

            string errorId = GenerateErrorCode();

            error.HelpLink = errorId;

            var errorMessage = new StringBuilder();

            errorMessage.AppendFormat("Application server - {0} - error with code: {1}",
                                      Environment.MachineName, errorId);
            if (IncludeInnerException)
            {
                errorMessage.Append(Environment.NewLine).Append(ExceptionToString(error));
            }

            var          fe = new FaultException(errorMessage.ToString(), new FaultCode(errorId));
            MessageFault mf = fe.CreateMessageFault();

            fault = Message.CreateMessage(version, mf, String.Empty);
        }
예제 #14
0
        public Exception CapturarSoapError(Exception ex)
        {
            try
            {
                FaultException faultException = ex as FaultException;
                MessageFault   msgFault       = faultException != null?faultException.CreateMessageFault() : null;

                XmlElement elm = msgFault != null?msgFault.GetDetail <XmlElement>() : null;

                if (elm != null)
                {
                    var errorCodeTag        = elm.GetElementsByTagName("ErrorCode");
                    var errorDescripcionTag = elm.GetElementsByTagName("ErrorDescription");

                    if (errorCodeTag != null && errorCodeTag.Count > 0 && errorDescripcionTag != null && errorDescripcionTag.Count > 0)
                    {
                        ex = new Exception(errorCodeTag[0].InnerText + " " + errorDescripcionTag[0].InnerText);
                    }
                }
            }
            catch
            {
            }
            return(ex);
        }
예제 #15
0
 private static void LogFaultException(FaultException ex)
 {
     if (ex == null)
     {
         return;
     }
     try
     {
         TraceHelper.WriteError("Fault Exception");
         TraceHelper.WriteError("Fault Message: {0}", ex.Message);
         TraceHelper.WriteError("Fault Action: {0}", ex.Action);
         TraceHelper.WriteError("Fault Code: {0}-{1}", ex.Code.Name, ex.Code);
         TraceHelper.WriteError("Fault Reason: {0}", ex.Reason);
         MessageFault fault = ex.CreateMessageFault();
         if (fault.HasDetail)
         {
             XmlReader reader = fault.GetReaderAtDetailContents();
             if (reader != null && reader.Name == "ExceptionDetail")
             {
                 var detail = fault.GetDetail <ExceptionDetail>();
                 if (detail != null)
                 {
                     TraceHelper.WriteError("-Detail Message: {0}", detail.Message);
                     TraceHelper.WriteError("-Detail Stack: {0}", detail.StackTrace);
                 }
             }
         }
     }
     catch (Exception e)
     {
         TraceHelper.WriteError("Error handling Fault Exception: {0}", e.Message);
     }
 }
        public virtual void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            var          faultException = new FaultException(error.Message);
            MessageFault messageFault   = faultException.CreateMessageFault();

            fault = Message.CreateMessage(version, messageFault, "Error");
        }
예제 #17
0
        /// <summary>
        /// Enables the creation of a custom System.ServiceModel.FaultException{TDetail}
        /// that is returned from an exception in the course of a service method.
        /// </summary>
        /// <param name="error">The System.Exception object thrown in the course of the service operation.</param>
        /// <param name="version">The SOAP version of the message.</param>
        /// <param name="fault">The System.ServiceModel.Channels.Message object that is returned to the client, or service in duplex case</param>
        public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
        {
            if (error is FaultException <ApplicationServiceError> )
            {
                MessageFault messageFault = ((FaultException <ApplicationServiceError>)error).CreateMessageFault();

                //propagate FaultException
                fault = Message.CreateMessage(version, messageFault, ((FaultException <ApplicationServiceError>)error).Action);
            }
            else
            {
                //create service error
                ApplicationServiceError defaultError = new ApplicationServiceError()
                {
                    ErrorMessage = Resources.Messages.message_DefaultErrorMessage
                };

                //Create fault exception and message fault
                FaultException <ApplicationServiceError> defaultFaultException = new FaultException <ApplicationServiceError>(defaultError);
                MessageFault defaultMessageFault = defaultFaultException.CreateMessageFault();

                //propagate FaultException
                fault = Message.CreateMessage(version, defaultMessageFault, defaultFaultException.Action);
            }
        }
                public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
                {
                    var exception    = new FaultException("Web Server error encountered. All details have been logged.");
                    var messageFault = exception.CreateMessageFault();

                    fault = Message.CreateMessage(version, messageFault, exception.Action);
                }
예제 #19
0
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            var exception = error as GeneralInternalException;

            if (exception == null)
            {
                // log exception
                AppLogger.Error(error);

                var serverInternalFault = new FaultException("Internal Server Error!");

                MessageFault messageFault = serverInternalFault.CreateMessageFault();
                fault = Message.CreateMessage(version, messageFault, null);
            }
            else
            {
                var generalInternalFault = new FaultException <GeneralInternalFault>(new GeneralInternalFault()
                {
                    FaultCode = exception.FaultCode
                });

                MessageFault messageFault = generalInternalFault.CreateMessageFault();
                fault = Message.CreateMessage(version, messageFault, null);
            }
        }
예제 #20
0
        /// <summary>
        /// Enables the creation of a custom <see cref="T:System.ServiceModel.FaultException`1" /> that is returned from an exception in the course of a service method.
        /// </summary>
        /// <param name="error">The <see cref="T:System.Exception" /> object thrown in the course of the service operation.</param>
        /// <param name="version">The SOAP version of the message.</param>
        /// <param name="fault">The <see cref="T:System.ServiceModel.Channels.Message" /> object that is returned to the client, or service, in the duplex case.</param>
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            var message = String.Empty;

            if (error is ReflectionTypeLoadException)
            {
                var ex = error as ReflectionTypeLoadException;
                var sb = new StringBuilder();
                foreach (var exSub in ex.LoaderExceptions)
                {
                    sb.AppendLine(exSub.Message);
                    if (exSub is FileNotFoundException)
                    {
                        var exFileNotFound = exSub as FileNotFoundException;
                        if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
                        {
                            sb.AppendLine("Fusion Log:");
                            sb.AppendLine(exFileNotFound.FusionLog);
                        }
                    }
                    sb.AppendLine();
                }
                var errorMessage = sb.ToString();
                message += errorMessage;
            }
            // Shield the unknown exception
            var faultException = new FaultException(
                "Server error encountered. All details have been logged. " + message);
            var messageFault = faultException.CreateMessageFault();

            fault = Message.CreateMessage(version, messageFault, faultException.Action);
        }
예제 #21
0
        private bool IsExpiredTokenException(FaultException exception)
        {
            var fault = exception.CreateMessageFault();

            if (!fault.HasDetail)
            {
                return(false);
            }

            var root = fault.GetDetail <XElement>();

            if (root == null)
            {
                return(false);
            }

            XNamespace ns = "https://adapi.microsoft.com";

            var errors = root.Element(ns + "Errors");

            if (errors == null)
            {
                return(false);
            }

            return(errors.Elements(ns + "AdApiError").Any(error =>
            {
                var code = error.Element(ns + "Code");

                return code != null && code.Value == "109";
            }));
        }
예제 #22
0
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            var ex = new FaultException <ExceptionDetail>(new ExceptionDetail(error), error.Message);
            var mf = ex.CreateMessageFault();

            fault = Message.CreateMessage(version, mf, ex.Action);
        }
        void IErrorHandler.ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            if (error.GetType().IsGenericType&& error is FaultException)
            {
                Debug.Assert(error.GetType().GetGenericTypeDefinition() == typeof(FaultException <>));
                return;
            }

            try
            {
                Type   detailType = null;
                object detail     = null;

                //Always return details. The channel invoker translates them into an exception hierarchy.
                ExceptionDetail newDetail = new ExceptionDetail(error);
                detailType = newDetail.GetType();
                detail     = newDetail;

                Type           faultUnboundedType = typeof(FaultException <>);
                Type           faultBoundedType   = faultUnboundedType.MakeGenericType(detailType);
                FaultException faultException     = (FaultException)Activator.CreateInstance(faultBoundedType, detail, error.Message);
                MessageFault   messageFault       = faultException.CreateMessageFault();
                fault = Message.CreateMessage(version, messageFault, faultException.Action);
            }
            catch
            {}
        }
예제 #24
0
        public static void PromoteException(Type serviceType, Exception error, MessageVersion version, ref Message fault)
        {
            //Is error in the form of FaultException<T> ?
            if (error.GetType().IsGenericType&& error is FaultException)
            {
                Debug.Assert(error.GetType().GetGenericTypeDefinition() == typeof(FaultException <>));
                return;
            }

            bool inContract = ExceptionInContract(serviceType, error);

            if (inContract == false)
            {
                return;
            }
            try
            {
                Type           faultUnboundedType = typeof(FaultException <>);
                Type           faultBoundedType   = faultUnboundedType.MakeGenericType(error.GetType());
                Exception      newException       = (Exception)Activator.CreateInstance(error.GetType(), error.Message);
                FaultException faultException     = (FaultException)Activator.CreateInstance(faultBoundedType, newException);
                MessageFault   messageFault       = faultException.CreateMessageFault();
                fault = Message.CreateMessage(version, messageFault, faultException.Action);
            }
            catch
            {}
        }
예제 #25
0
        /// <summary>
        /// Process a general FaultException from Medicare, and rethrow a FaultException<![CDATA[<ServiceMessageType>]]> fault.
        /// </summary>
        /// <typeparam name="T">ServiceMessageType type belonging to any of the SMD namespaces.</typeparam>
        /// <param name="ex">Exception to process</param>
        internal static void ProcessAndThrowFault <T>(Exception ex)
        {
            if (!(ex is FaultException))
            {
                throw ex;
            }

            FaultException fex   = (FaultException)ex;
            MessageFault   fault = fex.CreateMessageFault();

            if (fault != null && fault.HasDetail)
            {
                XmlElement errorElement = fault.GetDetail <XmlElement>();

                if (errorElement.LocalName == "serviceMessages")
                {
                    T serviceMessages = errorElement.Deserialize <T>();
                    throw new FaultException <T>(
                              serviceMessages,
                              fex.Reason,
                              fex.Code,
                              fex.Action);
                }
            }

            throw ex;
        }
예제 #26
0
        /// <summary>
        /// ProvideFault
        /// </summary>
        /// <param name="ex">ex</param>
        /// <param name="version">version</param>
        /// <param name="msg">msg</param>
        public void ProvideFault(Exception ex, MessageVersion version, ref Message msg)
        {
            var          newEx    = new FaultException(string.Format("WCF接口出错 {0}", ex.TargetSite.Name));
            MessageFault msgFault = newEx.CreateMessageFault();

            msg = Message.CreateMessage(version, msgFault, newEx.Action);
        }
예제 #27
0
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            //throw new NotImplementedException();

            if (error is FaultException <ServiceError> )
            {
                MessageFault msgfault = ((FaultException <ServiceError>)error).CreateMessageFault();

                fault = Message.CreateMessage(version, msgfault, ((FaultException <ServiceError>)error).Action);
            }
            else
            {
                ServiceError defaultError = new ServiceError()
                {
                    Operation    = "Uncatched ServiceError",
                    ErrorMessage = error == null ? string.Empty : (error.InnerException == null ? error.Message : error.InnerException.Message)
                };
                if (error is UnauthorizedAccessException)
                {
                    defaultError.Operation    = "UnauthorizedAccess Action";
                    defaultError.ErrorMessage = error == null ? string.Empty : error.Message;
                }

                FaultException <ServiceError> defaultExp = new FaultException <ServiceError>(defaultError);
                MessageFault defaultMsgFault             = defaultExp.CreateMessageFault();

                fault = Message.CreateMessage(version, defaultMsgFault, defaultExp.Action);
            }
        }
예제 #28
0
        public Fault(FaultException e) : base(e.Reason.ToString(), e)
        {
            this.Code = "qc:E502"; // 502 : by default a bad gateway

            if (e.Code.SubCode != null)
            {
                Code = formatFaultCode(e.Code.SubCode);
            }

            if (e.Code.SubCode.SubCode != null)
            {
                SubCode = formatFaultCode(e.Code.SubCode.SubCode);
            }

            MessageFault msgFault = e.CreateMessageFault();

            if (msgFault.HasDetail)
            {
                MessageInserts = msgFault.GetReaderAtDetailContents().ReadOuterXml();
                XmlReader reader = msgFault.GetReaderAtDetailContents();
                if (reader.ReadToFollowing("messageInserts"))
                {
                    MessageInserts = reader.ReadOuterXml();
                }
            }
        }
예제 #29
0
        public void ProvideFault(Exception error, MessageVersion version, ref System.ServiceModel.Channels.Message fault)
        {
            FaultException faultException = new FaultException("Server error encountered. All details have been logged.");
            MessageFault   messageFault   = faultException.CreateMessageFault();

            fault = System.ServiceModel.Channels.Message.CreateMessage(version, messageFault, faultException.Action);
        }
예제 #30
0
        public Fault(FaultException e) : base(e.Reason.ToString(), e)
        {
            if (e.Code.SubCode != null)
            {
                Code = e.Code.SubCode.Name;
            }
            else
            {
                Code = "502";
            }

            if (e.Code.SubCode.SubCode != null)
            {
                SubCode = e.Code.SubCode.SubCode.Name;
            }

            MessageFault msgFault = e.CreateMessageFault();

            if (msgFault.HasDetail)
            {
                MessageInserts = msgFault.GetReaderAtDetailContents().ReadOuterXml();
                XmlReader reader = msgFault.GetReaderAtDetailContents();
                if (reader.ReadToFollowing("messageInserts"))
                {
                    MessageInserts = reader.ReadOuterXml();
                }
            }
        }
 public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
 {
     if (!(error is FaultException))
     {
         LoggingExceptionDetails guidException = new LoggingExceptionDetails();
         FaultException<LoggingExceptionDetails> faultException = new FaultException<LoggingExceptionDetails>(guidException, new FaultReason(guidException.Message));
         fault = Message.CreateMessage(version, faultException.CreateMessageFault(), faultException.Action);
         this.LogMessage(error, guidException);
     }
 }