To understand Web services better, you'll build a simple Web service. The Web service will receive an order ID and then query the Northwind database's Orders table for the correct order. It will then return the order in a DataSet.
The first step in creating the Web service is to select File New Project and choose the ASP.NET Web Service template, as shown in Figure 15-1.
Figure 15-1: Creating a new Web service project
This creates a Web service project on the local Web server. By default, localhost is available as a Web server on your development machine. Alternatively you could use another Web server for your applications. After entering a server name, you enter a project name. In this sample, the project name is OrderRetrievalWebService. VS .NET creates a new folder with the project name on the Web server and keeps all project files under that folder.
First, the Web.config file is an XML file containing information on how to run the service under ASP.NET. Second, the Global.asax and Global.asax.vb files enable you to handle application-level events for the service. These event handlers include event handling pairs such as Application_Start/Application_End, Session_Start/Session_End, and Begin_Request/EndRequest. You can view Global.asax.vb by right-clicking the Global.asax file in the Solution Explorer and choosing View Code. Next, OrderRetrievalWebService.vsdisco is an XML file containing information for discovery of the service. You'll actually need to generate an OrderRetrievalWebService.disco file for your client to see the service (more on this later).
Finally, the Service1.asmx file serves as the entry point into the service. The code behind the service, Service1.asmx.vb, is where you'll place the method for retrieving the order. Right-click the Service1.asmx file in the Solution Explorer and choose View Code from the pop-up menu. Note that it looks like any other VB .NET component; it has a constructor, an InitializeComponent method, and a Dispose method.
Now you'll add database support to the Web service. Actually, adding database support to a Web service is quite easy using ADO.NET. You can add ADO.NET data components to a Web service by just dragging ADO.NET components from the Toolbox's Data tab to Web Forms. Similar to Windows or Web Forms, you can also use the Server Explorer to add database components to a Web service.
In this example, you'll use the SQL Server Northwind database. You won't use the design-time support for ADO.NET, for two reasons. Firstly, when you drop a table from the Server Explorer onto a Web page, the designer adds lots of unwanted code. Secondly, it's hard to understand and modify the designer-added code. We recommend writing code manually.
Now, the next step is to add a Web method to the project. You can use the Class Wizard to add a method, or you can add a method manually. If you examine the default code in Service1.asmx.vb, it has a method called HelloWorld, which looks like following:
'<WebMethod()> Public Function HelloWorld() As String 'HelloWorld = "Hello World" ' End Function
As you can see from this code, adding a Web method is simple. You just need to add the <WebMethod()> attribute in front of a method. Add a method called GetOrderFromDatabase. This method reads data from a database and returns the data as a DataSet object. The method takes an OrderID as an argument and returns all records related to that OrderID. Before you add the method, though, you also need to add the variables in the following code to the project:
Private ConnectionString As String = "user id=sa;password=;" & _ "Initial Catalog=Northwind;" & _ "Data Source=MCB;" Private conn As SqlConnection = Nothing Private adapter As SqlDataAdapter = Nothing Private sql As String = Nothing
The GetOrdersFromDatabase method fills data from the Orders table of the Northwind database and returns a DataSet that has records from the Orders table corresponding to an order ID. Listing 15-1 shows the GetOrdersFromDatabase method.
Listing 15-1: The GetOrdersFromDatabase Method for Obtaining an Order from an Order ID
<WebMethod()> Public Function GetOrderFromDatabase(_ ByVal orderID As Integer) As DataSet Dim ds As DataSet = New DataSet() ' Create a new connection conn = New SqlConnection(ConnectionString) sql = "SELECT * FROM Orders WHERE OrderID = " + orderID.ToString() ' Open the connection If (conn.State <> ConnectionState.Open) Then conn.Open() End If ' Create a DataAdapter Dim adapter As SqlDataAdapter = New SqlDataAdapter(sql, conn) adapter.Fill(ds) ' Close the connection If (conn.State = ConnectionState.Open) Then conn.Close() End If ' Return DataSet Return ds End Function
As you can see from Listing 15-1, the method receives an order ID from a client and returns a DataSet to the client. It uses the DataAdapter to fill a DataSet with the row containing that order ID.
You have to add a WHERE clause onto the SelectCommand to filter the single row of data. Also, you must make all exposed methods in the Web service public, and they must contain a [WebMethod] attribute; otherwise, the client won't see them.