Providing Web Services Video Highlights

  • Creating a Web service
  • Testing a Web service
  • Returning tabular data from a database
  • Securing the data exchanged in a Web service

Evaluate Visual Studio

Introduction

A previous video presented server-side code that consumes a Web service. In this video, we will examine server-side technology to publish these kinds of services to the Internet. Requests and responses travel across the same network protocols that Web pages do, so the traffic is uninhibited by firewalls, and can be received wherever a browser can surf to a Web page. Additionally, Web services can be used inside an intranet environment as a common ground between dissimilar systems.

Dreamweaver Section

Dreamweaver Version 8 does not have support to create a server-side solutions to publish Web services.

Visual Studio 2005 Section

Visual Studio 2005 allows you to add an ASMX page to a project by referencing the context menu of the project and selecting "Add Item." Then in the list of templates, choose "Web Service." This kind of page accepts requests either with standard HTTP GET and POST, or with the Simple Object Access Protocol, also known as SOAP. A response is given in XML. It is thus compatible with any system that can consume a Web service.

To get started writing the code, examine the sample method provided in the template from Visual Studio. It responds with the string "Hello World" whenever it is called. In the same fashion we will add a function called "AddTwoNumbers," and take in two integer parameters. This will simply return the sum of these two, A plus B. We will need to mark the method with the "WebMethod" attribute in order for it to act as a method available in the Web service. We can test the service by using the "Start with Debugging" option on the toolbar. This page that appears is a test harness that allows us to select the method we want to run, and place the two integers in. 3 plus 8, and the response that comes back is this simple XML document with the answer of 11.

Back in the code, let's now create a more relevant example that pulls data from the Northwind database. In this data, set we will examine two tables: Customers and Orders. There are 830 orders that have been placed by 91 various customers. We would like to create a Web service that takes the customer ID and returns all of the orders that were placed by that customer. At first glance, it may appear that we have quite a bit of work to do in converting the data into the proper XML that will be expected by the consumer of the Web service. But this is where we can leverage a nice feature of the DataSet object. If a Web service returns a DataSet, then all of the XML conversion is done for us automatically.

All we need to add now is a SqlDataAdapter object that populates the DataSet. It can be built with two parameters: the SQL statement we wish to run, and the connection string. We will add the incoming customerId variable as a parameter to that query, and we are now prepared to test our solution. Running the same test harness again, we will reference the "OrdersByCustomerId" method. And providing the customer ID of 'QUICK' for the Quick-Stop store from Germany, we can click "Invoke" and see the items that have been ordered in the past.

Using a Web service, it is possible to share data across the Internet or an intranet. Security measures such as SSL and certificates can be applied. Dissimilar systems can easily communicate using these protocols. Altogether it becomes a secure way to easily share data between systems.

Conclusion

In this video, we looked at the ability to publish a Web Service using Visual Studio 2005.