简体   繁体   中英

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. I have create the web reference in my project using the local WSDL file and created a class using 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. The web service documentation tells me that myObj is of type ObjectRQ (created from the 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. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

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. 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. One way is the XmlInclude attribute. 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. You can find a blog post about it here .

HTH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM