简体   繁体   English

调用Web服务时出现问题-使用XmlInclude或SoapInclude属性

[英]Issue calling a web service - Use the XmlInclude or SoapInclude attribute

I'm calling someone else's web service, they have provided a WSDL file and a bunch of XSD files. 我正在呼叫其他人的Web服务,他们提供了WSDL文件和一堆XSD文件。 I have create the web reference in my project using the local WSDL file and created a class using xsd.exe. 我已经使用本地WSDL文件在项目中创建了Web参考,并使用xsd.exe创建了一个类。 The web method I'm calling is 我正在调用的网络方法是

object MyService.MyMethod(object myObj)

So I create a new instance of my service and a new instance of my object created by the xsd. 因此,我创建了服务的新实例以及xsd创建的对象的新实例。 The web service documentation tells me that myObj is of type ObjectRQ (created from the xsd). Web服务文档告诉我myObj的类型为ObjectRQ(从xsd创建)。

My code is like this: 我的代码是这样的:

MyService service = new MyService();

ObjectRQ request = new ObjectRQ();

// Set the values of request.

object result = service.MyMethod(request);

On the last line of that code I get an error: 在该代码的最后一行,我得到一个错误:

The type ObjectRQ was not expected. 不需要ObjectRQ类型。 Use the XmlInclude or SoapInclude attribute to specify types that are not known statically. 使用XmlInclude或SoapInclude属性可以指定静态未知的类型。

I don't know what could be causing this error and my search hasn't yielded anything helpful. 我不知道是什么原因导致此错误,并且我的搜索没有任何帮助。 Can anyone help me with this? 谁能帮我这个?

Because the parameter type in your proxy is object, the XmlSerializer that composes your messages, doesn't know about the ObjectRQ type. 因为代理中的参数类型是对象,所以组成您的消息的XmlSerializer并不知道ObjectRQ类型。 In that sense it was unexpected. 从这个意义上讲,这是出乎意料的。 So basically what you have to do is let the XmlSerializer know, one way or the other, to expect this type. 因此,基本上您要做的就是让XmlSerializer知道一种或另一种期望这种类型的信息。 One way is the XmlInclude attribute. 一种方法是XmlInclude属性。 Another way is to add the type to the proxy class operations known types. 另一种方法是将类型添加到代理类操作的已知类型。 In the data contract you would do this with the KnownType attribute, but as you only have control over the client, you'd have to do it in code, yourself. 在数据协定中,您可以使用KnownType属性来执行此操作,但是由于您只能控制客户端,因此必须自己在代码中执行。 You can find a blog post about it here . 您可以在此处找到有关它的博客文章。

HTH. HTH。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASMX 服务错误 - 使用 XmlInclude 或 SoapInclude 属性指定静态未知的类型 - ASMX Service Error - Use the XmlInclude or SoapInclude attribute to specify types that are not known statically 使用 XmlInclude 或 SoapInclude 属性指定静态未知的类型 - Use the XmlInclude or SoapInclude attribute to specify types that are not known statically “不希望使用RoleProxy类型。 使用XmlInclude或SoapInclude属性可以指定静态未知的类型。” NHibernate - “The type RoleProxy was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.” NHibernate 类型xxxx不期望使用xmlinclude或soapinclude - type xxxx not expected use xmlinclude or soapinclude xxx类型不是预期的。 使用XmlInclude或SoapInclude属性来指定静态未知的类型 - The type xxx was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically Xml序列化异常:不需要类型UserQuery + SpecificContentItem。 使用XmlInclude或SoapInclude - Xml serialization Exception : The type UserQuery+SpecificContentItem was not expected. Use the XmlInclude or SoapInclude WCF服务引发“未预期类型[…]。 即使已经使用XmlInclude,也要使用XmlInclude […]” - WCF service throws “The type […] was not expected. Use the XmlInclude […]” even if XmlInclude is already being used XmlInclude属性和派生类 - XmlInclude attribute and derived classes 如何动态添加XmlInclude属性 - How to add XmlInclude attribute dynamically 如何使用XmlInclude序列化IEnumerable - How to use XmlInclude to serialize IEnumerable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM