KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
In order to return useful information in SoapException.Detail for an asmx web service, I took an idea from WCF and created a fault class to contain said useful information. That fault object is then serialised to the required XmlNode of a thrown SoapException . I'm wondering whether I have the best code to create the XmlDocument - here is my take on it: var xmlDocument = new XmlDocument(); var serializer = new XmlSerializer(typeof(T)); using (var stream = new MemoryStream()) { serializer.Serialize(stream, theObjectContainingUsefulInformation); stream.Flush(); stream.Seek(0, SeekOrigin.Begin); xmlDocument.Load(stream); } Is there a better way of doing this? UPDATE: I actually ended up doing the following, because unless you wrap the XML in a <detail> xml element, you get a SoapHeaderException at the client end: var serialiseToDocument = new XmlDocument(); var serializer = new XmlSerializer(typeof(T)); using (var stream = new MemoryStream()) { serializer.Serialize(stream, e.ExceptionContext); stream.Flush(); stream.Seek(0, SeekOrigin.Begin); serialiseToDocument.Load(stream); } // Remove the xml declaration serialiseToDocument.RemoveChild(serialiseToDocument.FirstChild); // Memorise the node we want var serialisedNode = serialiseToDocument.FirstChild; // and wrap it in a <detail> element var rootNode = serialiseToDocument.CreateNode(XmlNodeType.Element, "detail", ""); rootNode.AppendChild(serialisedNode); UPDATE 2: Given John Saunders excellent answer, I've now started using the following: private static void SerialiseFaultDetail() { var fault = new ServiceFault { Message = "Exception occurred", ErrorCode = 1010 };
Tags (comma-separated)
Save Edits
Cancel