Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Services > Passing a derived class to a WebMethod

Reply
Thread Tools

Passing a derived class to a WebMethod

 
 
Michael Carr
Guest
Posts: n/a
 
      05-31-2004
I am writing a client that consumes a web service and extends the
functionality of one of the web service's classes. The definition is
something like this:

/* This class is defined within the webservice and
added to the client as a Web Reference */
class ServerClass
{ ... }

/* This class exists inside the client only and inherits from the
class above to add additional functionality */
class ClientClass : ServerClass
{ ... }

Now, I'd like to call a WebMethod and pass a ClientClass where it would
normally expect a ServerClass parameter, since I've been working with an
instance of ClientClass locally. On the server, this WebMethod is defined
as:

[WebMethod]
void ServerMethod(ServerClass a)
{ ... }

I would like to call it from the client as

ClientClass b; // Derives from ServerClass
ServerMethod(b);

which seems like it should be possible since ClientClass inherits from
ServerClass... However, when I try to execute this WebMethod I get the
message "An unhandled exception of type 'System.InvalidOperationException'
occurred in system.xml.dll. Additional information: There was an error
generating the XML document."

Are there any tricks I need to play in order to pass a derived class into a
WebMethod?

Thank you for any help,
Michael Carr


 
Reply With Quote
 
 
 
 
Dino Chiesa [Microsoft]
Guest
Posts: n/a
 
      06-01-2004
Can you explain what you are trying to accomplish?

Think of webservices as message-based communications infrastructure. A
webservice method should not accept an instance of its parent class as an
input argument. Instead it should accept a request message, like so:

[WebService]
public MyService {
[WebMethod]
public ResponseMessage Method1(RequestMessage request) {
...
}
}

The RequestMessage should be thought of as a data transfer object, not as a
pure class.

The model is: you are not passing instances of objects. You are passing
messages. (==DTO)

In your case you have ClientClass that derives from ServerClass, but there
is no assurance that the two serialize to compatible XML message formats.
The messages are the key, not the class instances.


-Dino


"Michael Carr" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I am writing a client that consumes a web service and extends the
> functionality of one of the web service's classes. The definition is
> something like this:
>
> /* This class is defined within the webservice and
> added to the client as a Web Reference */
> class ServerClass
> { ... }
>
> /* This class exists inside the client only and inherits from the
> class above to add additional functionality */
> class ClientClass : ServerClass
> { ... }
>
> Now, I'd like to call a WebMethod and pass a ClientClass where it would
> normally expect a ServerClass parameter, since I've been working with an
> instance of ClientClass locally. On the server, this WebMethod is defined
> as:
>
> [WebMethod]
> void ServerMethod(ServerClass a)
> { ... }
>
> I would like to call it from the client as
>
> ClientClass b; // Derives from ServerClass
> ServerMethod(b);
>
> which seems like it should be possible since ClientClass inherits from
> ServerClass... However, when I try to execute this WebMethod I get the
> message "An unhandled exception of type 'System.InvalidOperationException'
> occurred in system.xml.dll. Additional information: There was an error
> generating the XML document."
>
> Are there any tricks I need to play in order to pass a derived class into

a
> WebMethod?
>
> Thank you for any help,
> Michael Carr
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Derived Structure in Derived Class?? David C++ 3 01-29-2008 07:38 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Passing reference to derived class object to base class constructor Jeff C++ 4 03-08-2006 03:14 AM
Passing derived class object array in place of base class object array justanotherguy63@yahoo.com C++ 9 12-03-2004 10:57 PM



Advertisments