2

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;
...

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);

I have also tried:

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.
1
  • Why not string startReaction(Reaction reaction); ? Commented Mar 20, 2012 at 11:44

1 Answer 1

4

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)
{
    ...
}

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.