简体   繁体   English

WCF无法从XmlDataDocument.DocumentElement返回XmlElement

[英]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: 我正在构建一个WCF Web服务,该服务返回一个类似于以下内容的复合对象:

    [DataContract]
    public class WebServiceReturn
    {
        ...

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

        ...
    }

When I return a WebServiceReturn object with the following code, everything is fine: 当我使用以下代码返回WebServiceReturn对象时,一切都很好:

    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的存储过程调用的结果中获取XmlElement)时,将抛出CommunicationException(没有内部异常)。

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. 令人困惑的部分是,如果我将XmlDataDocument.DocumentElement(它是XmlElement)转换为XElement,然后又转换回XmlElement,则没有问题(哇,这是一个大问题)-因此以下代码返回没有问题。

        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; [CommunicationException:服务器未提供有意义的回复; 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? 我的测试代码调用Web服务是否存在问题? Why would converting an XmlElement into an XElement and then back into an XmlElement fix the issue? 为什么将XmlElement转换为XElement然后再转换为XmlElement可以解决此问题? 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: 我对XmlDataDocument并不XmlDataDocument ,但您不一定需要XDocument尝试:

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. 好吧,为了获取更多错误信息,您需要在服务器的故障中启用调试详细信息-您现在得到的消息是通用的,根本不向攻击者显示任何内容的WCF错误消息,基本上说:出事了。

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. 否则,您将需要调试服务器端代码,并找出那里发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM