2012-03-20 113 views
2

我有方法SOAP Web服务:无法发送对象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. 
+0

为什么'string startReaction(Reaction reaction); '? – 2012-03-20 11:44:55

回答

4

您必须公开Reaction类的sevrice的元数据,以便客户了解它:

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