public Message GetDoc(Message msgRequest) { Message msgResponse = null; string key = null; // Pull the document key from request Message try { XmlReader body = msgRequest.GetReaderAtBodyContents(); body.Read(); key = body.ReadString().Trim(); msgRequest.Close(); } catch (Exception ex) { DocumentNotFoundFaultException fault = new DocumentNotFoundFaultException(key, ex.Message); throw new FaultException <DocumentNotFoundFaultException>(fault, new FaultReason("Document contains invalid XML")); } // Pull the document from the store using the document key IRecipe recipe = store[key]; if (recipe != null) { // Create the response message try { XmlNodeReader reader = new XmlNodeReader(recipe.RecipeXml); msgResponse = Message.CreateMessage(MessageVersion.Default, "http://Microsoft.Samples.RecipeCatalog/IDocumentService/GetDocResponse", reader); } catch (Exception ex) { DocumentNotFoundFaultException fault = new DocumentNotFoundFaultException(key, ex.Message); throw new FaultException <DocumentNotFoundFaultException>(fault, new FaultReason("Error retrieving the document")); } } else { DocumentNotFoundFaultException fault = new DocumentNotFoundFaultException(key, "Document does not exist in store"); throw new FaultException <DocumentNotFoundFaultException>(fault, new FaultReason("Document does not exist in store")); } return(msgResponse); }
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"); }