views:

449

answers:

2

Hi guys, I'm building a WCF web service which returns a composite object that looks similar to the following:

    [DataContract]
    public class WebServiceReturn
    {
        ...

        [DataMember]
        public XmlElement Results { get; set; }

        ...
    }

When I return a WebServiceReturn object with the following code, everything is fine:

    XElement cities = new XElement("Cities",
                          from r in results
                          select new XElement("City", r));            

    using (XmlReader xmlReader = cities.CreateReader())
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xmlReader);
        WebServiceReturn response = new WebServiceReturn();
        response.Results = xmlDoc.DocumentElement;
    }

However, when I use the code below, which takes an XmlElement from the results of a stored procedure call that returns an XmlDataDocument, a CommunicationException is thrown (which has no inner exceptions).

XmlDataDocument xdd = DataAccess.ExecuteXML("MyStoredProc", parameter);
response.Results = xdd.DocumentElement;

The confusing part is if I convert the XmlDataDocument.DocumentElement (which is an XmlElement) into an XElement and then back into an XmlElement, there are no problems (wow that was a mouthful) - so the following code returns with no problem.

        XmlElement xe = DataAccess.ExecuteXML("MyStoredProc", parameter).DocumentElement;
        XDocument xDoc = new XDocument();
        using (XmlWriter xmlWriter = xDoc.CreateWriter()){
            xe.WriteTo(xmlWriter);
        }

        using (XmlReader xmlReader = xDoc.Root.CreateReader())
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlReader);
            response.Results = xmlDoc.DocumentElement;
        }


The communicationexception details are:

[CommunicationException: The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.]

I have also updated the service reference in my test application multiple times which has had no effect.

Is the problem with my test code that is calling the web service? Why would converting an XmlElement into an XElement and then back into an XmlElement fix the issue? Any information at all would be much appreciated! :)

+1  A: 

Well, in order to get more error information, you'll need to enable the debugging details in your server's fault - the message you're getting right now is the generic, reveal-nothing-to-possible-attackers WCF error message, basically saying: something went wrong.

In order to do that, you need to tweak your service config - add this section (if you don't already have one):

<behaviors>
  <serviceBehaviors>
    <behavior name="MEXandDebug">
      <serviceMetadata />
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

and then reference that section from your service definition:

<services>
  <service behaviorConfiguration="MEXandDebug" name="WCFService.MyWCFService">

That should give you a more meaningful error, which hopefully gives you an idea what goes wrong.

Otherwise you'll need to debug into your server-side code and find out what's happening there.

marc_s
This is handy to know, but unfortunately enabling it did not give any additional information in the CommunicationException that gets thrown.
Blakomen
+1  A: 

I don't know anything odd about XmlDataDocument, but you don't necessarily need the XDocument - try:

XmlDocument newDoc = new XmlDocument();
newDoc.Load(new XmlNodeReader(doc.DocumentElement));
return newDoc.DocumentElement;

Still not ideal, but it looks cleaner to me...

Marc Gravell
Thanks! This also works - would still be nice to know what the exact issue working with the XmlDataDocument's document element and WCF is though :)
Blakomen