无法将对象发送到 SOAP Web 服务 [英] Can't send object to SOAP Web Service

查看:30
本文介绍了无法将对象发送到 SOAP Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a soap web service with method:

string startReaction(object reaction);

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

Inside that method I convert this object to it's real type:

Reaction reactionObj = (Reaction)reaction;
...

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

I have the same Reaction class in the form project (windows for that should invoke this ws). Here I make Reaction object instance and fill it with data and try to send to web service.

string data = webserviceReference1.startReaction(reaction);

我也试过:

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

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

but nothing. Then I try to add this attribute on Reaction class:

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

但没什么.我得到的错误是:

but nothing. The error that I get is:

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 类,以便客户知道它:

You must expose the Reaction class in the metadata of the sevrice so that the clients know about it:

[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 中的所有已知类型,这样当您在客户端上生成强类型代理时,所有这些类型都将被导入,您将能够调用该服务.

You should not redefine the same class on the client because that won't work. The server wouldn't know how to serialize it. The correct way is to have the web service expose all known types in the WSDL so that when you generate a strongly typed proxy on the client all those types will be imported and you will be able to invoke the service.

这篇关于无法将对象发送到 SOAP Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆