Passing a derived class to a WebMethod

Ask a Question related to ASP.NET Web Services, Design and Development.

  1. #1

    Default Passing a derived class to a WebMethod

    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


    Michael Carr Guest

  2. Similar Questions and Discussions

    1. Derived class not exposed in XML
      Say I have a class inherited from ArrayList called MyCollection. I have a WebMethod that returns a MyCollection. But when I reference the service,...
    2. [WebMethod] passing, receving XmlNode Without a Namespace
      Hi all, For some reasons I sometimes use the following interface for xml passing: public XmlNode getSomeOutputXml(XmlNode someInputXml) { }...
    3. WS Serialization of ArrayList derived Class
      Hi Steve, You may refer to this article to see if it will help: Controlling XML Serialization Using Attributes...
    4. exposing derived classes to proxy without fake webmethod?
      Hi, try using on the appropriate WebMethod. This attribute instructs the ASMX engine to include the type description for type bla. Cheers, --...
    5. Access DataGrid declared in derived class from base class??
      Hi! I have a base class called ListBase.vb, from which I derive EmplList.ascx.vb. In EmplList.ascx.vb I declared a...
  3. #2

    Default Re: Passing a derived class to a WebMethod

    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" <mcarr@umich.edu> wrote in message
    news:e3Xd3DrREHA.1644@TK2MSFTNGP09.phx.gbl...
    > 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
    >
    >

    Dino Chiesa [Microsoft] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139