Question about web services and sharing classes

Discussion in 'ASP .Net' started by Chris Dunaway, Feb 27, 2004.

  1. How can a class be shared between a web service and a client that consumes
    the web service?

    Suppose I have a Class Libraray with the following simple class:

    Public Class SimpleClass
    Private _AnInteger As Integer

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

    This is compiled to a .DLL.

    Now, I create a Web Service project and Reference the ClassLibrary and then
    add a Web Method as follows:

    <WebMethod()> _
    Public Sub DoSomething(ByVal sc As SimpleClass)
    'Do Something with sc here
    End Sub


    Finally, I create a Windows Forms Application to consume the web service.
    I add the Web Reference to the web service and also Reference the
    SimpleClass library and everything looks OK so far, until I try to use
    them. Here is an example

    Private Sub Button1_Click(...) Handles Button1.Click
    'Declare an instance of the web service
    Dim ws As New localhost.Service1

    'Create an instance of the simple class
    Dim sc As New SimpleClass

    'Now call the web service method:
    ws.DoSomething(sc) '<------- Error here
    End Sub

    When I try this, it tells me that it cant convert an instance of
    SimpleClass to localhost.SimpleClass.

    If I change the instantiation of the simple class as follows:

    Dim sc As New localhost.SimpleClass

    Then it seems to work OK. But my problem is that if SimpleClass in the
    ClassLibrary has methods and not just properties, those methods are not
    available in the localhost.SimpleClass version, just the properties.

    Is it possible to share a ClassLibrary in this manner?

    Any assistance would be appreciated.

    Chris
     
    Chris Dunaway, Feb 27, 2004
    #1
    1. Advertisements

  2. Chris Dunaway

    Malek Guest

    Your code didn't work, not because your class cannot be shared between the
    server and client, but because the proxy has declared the method argument as
    being localhost.SimpleClass ... arrange your proxy class, and it will do
    fine ...

    However, you are defeating the whole idea behind web services when you want
    to have the class shared ... You are totally in an RPC mind set, why then
    use a message oriented technology and architecture ? Do you really mean to
    do your class sharing outside of your own context (with partners and such)?
    why ? if it is more like an intranet setting, Enterprise Services stays your
    best choice, most performing, most secure and richest features ... if it is
    a problem with going through firewalls and such (although I don't see why
    you would really need RPC outside of firewalls, if you think a little about
    it) why not go for remoting (it can use http, it can go through firewalls
    ....etc. Well, I have lots of reasons for not recommending it, but still, if
    you need RPC so badly across firewalls...).
    If it is about interop, then class sharing is totally out of question ...
     
    Malek, Feb 28, 2004
    #2
    1. Advertisements

  3. Hi Chris,

    No you can't do this unfortunately with standard Web Services. I've also
    often wanted to do this because I have many application where the same
    business objects sit both on the client and the server. It seems logicial
    that you should be able to pass an object from client to server, but the
    plain WebService architecture MS has doesn't work that way.

    As Malek pointed out Web Services work of Proxy objects which are actually
    very different than the 'live' object that is passed back from the server.
    For one thing it has a completely different structure as methods aren't
    proxied.

    SO your workaround in these scenarios is to copy properties or use smart
    classes to ship over the wire that hold all of their data in a format that's
    easy to reattach in some way. FOr example, you can pass datasets back and
    forth and simply pick up the data sets and attach them to your client
    object.


    +++ Rick ---

    --

    Rick Strahl
    West Wind Technologies
    http://www.west-wind.com/
    http://www.west-wind.com/weblog/
     
    Rick Strahl [MVP], Feb 28, 2004
    #3
  4. On Sat, 28 Feb 2004 00:35:48 -0000, Malek wrote:

    Thanks for the response. Perhaps web services are not the way to go for my
    project. We offer a service in which all the equipment needed to carry
    out that service is maintained at our central facility. We want to give
    clients the ability to submit a 'job' over the internet to use our service.
    I am creating a 'Thin Client' app to allow them to do this. The web
    service will only be accessed by this 'Thin Client' app and not made
    available otherwise. This 'Thin Client' will allow the customer to gather
    necessary data at their end and submit to our central facility for
    processing. We would then provide them with a report with the results of
    the processing.

    Web services seemed to be a simple way to allow the client app to
    communicate with our servers. Do you think remoting would be a more
    appropriate method for this type of project?

    Thanks again
     
    Chris Dunaway, Mar 1, 2004
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.