开发者

WCF Can't Return XmlElement from XmlDataDocument.DocumentElement

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开发者_C百科 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! :)


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...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜