WCF 无法从 XmlDataDocument.DocumentElement 返回 XmlElement

发布于 2024-08-15 04:37:01 字数 1816 浏览 6 评论 0原文

我正在构建一个 WCF Web 服务,它返回一个类似于以下内容的复合对象:

    [DataContract]
    public class WebServiceReturn
    {
        ...

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

        ...
    }

当我使用以下代码返回 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;
    }

但是,当我使用下面的代码时,它从返回 XmlDataDocument 的存储过程调用的结果,将引发 CommunicationException(没有内部异常)。

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

令人困惑的部分是,如果我将 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;
        }   

通信异常详细信息为:

[CommunicationException:服务器未提供有意义的回复;这可能是由合同不匹配、会话过早关闭或内部服务器错误引起的。]

我还多次更新了我的测试应用程序中的服务引用,但没有效果。

是我调用 Web 服务的测试代码有问题吗?为什么将 XmlElement 转换为 XElement,然后再转换回 XmlElement 可以解决此问题?任何信息都将不胜感激! :)

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! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧话新听 2024-08-22 04:37:01

我不知道 XmlDataDocument 有什么奇怪的,但你不一定需要 XDocument - 尝试:

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

仍然不理想,但对我来说看起来更干净......

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

终弃我 2024-08-22 04:37:01

好吧,为了获得更多错误信息,您需要启用服务器故障中的调试详细信息 - 您现在收到的消息基本上是通用的、不透露任何可能的攻击者的 WCF 错误消息说:出事了。

为了做到这一点,您需要调整您的服务配置 - 添加此部分(如果您还没有):

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

然后从您的服务定义中引用该部分:

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

这应该会给您一个更有意义的错误,希望能够给出你知道出了什么问题。

否则,您需要调试服务器端代码并​​找出那里发生了什么。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文