【问题标题】:Can't send object to SOAP Web Service无法将对象发送到 SOAP Web 服务
【发布时间】:2012-04-04 17:17:25
【问题描述】:

我有一个带有方法的肥皂网络服务:

string startReaction(object reaction);

在该方法中,我将此对象转换为它的真实类型:

Reaction reactionObj = (Reaction)reaction;
...

我在表单项目中有相同的Reaction 类(该窗口应该调用这个ws)。在这里,我创建了 Reaction 对象实例并用数据填充它并尝试发送到 Web 服务。

string data = webserviceReference1.startReaction(reaction);

我也试过了:

string data = webserviceReference1.startReaction(reaction as object);

但什么都没有。 然后我尝试在 Reaction 类上添加这个属性:

[XmlInclude(typeof(object))]
public class Reaction{...

但什么都没有。 我得到的错误是:

There was an error generating the XML document. :: System.InvalidOperationException: The type Demo.Form1+Reaction was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

【问题讨论】:

  • 为什么不string startReaction(Reaction reaction); ?

标签: c# web-services soap xml-serialization


【解决方案1】:

您必须在服务的元数据中公开Reaction 类,以便客户端知道它:

[WebMethod]
[XmlInclude(typeof(Reaction))]
[XmlInclude(typeof(Foo))]
[XmlInclude(typeof(Bar))]
// ... the list goes on with all possible types that you might want to pass to this method
// since you used object as argument you need to explicitly say which types are allowed here
public string startReaction(object reaction)
{
    ...
}

您不应该在客户端上重新定义相同的类,因为那样行不通。服务器不知道如何序列化它。正确的方法是让 Web 服务在 WSDL 中公开所有已知类型,这样当您在客户端生成强类型代理时,所有这些类型都将被导入,您将能够调用该服务。

【讨论】:

    猜你喜欢
    • 2014-02-10
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多