Exemplo n.º 1
0
        private void WriteTo12Driver(XmlDictionaryWriter writer, EnvelopeVersion version)
        {
            writer.WriteStartElement(XD.MessageDictionary.Fault, version.DictionaryNamespace);
            writer.WriteStartElement(XD.Message12Dictionary.FaultCode, version.DictionaryNamespace);
            WriteFaultCode12Driver(writer, Code, version);
            writer.WriteEndElement();
            writer.WriteStartElement(XD.Message12Dictionary.FaultReason, version.DictionaryNamespace);
            FaultReason reason = Reason;

            for (int i = 0; i < reason.Translations.Count; i++)
            {
                FaultReasonText text = reason.Translations[i];
                writer.WriteStartElement(XD.Message12Dictionary.FaultText, version.DictionaryNamespace);
                writer.WriteAttributeString("xml", "lang", XmlUtil.XmlNs, text.XmlLang);
                writer.WriteString(text.Text);
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            if (Node.Length > 0)
            {
                writer.WriteElementString(XD.Message12Dictionary.FaultNode, version.DictionaryNamespace, Node);
            }

            if (Actor.Length > 0)
            {
                writer.WriteElementString(XD.Message12Dictionary.FaultRole, version.DictionaryNamespace, Actor);
            }

            if (HasDetail)
            {
                OnWriteDetail(writer, version);
            }
            writer.WriteEndElement();
        }
        private void WriteTo12Driver(XmlDictionaryWriter writer, EnvelopeVersion version)
        {
            writer.WriteStartElement(XD.MessageDictionary.Fault, version.DictionaryNamespace);
            writer.WriteStartElement(XD.Message12Dictionary.FaultCode, version.DictionaryNamespace);
            this.WriteFaultCode12Driver(writer, this.Code, version);
            writer.WriteEndElement();
            writer.WriteStartElement(XD.Message12Dictionary.FaultReason, version.DictionaryNamespace);
            FaultReason reason = this.Reason;

            for (int i = 0; i < reason.Translations.Count; i++)
            {
                FaultReasonText text = reason.Translations[i];
                writer.WriteStartElement(XD.Message12Dictionary.FaultText, version.DictionaryNamespace);
                writer.WriteAttributeString("xml", "lang", "http://www.w3.org/XML/1998/namespace", text.XmlLang);
                writer.WriteString(text.Text);
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            if (this.Node.Length > 0)
            {
                writer.WriteElementString(XD.Message12Dictionary.FaultNode, version.DictionaryNamespace, this.Node);
            }
            if (this.Actor.Length > 0)
            {
                writer.WriteElementString(XD.Message12Dictionary.FaultRole, version.DictionaryNamespace, this.Actor);
            }
            if (this.HasDetail)
            {
                this.OnWriteDetail(writer, version);
            }
            writer.WriteEndElement();
        }
Exemplo n.º 3
0
        public void NoCurrentCulture()
        {
            string current = CultureInfo.CurrentCulture.Name;

            FaultReason r = new FaultReason(new FaultReasonText []
            {
                new FaultReasonText("hola", "es-ES"),
                new FaultReasonText("bonjour", "fr")
            });

            // CurrentCulture
            FaultReasonText t = r.GetMatchingTranslation(
                CultureInfo.CurrentCulture);

            Assert.IsNotNull(t);
            // seems like the first item is used.
            Assert.AreEqual("hola", t.Text, "#1");
            Assert.AreEqual("es-ES", t.XmlLang, "#2");

            // InvariantCulture
            t = r.GetMatchingTranslation(
                CultureInfo.InvariantCulture);
            Assert.IsNotNull(t);
            // seems like the first item is used.
            Assert.AreEqual("hola", t.Text, "#3");
            Assert.AreEqual("es-ES", t.XmlLang, "#4");
        }
        /// <summary>Called after an inbound message has been received but before the message is dispatched to the intended operation.</summary>
        /// <param name="request">The request message.</param>
        /// <param name="channel">The incoming channel.</param>
        /// <param name="instanceContext">The current service instance.</param>
        /// <returns>The object used to correlate state. This object is passed back in the BeforeSendReply(Message@,object) method.</returns>
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            if (request == null)
            {
                return(null);
            }

            /* Create a buffer in order to make it possible to work with copies of the message */
            MessageBuffer buffer = request.CreateBufferedCopy(int.MaxValue);

            this.OnPreValidation(buffer.CreateMessage(), channel, instanceContext);

            /* Create a copy of the message and send it to the validation */
            request = buffer.CreateMessage();
            try {
                this.ValidateMessage(request);
            }
            catch (XmlSchemaValidationException ex) {
                this.OnValidationError(buffer.CreateMessage(), channel, instanceContext, ex);

                FaultReasonText reasonText = new FaultReasonText(Resources.MessageDoesNotComplyWithSchema, CultureInfo.InvariantCulture);
                throw new FaultException <string>(ex.Message, new FaultReason(reasonText), FaultCode.CreateSenderFaultCode(new FaultCode("InvalidMessage")));
            }

            this.OnValidationSuccess(buffer.CreateMessage(), channel, instanceContext);

            /* Validation was succesfull. Create a new copy of the message and pass it to the WCF process. */
            request = buffer.CreateMessage();

            /* There is no need to correlate the AfterReceiveRequest en BeforeSendReply calls, so simply return null */
            return(null);
        }
 static ProtocolException CreateReceiveShutdownReturnedNonNull(Message message)
 {
     if (message.IsFault)
     {
         try
         {
             MessageFault    fault  = MessageFault.CreateFault(message, 64 * 1024);
             FaultReasonText reason = fault.Reason.GetMatchingTranslation(CultureInfo.CurrentCulture);
             string          text   = String.Format(ExceptionMessages.ReceiveShutdownReturnedFault, reason.Text);
             return(new ProtocolException(text));
         }
         catch (QuotaExceededException)
         {
             string text = String.Format(ExceptionMessages.ReceiveShutdownReturnedLargeFault,
                                         message.Headers.Action);
             return(new ProtocolException(text));
         }
     }
     else
     {
         string text = String.Format(ExceptionMessages.ReceiveShutdownReturnedMessage,
                                     message.Headers.Action);
         return(new ProtocolException(text));
     }
 }
Exemplo n.º 6
0
        public static ReceivedFault CreateFault11(XmlDictionaryReader reader, int maxBufferSize)
        {
            string str;
            string str2;

            reader.ReadStartElement(XD.MessageDictionary.Fault, XD.Message11Dictionary.Namespace);
            reader.ReadStartElement(XD.Message11Dictionary.FaultCode, XD.Message11Dictionary.FaultNamespace);
            XmlUtil.ReadContentAsQName(reader, out str2, out str);
            FaultCode code = new FaultCode(str2, str);

            reader.ReadEndElement();
            string xmlLang = reader.XmlLang;

            reader.MoveToContent();
            FaultReasonText translation = new FaultReasonText(reader.ReadElementContentAsString(XD.Message11Dictionary.FaultString.Value, XD.Message11Dictionary.FaultNamespace.Value), xmlLang);
            string          actor       = "";

            if (reader.IsStartElement(XD.Message11Dictionary.FaultActor, XD.Message11Dictionary.FaultNamespace))
            {
                actor = reader.ReadElementContentAsString();
            }
            XmlBuffer detail = null;

            if (reader.IsStartElement(XD.Message11Dictionary.FaultDetail, XD.Message11Dictionary.FaultNamespace))
            {
                detail = new XmlBuffer(maxBufferSize);
                detail.OpenSection(reader.Quotas).WriteNode(reader, false);
                detail.CloseSection();
                detail.Close();
            }
            reader.ReadEndElement();
            return(new ReceivedFault(code, new FaultReason(translation), actor, actor, detail, EnvelopeVersion.Soap11));
        }
        private void WriteTo11(XmlDictionaryWriter writer)
        {
            string name;
            string str2;

            writer.WriteStartElement(XD.MessageDictionary.Fault, XD.Message11Dictionary.Namespace);
            writer.WriteStartElement(XD.Message11Dictionary.FaultCode, XD.Message11Dictionary.FaultNamespace);
            FaultCode subCode = this.Code;

            if (subCode.SubCode != null)
            {
                subCode = subCode.SubCode;
            }
            if (subCode.IsSenderFault)
            {
                name = "Client";
            }
            else if (subCode.IsReceiverFault)
            {
                name = "Server";
            }
            else
            {
                name = subCode.Name;
            }
            if (subCode.IsPredefinedFault)
            {
                str2 = "http://schemas.xmlsoap.org/soap/envelope/";
            }
            else
            {
                str2 = subCode.Namespace;
            }
            if (writer.LookupPrefix(str2) == null)
            {
                writer.WriteAttributeString("xmlns", "a", "http://www.w3.org/2000/xmlns/", str2);
            }
            writer.WriteQualifiedName(name, str2);
            writer.WriteEndElement();
            FaultReasonText text = this.Reason.Translations[0];

            writer.WriteStartElement(XD.Message11Dictionary.FaultString, XD.Message11Dictionary.FaultNamespace);
            if (text.XmlLang.Length > 0)
            {
                writer.WriteAttributeString("xml", "lang", "http://www.w3.org/XML/1998/namespace", text.XmlLang);
            }
            writer.WriteString(text.Text);
            writer.WriteEndElement();
            if (this.Actor.Length > 0)
            {
                writer.WriteElementString(XD.Message11Dictionary.FaultActor, XD.Message11Dictionary.FaultNamespace, this.Actor);
            }
            if (this.HasDetail)
            {
                this.OnWriteDetail(writer, EnvelopeVersion.Soap11);
            }
            writer.WriteEndElement();
        }
Exemplo n.º 8
0
        public void Simple()
        {
            FaultReason     r = new FaultReason("testing");
            FaultReasonText t = r.GetMatchingTranslation();

            Assert.IsNotNull(t);
            Assert.AreEqual("testing", t.Text, "#1");
            Assert.AreEqual(CultureInfo.CurrentCulture.Name,
                            t.XmlLang, "#2");
        }
        /// <summary>
        /// Create an Access Denied fault.
        /// </summary>
        /// <returns></returns>
        private Exception CreateAccessDeniedFault(ErrorCode errorCode)
        {
            //FaultCode code = FaultCode.CreateSenderFaultCode(
            //  "FailedAuthentication",
            //  "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

            FaultReasonText faultText =
                new FaultReasonText("Access Denied.", CultureInfo.CurrentCulture);


            throw new FaultException(ExtensionUtility.GetDescription <ErrorCode>(errorCode));
        }
Exemplo n.º 10
0
        public void CreateFaultMessageAndWrite()
        {
            string xml = @"<s:Envelope xmlns:a=""http://www.w3.org/2005/08/addressing"" xmlns:s=""http://www.w3.org/2003/05/soap-envelope""><s:Header><a:Action s:mustUnderstand=""1"">http://www.w3.org/2005/08/addressing/fault</a:Action></s:Header><s:Body><s:Fault><s:Code><s:Value xmlns:a=""urn:me"">a:me</s:Value></s:Code><s:Reason><s:Text xml:lang="""">am so lazy</s:Text></s:Reason></s:Fault></s:Body></s:Envelope>";

            FaultCode       fc  = new FaultCode("me", "urn:me");
            FaultReasonText ft  = new FaultReasonText("am so lazy", CultureInfo.InvariantCulture);
            Message         msg = Message.CreateMessage(
                MessageVersion.Default,
                MessageFault.CreateFault(fc, new FaultReason(ft)),
                "http://www.w3.org/2005/08/addressing/fault");

            // It should be XML comparison, not string comparison
            Assert.AreEqual(xml, GetSerializedMessage(msg));
        }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            FaultCode       code        = FaultCode.CreateSenderFaultCode("calcuError", "http://www.artech.com");
            FaultReasonText reasonText1 = new FaultReasonText("Divided by zero!", "en-US");
            FaultReasonText reasonText2 = new FaultReasonText("试图除以零!", "zh-CN");
            FaultReason     reason      = new FaultReason(new FaultReasonText[] { reasonText1, reasonText2 });
            MessageFault    fault       = MessageFault.CreateFault(code, reason);
            string          action      = "http://www.artech.com/divideFault";

            using (Message message = Message.CreateMessage(MessageVersion.Default, fault, action))
            {
                WriteMessage(message, "message.xml");
            }
        }
Exemplo n.º 12
0
        public void SetState(int i)
        {
            if (i == -1)
            {
                FaultReasonText[] text = new FaultReasonText[2];
                text[0] = new FaultReasonText("Sample Error", new CultureInfo("en"));
                text[1] = new FaultReasonText("Beispiel Fehler", new CultureInfo("de"));
                FaultReason reason = new FaultReason(text);

                throw new FaultException <StateFault>(
                          new StateFault()
                {
                    BadState = i
                }, reason);
            }
            else
            {
                this.i = i;
            }
        }
Exemplo n.º 13
0
        public static ReceivedFault CreateFault11(XmlDictionaryReader reader, int maxBufferSize)
        {
            reader.ReadStartElement(XD.MessageDictionary.Fault, XD.Message11Dictionary.Namespace);
            string ns;
            string name;

            reader.ReadStartElement(XD.Message11Dictionary.FaultCode, XD.Message11Dictionary.FaultNamespace);
            XmlUtil.ReadContentAsQName(reader, out name, out ns);
            FaultCode code = new FaultCode(name, ns);

            reader.ReadEndElement();

            string xmlLang = reader.XmlLang;

            reader.MoveToContent();  // Don't do IsStartElement.  FaultString is required, so let the reader throw.
            string          text        = reader.ReadElementContentAsString(XD.Message11Dictionary.FaultString.Value, XD.Message11Dictionary.FaultNamespace.Value);
            FaultReasonText translation = new FaultReasonText(text, xmlLang);

            string actor = "";

            if (reader.IsStartElement(XD.Message11Dictionary.FaultActor, XD.Message11Dictionary.FaultNamespace))
            {
                actor = reader.ReadElementContentAsString();
            }

            XmlBuffer detail = null;

            if (reader.IsStartElement(XD.Message11Dictionary.FaultDetail, XD.Message11Dictionary.FaultNamespace))
            {
                detail = new XmlBuffer(maxBufferSize);
                XmlDictionaryWriter writer = detail.OpenSection(reader.Quotas);
                writer.WriteNode(reader, false);
                detail.CloseSection();
                detail.Close();
            }
            reader.ReadEndElement();
            FaultReason reason = new FaultReason(translation);

            return(new ReceivedFault(code, reason, actor, actor, detail, EnvelopeVersion.Soap11));
        }
Exemplo n.º 14
0
        public void MultipleLanguages()
        {
            string current = CultureInfo.CurrentCulture.Name;

            FaultReason r = new FaultReason(new FaultReasonText []
            {
                new FaultReasonText("hello"),
                new FaultReasonText("hola", "es-ES"),
                new FaultReasonText("bonjour", "fr")
            });

            // CurrentCulture
            FaultReasonText t = r.GetMatchingTranslation(
                CultureInfo.CurrentCulture);

            Assert.IsNotNull(t);
            Assert.AreEqual("hello", t.Text, "#1");
            Assert.AreEqual(current, t.XmlLang, "#2");

            // non-neutral name, get by non-neutral culture
            t = r.GetMatchingTranslation(
                new CultureInfo("es-ES"));
            Assert.IsNotNull(t);
            Assert.AreEqual("hola", t.Text, "#3");
            Assert.AreEqual("es-ES", t.XmlLang, "#4");

            // .ctor(non-neutral name), get by neutral culture
            t = r.GetMatchingTranslation(new CultureInfo("es"));
            Assert.IsNotNull(t);
            Assert.AreEqual("hello", t.Text, "#5");
            Assert.AreEqual(current, t.XmlLang, "#6");

            // .ctor(neutral name), get by non-neutral culture
            t = r.GetMatchingTranslation(
                new CultureInfo("fr-FR"));
            Assert.IsNotNull(t);
            Assert.AreEqual("bonjour", t.Text, "#7");
            Assert.AreEqual("fr", t.XmlLang, "#8");
        }
Exemplo n.º 15
0
        private void WriteTo11(XmlDictionaryWriter writer)
        {
            writer.WriteStartElement(XD.MessageDictionary.Fault, XD.Message11Dictionary.Namespace);
            writer.WriteStartElement(XD.Message11Dictionary.FaultCode, XD.Message11Dictionary.FaultNamespace);

            FaultCode faultCode = Code;

            if (faultCode.SubCode != null)
            {
                faultCode = faultCode.SubCode;
            }

            string name;

            if (faultCode.IsSenderFault)
            {
                name = "Client";
            }
            else if (faultCode.IsReceiverFault)
            {
                name = "Server";
            }
            else
            {
                name = faultCode.Name;
            }

            string ns;

            if (faultCode.IsPredefinedFault)
            {
                ns = Message11Strings.Namespace;
            }
            else
            {
                ns = faultCode.Namespace;
            }

            string prefix = writer.LookupPrefix(ns);

            if (prefix == null)
            {
                writer.WriteAttributeString("xmlns", "a", XmlUtil.XmlNsNs, ns);
            }

            writer.WriteQualifiedName(name, ns);
            writer.WriteEndElement();
            FaultReasonText translation = Reason.Translations[0];

            writer.WriteStartElement(XD.Message11Dictionary.FaultString, XD.Message11Dictionary.FaultNamespace);
            if (translation.XmlLang.Length > 0)
            {
                writer.WriteAttributeString("xml", "lang", XmlUtil.XmlNs, translation.XmlLang);
            }

            writer.WriteString(translation.Text);
            writer.WriteEndElement();
            if (Actor.Length > 0)
            {
                writer.WriteElementString(XD.Message11Dictionary.FaultActor, XD.Message11Dictionary.FaultNamespace, Actor);
            }

            if (HasDetail)
            {
                OnWriteDetail(writer, EnvelopeVersion.Soap11);
            }
            writer.WriteEndElement();
        }