可以';t将对象发送到SOAP Web服务

本文关键字:SOAP Web 服务 对象 可以 | 更新日期: 2023-09-27 18:28:16

我有一个方法为的soap web服务

string startReaction(object reaction);

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

Reaction reactionObj = (Reaction)reaction;
...

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

string data = webserviceReference1.startReaction(reaction);

我也试过:

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

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

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

可以';t将对象发送到SOAP Web服务

您必须在服务的元数据中公开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中的所有已知类型,这样当您在客户端上生成强类型代理时,所有这些类型都将被导入,并且您将能够调用该服务。