Passing objects to web methods

Discussion in 'ASP .Net Web Services' started by Chris Dunaway, Mar 4, 2004.

  1. Fairly new to web services so bear with me.

    I have a simple class library with the following class:

    Public Class SimpleClass
    Private _AnInteger As Integer
    Private _AString As String

    Public Property AnInteger() As Integer
    Get
    Return _AnInteger
    End Get
    Set(ByVal Value As Integer)
    _AnInteger = Value
    End Set
    End Property

    Public Property AString() As String
    Get
    Return _AString
    End Get
    Set(ByVal Value As String)
    _AString = Value
    End Set
    End Property

    End Class

    This is compiled into a .dll

    I have a web service that references the .dll with web methods declared
    thusly:

    <WebMethod()> _
    Public Function SomeMethod() As SimpleClass
    Dim sc as new SimpleClass

    sc.AnInteger = 1
    sc.AString = "One"
    End Function

    <WebMethod()> _
    Public Sub AnotherMethod(ByVal sc As SimpleClass)
    Dim sc2 as SimpleClass = sc

    sc2.AnInteger = 2
    sc2.AString = "Two"
    End Sub


    Nothing too complex about this so far.

    Finally I have a Windows Forms Application that consumes the web service.

    Dim ws as New localhost.WebService

    this windows app also references the SimpleClass class library .dll


    What I want to be able to do is something like this:

    Dim oMySimpleClassInstance As SimpleClass = ws.SomeMethod()

    ws.AnotherMethod(oMySimpleClassInstance)


    But I can't do this. I have to declare the object as
    localhost.SimpleClass. So if I want to work with a SimpleClass instance
    from the class library, I have to use code like this:

    Dim oMySimpleClassInstance As New SimpleClass
    Dim oWebServiceSimpleClass As localhost.SimpleClass = ws.SomeMethod()

    oMySimpleClassInstance.AnInteger = oWebServiceSimpleClass.AnInteger
    oMySimpleClassInstance.AString = oWebServiceSimpleClass.AString

    And I have to do the opposite when calling the ws.AnotherMethod.


    Bottom line, All I want to do is create an object on the client side and
    pass that object to the web service without having to manually convert it
    back and forth to the proxy class! The classes I intend to use will only
    have basic data types (strings, integers, etc). They might have an array
    variable, but the array will only hold basic datatypes.

    I'm sure there must be something I'm not getting. Do I need to serialize
    the object and just pass a byte array to the web service?

    Any help is appreciated.

    Chris

    P.S. Sorry for the long post

    --
    Chris

    To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
    address.
    Chris Dunaway, Mar 4, 2004
    #1
    1. Advertising

  2. Chris Dunaway

    richlm Guest

    Chris

    I find it useful to think of web services as more of a message based
    protocol than an RPC based protocol.
    You send a request; you get back a response - much like web pages.

    If you think of it that way, it's easier to see that serializing "complex"
    types to binary or XML is an appropriate approach.

    You CAN do this with remoting, and for example define an interface in VB/C#
    used by both client and server.
    The "interface" language for web services is of course WSDL, which has no
    way of expressing a direct relationship to a .NET class.

    Hope this makes it clearer.

    Richard.

    "Chris Dunaway" <dunawayc@_lunchmeat_sbcglobal.net> wrote in message
    news:...
    > Fairly new to web services so bear with me.
    >
    > I have a simple class library with the following class:
    >
    > Public Class SimpleClass
    > Private _AnInteger As Integer
    > Private _AString As String
    >
    > Public Property AnInteger() As Integer
    > Get
    > Return _AnInteger
    > End Get
    > Set(ByVal Value As Integer)
    > _AnInteger = Value
    > End Set
    > End Property
    >
    > Public Property AString() As String
    > Get
    > Return _AString
    > End Get
    > Set(ByVal Value As String)
    > _AString = Value
    > End Set
    > End Property
    >
    > End Class
    >
    > This is compiled into a .dll
    >
    > I have a web service that references the .dll with web methods declared
    > thusly:
    >
    > <WebMethod()> _
    > Public Function SomeMethod() As SimpleClass
    > Dim sc as new SimpleClass
    >
    > sc.AnInteger = 1
    > sc.AString = "One"
    > End Function
    >
    > <WebMethod()> _
    > Public Sub AnotherMethod(ByVal sc As SimpleClass)
    > Dim sc2 as SimpleClass = sc
    >
    > sc2.AnInteger = 2
    > sc2.AString = "Two"
    > End Sub
    >
    >
    > Nothing too complex about this so far.
    >
    > Finally I have a Windows Forms Application that consumes the web service.
    >
    > Dim ws as New localhost.WebService
    >
    > this windows app also references the SimpleClass class library .dll
    >
    >
    > What I want to be able to do is something like this:
    >
    > Dim oMySimpleClassInstance As SimpleClass = ws.SomeMethod()
    >
    > ws.AnotherMethod(oMySimpleClassInstance)
    >
    >
    > But I can't do this. I have to declare the object as
    > localhost.SimpleClass. So if I want to work with a SimpleClass instance
    > from the class library, I have to use code like this:
    >
    > Dim oMySimpleClassInstance As New SimpleClass
    > Dim oWebServiceSimpleClass As localhost.SimpleClass = ws.SomeMethod()
    >
    > oMySimpleClassInstance.AnInteger = oWebServiceSimpleClass.AnInteger
    > oMySimpleClassInstance.AString = oWebServiceSimpleClass.AString
    >
    > And I have to do the opposite when calling the ws.AnotherMethod.
    >
    >
    > Bottom line, All I want to do is create an object on the client side and
    > pass that object to the web service without having to manually convert it
    > back and forth to the proxy class! The classes I intend to use will only
    > have basic data types (strings, integers, etc). They might have an array
    > variable, but the array will only hold basic datatypes.
    >
    > I'm sure there must be something I'm not getting. Do I need to serialize
    > the object and just pass a byte array to the web service?
    >
    > Any help is appreciated.
    >
    > Chris
    >
    > P.S. Sorry for the long post
    >
    > --
    > Chris
    >
    > To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
    > address.
    richlm, Mar 4, 2004
    #2
    1. Advertising

  3. On Thu, 4 Mar 2004 21:38:38 +0100, richlm wrote:

    > I find it useful to think of web services as more of a message based
    > protocol than an RPC based protocol.
    > You send a request; you get back a response - much like web pages.


    Thanks for the response. I resolved my problem by editing the proxy class
    to use my own custom objects. That seemed to work fine. The only glitch
    is that if I update the web service through the IDE, I will have to re-edit
    the proxy class. But since it is so little code to change, it is
    convenient for me.

    Thanks again.

    --
    Chris

    To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
    address.
    Chris Dunaway, Mar 4, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Payne

    Passing Objects between Web User Controls

    Richard Payne, Jun 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    4,668
    Steven Cheng[MSFT]
    Jun 10, 2004
  2. 7stud
    Replies:
    11
    Views:
    673
    Dennis Lee Bieber
    Mar 20, 2007
  3. ghoetker
    Replies:
    1
    Views:
    366
  4. Kenneth McDonald
    Replies:
    5
    Views:
    300
    Kenneth McDonald
    Sep 26, 2008
  5. Replies:
    8
    Views:
    292
    Jorgen Grahn
    May 16, 2013
Loading...

Share This Page