Exemplo n.º 1
0
        public Message AddDoc(Message msgRequest)
        {
            string key = "";

            try
            {
                // Pull the document from request Message
                XmlReader body = msgRequest.GetReaderAtBodyContents();

                // Add document to store
                key = store.AddRecipe(body);
                msgRequest.Close();

                // Create the response message
                Message msgResponse = Message.CreateMessage(MessageVersion.Default, "http://Microsoft.Samples.RecipeCatalog/IDocumentService/AddDocResponse",
                                                            key);

                return(msgResponse);
            }
            catch (Exception ex)
            {
                UnableToAddDocumentFaultException fault = new UnableToAddDocumentFaultException(key, ex.Message);
                throw new FaultException <UnableToAddDocumentFaultException>(fault,
                                                                             new FaultReason("Error adding document"));
            }
        }
Exemplo n.º 2
0
        static public void ProcessMessageFault(XmlReader body)
        {
            FaultCode   faultCode       = null;
            FaultReason faultReason     = null;
            string      faultTypePrefix = "";
            string      faultTypeName   = "";
            string      sn = "http://www.w3.org/2003/05/soap-envelope";

            // We should be on the <Fault> element; get fault <Code> info
            if (body.ReadToFollowing("Code", sn) && body.ReadToDescendant("Value", sn))
            {
                faultCode = new FaultCode(body.ReadElementContentAsString());

                // Get fault <Reason> info
                if (body.ReadToFollowing("Reason", sn) && body.ReadToDescendant("Text", sn))
                {
                    faultReason = new FaultReason(body.ReadElementContentAsString());

                    // Get fault <Detail> info
                    if (body.ReadToFollowing("Detail", sn) && body.ReadToDescendant("anyType"))
                    {
                        // Grab the fault type info from the xsi:type attribute
                        body.MoveToAttribute("type", "http://www.w3.org/2001/XMLSchema-instance");
                        string[] faultType = body.Value.Split(new Char[] { ':' });
                        if (faultType.Length == 1)
                        {
                            faultTypeName = faultType[0];
                        }
                        else
                        {
                            faultTypePrefix = faultType[0];
                            faultTypeName   = faultType[1];
                        }

                        // Move back up to the <anyType> element & process the fault accordingly
                        body.MoveToElement();

                        switch (faultTypeName)
                        {
                        // Deal with our known faults
                        case "DocumentNotFoundFault":
                        {
                            // Rethrow the DocNotFoundFault fault
                            DocumentNotFoundFaultException fault = new DocumentNotFoundFaultException();
                            GetDocumentFaultDetail(body, faultTypePrefix, fault);
                            throw new FaultException <DocumentNotFoundFaultException>(fault,
                                                                                      faultReason,
                                                                                      faultCode);
                        }

                        case "UnableToAddDocumentFault":
                        {
                            UnableToAddDocumentFaultException fault = new UnableToAddDocumentFaultException();
                            GetDocumentFaultDetail(body, faultTypePrefix, fault);
                            throw new FaultException <UnableToAddDocumentFaultException>(fault,
                                                                                         faultReason,
                                                                                         faultCode);
                        }

                        // Deal with string faults (Indigo throws FaultException<string>)
                        case "string":
                        {
                            // Rethrow the string fault
                            throw new FaultException <string>(body.ReadElementContentAsString(),
                                                              faultReason,
                                                              faultCode);
                        }

                        // Deal with all other faults
                        default:
                        {
                            throw new FaultException("Unknown fault");
                        }
                        }
                    }
                }
            }
            // Bad fault message
            throw new Exception("Unable to read Fault message");
        }