Passing a custom object via a web method

C

cathywigzell

I have a web service which exposes a method...

[WebMethod]
public Object Invoke(Object obj)
{
...
}

both the input param and the return value objects can, in reality, be
anything. Howver, if I call this method from a client and cast my own
custom object as Object, I get this error...

System.InvalidOperationException: The type MyType was not expected. Use
the XmlInclude or SoapInclude attribute to specify types that are not
known statically.

My custom class is defined like this...

[XmlInclude( typeof( MyType ) )]
public class MyType
{
...
}

Obviously I need to do more, but what?

Cathy
 
N

Nicholas Paldino [.NET/C# MVP]

Cathy,

It's almost impossible to do this with a WebMethod. Ideally, the way
that this would translate into WSDL would be that the parameter and the
return value would allow any valid XML. This would also mean that you have
to process the XML yourself to interpret the contents correctly.

Basically, this kind of design is not recommended. You are better off
creating more strongly typed versions, or rather, expose XML as the inputs
and/or outputs and do some pre-post processing.

Hope this helps.
 
C

cathywigzell

Hi Nicholas,

Many thanks for your reply, but obviously your understanding far
outstrips mine. You say 'expose XML as the inputs and outputs...
pre-post processing'. Could you please be a bit more specific?

BTW, I've just come from a Java servlet/applet/browser environment and
I'm exploring the possibility of moving our app to C# with a ClickOnce
client talking to a webservice.

Cathy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top