Want to start implementing a couple of impressive Web services to gain friends and influence people? Then read on, as the following developer secrets are unveiled:
Exposing Database Information, the Quick and Easy Way
Five Things You Need to Do Before Publicizing Your Web Service
Improving Performance with Easy Caching
Online Translations: Get Your Programs Speaking Spanish in Seconds!
Adding Google Search to Your Programs
Querying the Amazon.com Web Service
View the Real World in Your Application, with TerraServer
Asynchronous Access: Secrets of Calling a Web Service in the Background
Web Services, Proxies, Connection Closed ”Oh My!
Change a Web Service URL, Without Recompiling
Where to Find the Best Web Services Around
Let s get busy!
One of the problems that most Web service developers soon run into is... well, they can be a little limiting. A function may return a True or False , a number or a string. But that s not very much, really, is it?
The clever developers pass about smart data . By that, I mean, instead of a simple data type, they pass back something containing more than just one piece of information. A structure ( user -defined type) is a prime example of this. You may create a CustomerType structure and pass a value of this type back. Visual Studio .NET will recognize that you re passing a structure and expose its details, so users of your service can properly interpret the response.
However, one of the most useful types of smart data is the DataSet ”and here we ll be looking at how to expose the information in a database table through a Web service, allowing your users to view and update, in just a few lines of code. Here s how .
Create your Web service, remembering to note the location. Using the Server Explorer (View Server Explorer), connect to a database and drag and drop the table you d like to expose through this Web service onto your Service1.asmx page. (You may rename your page, but, for simplicity in this example, we re sticking with the filename Service1.asmx and the class name Service1.)
You should see two objects ”a Connection and DataAdapter ”created for you. The Connection object connects into the database, whereas the DataAdapter talks to the database through the connection, sending and retrieving information. You can learn more about these objects in Chapter 4.
Right-click on your DataAdapter and select Generate Dataset. Here, you ll be creating a template DataSet class, which you can create instances from. Edit the new DataSet name if required, and make a note for future reference. (For this sample, I m going to call mine MyDataSetTemplate.) Next , ensure that your table is selected in the list, check the Add this dataset to the designer box, and click OK when you re finished. (See Figure 5-1.)
You may not realize it, but two things have happened here. Firstly, a new DataSet -based class has been added to your project, behind the new XSD file, customized to your table and fields. Secondly, an instance of this new DataSet class has been added alongside your Connection and DataAdapter objects. (For this tip, I m going to rename mine from the default MyDataSetTemplate1 to MyDataSet.) This is known as a typed DataSet due to its hard-coded typed out knowledge of your database table and fields.
Next, we need to add code to perform two simple actions: firstly, code to allow our client to retrieve data from the table; secondly, code to allow our client to pass back an edited DataSet for updating of the database. Press F7 to view the code window and add code similar to the following to your service:
<WebMethod()> Public Function GetData() As MyDataSetTemplate ' Passes populated typed DataSet back to client MyDataAdapter.Fill(MyDataSet) Return MyDataSet End Function <WebMethod()> Public Sub UpdateData(ByVal Data As MyDataSetTemplate) ' Updates the backend database where needed MyDataSet.Update(Data) End Sub
When you re ready, click on Build Build Solution to compile your Web service. You can also press F5 (or click on Debug Start) to view the Web interface to your service, should you wish to do so. And that s it! You ve built your server, and ”with just a few lines of code ”created a retrieval and update mechanism for your table data.
So, the server is complete. Now we need to work on our client, and this bit is even easier.
Create a new application of any type; a Windows application is easiest . From the menu, select Project Add Web Reference and type in the location of the Web service ASMX file you just edited, such as http://localhost/mywebservicelocation/ Service1.asmx , and then press Return. When details of your service have finished loading, click on Add Reference. This will create a wrapper class for the Web service, allowing you to access its methods in your application.
Next, I m going to add a little code to my application to access the data through my Web service. Yours may be similar, or completely different. The following snippet, however, should give you a decent idea of how the preceding , highly simple method of passing DataSets back and forth can work on the client side:
' Declare Service and DataSet class Dim MyWebService As New localhost.Service1() Dim MyDataSet As localhost.MyDataSetTemplate ' Grab data MyDataSet = MyWebService.GetData ' Retrieve sample data note the