我有一个 SOAP 网络服务,方法如下:
string startReaction(object reaction);
在该方法中,我将此对象转换为其真实类型:
Reaction reactionObj = (Reaction)reaction;
...
我在表单项目中有相同的 Reaction
类(该类的窗口应该调用此 ws)。这里我创建了 Reaction 对象实例并用数据填充它并尝试发送到 Web 服务。
string data = webserviceReference1.startReaction(reaction);
我也尝试过:
string data = webserviceReference1.startReaction(reaction as object);
但什么也没有。 然后我尝试在 react 类上添加此属性:
[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.
最佳答案
您必须在服务的元数据中公开 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 中的所有已知类型,以便当您在客户端上生成强类型代理时,所有这些类型都将被导入,并且您将能够调用该服务。
关于c# - 无法将对象发送到 SOAP Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9783419/