将服务引用代理与 XmlAnyElementAttribute 一起使用时未声明 XElement

发布于 2024-09-26 10:05:53 字数 2317 浏览 3 评论 0原文

我正在开发一个与第三方 Soap 服务对话的 Windows Phone 7 应用程序。我使用 Add Service Reference 到 wsdl 来生成代理类。某些调用有效,但某些使用 'sObject' 类的出站调用会导致错误 “不是预期的 System.Xml.Linq.XElement 类型。请使用 XmlInclude 或 SoapInclude属性来指定静态未知的类型”

sObject 类的相关部分已定义(自动生成)

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:sobject.partner.soap.sforce.com")]
 public partial class sObject : object, System.ComponentModel.INotifyPropertyChanged {

    <snipped ....>

    [System.Xml.Serialization.XmlAnyElementAttribute(Namespace="urn:sobject.partner.soap.sforce.com", Order=3)]
    public System.Xml.Linq.XElement[] Any {
        get {
            return this.anyField;
        }
        set {
            this.anyField = value;
            this.RaisePropertyChanged("Any");
        }
    }

我用来创建 sObject 的代码:

sObject post = new sObject();
        XElement postEls = new XElement("sObject",
            new XElement("Type", "TextPost"),
            new XElement("ParentId", userInfo.userId),
            new XElement("Body", postBody)
            );

        post.Any = postEls.Elements().ToArray();

我尝试过的步骤:

添加 < code>[System.Xml.Serialization.XmlInclude(typeof(System.Xml.Linq.XElement))] 到 sObject 类。同样的错误,看来这应该有效。

[System.Xml.Serialization.XmlElement()] 添加到 Any 属性。当我这样做时,消息被序列化,但正如预期的那样不正确(任何元素不应该在那里,目标只是 XElements[] 作为输出)

 <sObject>
      <type xmlns="urn:sobject.partner.soap.sforce.com">FeedPost</type>
      <Id xsi:nil="true" xmlns="urn:sobject.partner.soap.sforce.com" />
      <Any xmlns="urn:sobject.partner.soap.sforce.com">
        <Type xmlns="">TextPost</Type>
      </Any>
      <Any xmlns="urn:sobject.partner.soap.sforce.com">
        <ParentId xmlns="">XXXXXX</ParentId>
      </Any>
      <Any xmlns="urn:sobject.partner.soap.sforce.com">
        <Body xmlns="">Test post from WP7!</Body>
      </Any>
    </sObject>

顺便说一句,我怀疑这是相关的,返回 sObjects 入站的调用来自第 3 方服务的 Any 属性为 null,尽管数据存在于 RPC 响应中。

I am working on a Windows Phone 7 app that talks to a 3rd party Soap service. I used Add Service Reference to the wsdl to generate the proxy class. Some calls work, but certain outbound calls that make use of an 'sObject' class result in the error "The type System.Xml.Linq.XElement was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically"

The relevant part of the sObject class is defined (as autogenerated)

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:sobject.partner.soap.sforce.com")]
 public partial class sObject : object, System.ComponentModel.INotifyPropertyChanged {

    <snipped ....>

    [System.Xml.Serialization.XmlAnyElementAttribute(Namespace="urn:sobject.partner.soap.sforce.com", Order=3)]
    public System.Xml.Linq.XElement[] Any {
        get {
            return this.anyField;
        }
        set {
            this.anyField = value;
            this.RaisePropertyChanged("Any");
        }
    }

The code I use to create a sObject:

sObject post = new sObject();
        XElement postEls = new XElement("sObject",
            new XElement("Type", "TextPost"),
            new XElement("ParentId", userInfo.userId),
            new XElement("Body", postBody)
            );

        post.Any = postEls.Elements().ToArray();

Steps I Have Tried:

Adding [System.Xml.Serialization.XmlInclude(typeof(System.Xml.Linq.XElement))] to the sObject class. Same error, it seems like this should have worked.

Adding [System.Xml.Serialization.XmlElement()] to the Any property. When I did this the message was serialized, but -as expected- incorrect (the Any elements should not be there, the goal is just the XElements[] as output)

 <sObject>
      <type xmlns="urn:sobject.partner.soap.sforce.com">FeedPost</type>
      <Id xsi:nil="true" xmlns="urn:sobject.partner.soap.sforce.com" />
      <Any xmlns="urn:sobject.partner.soap.sforce.com">
        <Type xmlns="">TextPost</Type>
      </Any>
      <Any xmlns="urn:sobject.partner.soap.sforce.com">
        <ParentId xmlns="">XXXXXX</ParentId>
      </Any>
      <Any xmlns="urn:sobject.partner.soap.sforce.com">
        <Body xmlns="">Test post from WP7!</Body>
      </Any>
    </sObject>

As an aside, I suspect this is related, calls that return sObjects inbound from the 3rd party service have their Any property null, despite the data being there in the RPC Response.

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

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

发布评论

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

评论(1

厌倦 2024-10-03 10:05:53

事实证明,该异常是由于 XElement[] 的序列化造成的,这目前是 Windows Phone 7 中的一个问题。

正如 Vijay Verma 此处,有关 XmlSerializer.Serialize 的链接 MSDN 文章在平台说明下指出:

Windows Phone 版 Silverlight:
如果使用包含 XElement 类型的对象数组的类型参数初始化 XmlSerializer 对象,则 XmlSerializer.Serialize 方法将引发 InvalidOperationException。

Vijay 列出了使用单个 XElement 并单独解析字段的解决方法。如果您控制 SOAP 消息的双方,这将起作用。不幸的是我不这样做,所以该请求在第三者方面无效。

这解释了 XElement 的 InvalidOperationException 不是预期的,尽管不是我希望的答案。希望它可以帮助其他可能控制服务双方的人。

It turns out the exception was due to the serialization of XElement[], currently an issue in Windows Phone 7.

As noted by Vijay Verma here, The linked MSDN article on XmlSerializer.Serialize states under Platform Notes:

Silverlight for Windows Phone:
The XmlSerializer.Serialize method throws an InvalidOperationException if the XmlSerializer object is initialized with a type parameter that contains an array of objects of type XElement.

Vijay listed a workaround of using a single XElement and parsing the field separately. This would work if you control both sides of the SOAP message. Unfortunately I do not so the request is invalid on the 3rd party's side.

This explains the InvalidOperationException with XElement was not expected, although not the answer I was hoping for. Hopefully it helps someone else who may have control over both sides of the service.

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