Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Web Development  »  Web Services  »  WebServices and passing a user-defined Object
WebServices and passing a user-defined Object
by Jeff Laughlin | Published  06/12/2002 | Web Services | Rating:
Jeff Laughlin

Jeff Laughlin is founder of Coding Canada, a software development and web hosting company in Toronto, Canada. He has over 5 years of experience in the software industry having worked with a wide variety of computing technologies building both traditional windows applications and also advanced commerce and non-commerce websites.

Jeff has used Microsoft's .NET technologies since the first Beta release switching between C# and VB.NET to ensure that he remains current in both languages. Jeff has successfully launched a number of .NET applications including an internal newsletter processing application, a WebService enhanced application for a law firm, and numerous ASP.NET websites (both commerce and non-commerce).

Most recently, Jeff has focussed on Microsoft technologies including C#, VB.NET, ASP.NET, ASP, Visual Basic 6, SQL Server, and WebService development.

 

View all articles by Jeff Laughlin...
WebServices and passing a user-defined Object

Article source code: websvc_object.zip

This article will demonstrate how to pass a user-created object from a server to a client. The server will pass the object through a Web Service (therefore IIS will be required on the server). You will be able to view the serialized object through any standard web browser such as Internet Explorer, Opera, or Netscape Navigator. A simple Windows Client application will also be created in order to show how to take the object and use it directly in standard Windows components such as text boxes and labels. This article does assume that you have a base familiarity with Web Services, Object creation, and Internet Information Server.

What are Web Services?

Web Services allow programmers to easily solve one of the fundamental problems facing many programmers today: how to get information from "here" to "there". Web Services are easy to code, based on open XML standards, and can be scaled both up and out in the same manner that IIS web servers are (by adding more processing power to existing hardware or adding additional servers in a web farm layout). For more information regarding Web Services (and for samples on transferring XML and SQL DataSets through Web Services please see my previous DevCity.NET article at http://www.devcity.net/net/article.aspx?alias=20020331

Sending an Object through a Web Service

Using a Web Service to send an Object is almost as easy as sending standard object types (such as strings or integers). The main difference is that a user-created object is not intrinsically "known" to the calling application until a web reference is made. Of course, as with most things in .NET, making the reference to a remote object is painless.

Sending user-created objects through Web Services is a two-step process: first, the object needs be defined; then the object needs to be passed.

Step 1: Define Your Object

Public Class ApartmentDetails
    Public ApartmentID As Integer
    Public City As String
    Public RoomCount As Integer
    Public SquareFeet As Integer
    Public HighSpeedCapacity As Boolean
    Public MonthlyCharge As Integer
End Class

Step 2: Pass The Object Through A Web Service

<WebMethod()> Public Function ListApartmentDetails() as ApartmentDetails
   Dim myObjectToSend as New ApartmentDetails()
   With myObjectToSend
      .ApartmentID = 0
      .City = "CityTest"
      .RoomCount = 1
      .SquareFeet = 1000
      .HighSpeedCapacity = True
      .MonthlyCharge = 5000
   End With
   Return myObjectToSend
End Function

Deploying a Web Service

Much like a traditional website deploying a Web Service is not a difficult process though it is not without its potential hang-ups.

If you have built your Web Service through the Visual Studio.NET IDE then all local deployment issues have been taken care of automatically. If you wish to move your solution to another computer you can choose to deploy your service using Visual Studio.NET's build-in wizard or you can choose to copy the site to a folder on the other computer. If you choose to copy the site then you will need to perform one additional step using the IIS MMC interface. You will need to identify the folder as a Virtual Directory Application in order for the proper configuration settings to be handled by the ASP.NET processor. If you forget to designate the folder as an application you are likely to receive the following error message when attempting to load the URL into your web browser.

It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS

Testing the Web Service

Visual Studio.NET provides a very simple way to test Web Services using nothing more than a web browser. Specify the ASMX file in your web browser's address bar to connect to your Web Service and test your code.

http://localhost/ApartmentDetails/Service1.asmx

Windows Client Application Testing

Using the Web Service's object in your local application is a straightforward two-step process. On the Web Service's end you needed to both define the Object then make it available for outside use. Similarly, on the client end we need to retrieve the Web Service's definition of the object, then retrieve an instance of that object.

Step 1: Retrieve the Web Service's Object Definition

Dim localApartmentDetailsObject As New localhost.ApartmentDetails()

Step 2: Create an instance of the new object

Dim remoteWebService As New localhost.Service1()
localApartmentDetailsObject = remoteWebService.ListApartmentDetails

Downloadable Code

The sample application provided for download includes all code used in creating this article as well as a functioning sample web service and client application. A SQL Server 2000 script is included which will allow you to generate the exact dataset I used while writing the article. A sample web service and client application is provided demonstrating the transfer of both an object and a standard ADO.NET Dataset.

Related devCity.NET articles:

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:2.88888888888888 out of 5
 18 people have rated this page
Article Score17323
Comments    Submit Comment

Comment #1  (Posted by Robin on 01/04/2004)

So, how do you make it work from a remote server?
 
Comment #2  (Posted by F.G. on 03/22/2004)

I'm new to this technology but with some test i've made, this doen't transfer an object at all, it only transfer a structure of values. If a function would be defined in the object, it wouldn't be available on the client side.
If someone know how to transfer a real object from a web service to a client app, please tell me. thanks
 
Comment #3  (Posted by Russ on 04/15/2004)

How do you code the calling application to the web service with an alias or intermediate class of localhost, so that when the webservice is move to a real web host - you don't need to go use find replace all over to replace localhost with the new name?
 
Comment #4  (Posted by a c on 09/25/2004)

Gee....learn some serious programming before writing articles that may mislead other poor programmers!

Two errors - I'm sure there are more errors - but did not want to waste my time;

Dim localApartmentDetailsObject As New localhost.ApartmentDetails()

'This is Bad - you are not typing the object, so it will be just an Object. Plse use following syntax;
'Dim localApartmentDetailsObject As localhost.ApartmentDetails = New localhost.ApartmentDetails

Dim remoteWebService As New localhost.Service1()
'You do not have to do this instantiation as you have always ddone it above!!!! (calling New twice is Bad)

Same goes for the rest of this highly inefficient code!

/A - true coder

 
Comment #5  (Posted by an unknown user on 02/28/2005)
Rating
there is no depth to the article(i.e. serialization of properties, which accessors to use, how to define attributes), and the title misleads those who search for information - this article is about returning an object from WS, not "passing" one!!!
 
Comment #6  (Posted by an unknown user on 04/20/2005)
Rating
we need some more depth to the article(i.e. serialization of properties, which accessors to use, how to define attributes), and the title misleads those who search for information - this article is about returning an object from WS, not "passing" one!!!


 
Comment #7  (Posted by Mark on 05/31/2005)
Rating
Hey, thanks for the great example. I got a question though. When I compare this output to a DataSet output the XML is completely different. Your above example works fine in Flash MX 2004, but when I use the other one that uses the DataSet it fails. Why are they so different in the output?
 
Comment #8  (Posted by Tracy on 11/21/2005)
Rating
I agree with F.G on this issue.The sample source just returns an object with the public properties filled.If there were a function defined in the class
ApartmentDetails,the function cannot be invoked in the client.I guess maybe it is not permitted by SOAP to pass an object to the client.What do you think about it?
 
Comment #9  (Posted by an unknown user on 01/27/2006)
Rating
I'm using a SOAP Client to send over the data to the webservice. (ie:
Dim str As String = "1"
str += "Tinley Park"
str += "4"
str += "4500"
str += "1"
str += "1500")

How would I go about sending more than one apartment at a time?
 
Comment #10  (Posted by an unknown user on 04/03/2006)
Rating
it is good article.
 
Comment #11  (Posted by an unknown user on 10/10/2006)
Rating
badly written article
 
Comment #12  (Posted by an unknown user on 02/21/2007)
Rating
It's a terrible article.
 
Comment #13  (Posted by Mathias on 09/13/2007)
Rating
Perfect!
 
Comment #14  (Posted by an unknown user on 11/16/2007)
Rating
Sending an object? No, you sent back fields of data (in .NET 1.x) or properties ( .Net 2.0). No methods. The problem is with wsdl.exe constructing the partial class in the client application -- it doesn't handle the methods of the class. The only solution that I have found so far is to work with SchemaImporterExtensions in .Net 2.0

Hope this helps ...
 
Comment #15  (Posted by an unknown user on 02/12/2008)
Rating
Although this article covers what you intend to tell, the article is very misleading. You could have constructed it in a much better fashion. You have wasted a winner.
 
Comment #16  (Posted by an unknown user on 03/06/2008)
Rating
Crappy and it doesn't work.
 
Sponsored Links