Creating Web Services

   


To better understand Web services, you should be familiar with both sides of the conversation. In this section, you'll learn how to create a Web service by using the tools built in to ASP.NET.

Creating a Web Service Project

To create a Web service, you can build an ASP.NET project in Visual Studio .NET.

NOTE

Web Server Required You'll need to have a Web server available to you to complete the exercises in this section.


STEP BY STEP

4.2 Creating a Web Service

  1. Create a new project in Visual Studio .NET. Select the ASP.NET Web Service template and name the new project StringProc , as shown in Figure 4.4. You should replace HOURGLASS with the name of your own Web server.

    Figure 4.4. Creating a new Web service project.

  2. Right-click the Service1.asmx file in Solution Explorer and rename it Strings.asmx .

  3. Click the hyperlink on the Strings.asmx design surface to switch to code view. Enter this code for the class (don't alter the Web services designer generated code):

     Imports System.Web.Services <WebService(Namespace:= _  "http://NetExam.org/StringProc")> _ Public Class Strings     Inherits System.Web.Services.WebService     <WebMethod()> Public Function ToUpper(_      ByVal inputString As String) _      As String         ToUpper = inputString.ToUpper()     End Function     <WebMethod()> Public Function ToLower(_      ByVal inputString As String) _      As String         ToLower = inputString.ToLower()     End Function End Class 
  4. Save the project.

  5. Select Build, Build Solution to create the Web service on the server.

You now have a functioning Web service on your Web server. Congratulations! Although a lot of plumbing is involved in properly hooking up a Web service, Visual Studio .NET protects you from having to set up any of it. Instead, you only have to do three things:

  1. Build your project from the ASP.NET Web Service template.

  2. Mark the classes that should be available via the Web service with the WebService attribute.

  3. Mark the methods that should be available via the Web service with the WebMethod attribute.

EXAM TIP

The Web Service Namespace The WebService attribute requires you to supply a value for the Namespace property. This value ( http://NetExam.org/StringProc in this example) is purely arbitrary. It doesn't have to resolve to any actual Web site. This string is just a unique identifier for your Web service. If you leave the default value instead of changing it, you'll get a warning from Visual Studio .NET.


Testing the Web Service Project

Visual Studio .NET includes built-in tools, hosted on an HTML test page, for testing a Web service project without building any client applications for the Web service. Step by Step 4.3 shows you how to use these tools, which can save you time when you're debugging a Web service.

STEP BY STEP

4.3 Testing a Web Service

  1. Start with the Web Service project from Step by Step 4.2. Run the project. This launches a browser and opens the test page shown in Figure 4.5.

    Figure 4.5. A Web service test page.

  2. Click the Service Description link on the test page. This lets you view the WSDL for this Web service. Click the Back button in the browser to return to the test page.

  3. Click the ToUpper link on the test page. This opens a page for testing the ToUpper method, as shown in Figure 4.6.

    Figure 4.6. A test page for a Web method.

  4. The Web method test page shows the SOAP and other messages that the Web service understands. It also contains a form to allow you to test the Web method.

  5. Enter a string with mixed uppercase and lowercase characters in the inputString prompt.

  6. Click the Invoke button. A second browser window opens, as shown in Figure 4.7, with the XML message that the Web service sends back when you call the ToUpper method on your test string.

    Figure 4.7. Testing a Web method.

  7. You can also experiment with the ToLower method in the same way. When you click the Invoke button, the test page constructs the appropriate XML message and passes it to the Web service, which returns the results.

GUIDED PRACTICE EXERCISE 4.1

In this exercise, you'll be connecting a Web service to a SQL Server database. The goal is to allow a client application to perform a database lookup via the Web service.

Specifically, you should perform the following tasks :

  1. Build a Web service that exposes a single class named Customer . The Customer class should expose a Web method named GetCustomers . The GetCustomers Web method should accept a country name and return a Dataset that contains all the customers from that country. Use the data from the Customers table of the Northwind sample database.

  2. Build a client application that uses the Web service from step 1. The user should be able to enter a country name and see all the customers from that country.

You should try doing this on your own first. If you get stuck, or you'd like to see one possible solution, follow these steps:

  1. Create a new project in Visual Studio .NET. Select the ASP.NET Web Service template and name the new project Northwind.

  2. Right-click the Service1.asmx file in Solution Explorer and rename it Customer.asmx .

  3. Click the hyperlink on the Customer.asmx design surface to switch to code view. Change the name of the class to Customer and enter this code at the top of the file:

     Imports System.Web.Services Imports System.Data Imports System.Data.SqlClient 
  4. Enter this code to create the GetCustomers Web method:

     <WebMethod()> Public Function GetCustomers(_  ByVal Country As String) As DataSet     ' Create a SqlConnection     Dim cnn As SqlConnection = _      New SqlConnection("Data Source=(local);" & _      "Initial Catalog=Northwind; " & _      "Integrated Security=SSPI")     ' Create a SqlCommand     Dim cmd As SqlCommand = cnn.CreateCommand()     cmd.CommandType = CommandType.Text     cmd.CommandText = "SELECT * FROM Customers " & _     "WHERE Country = '" & _      Country & "'"     ' Set up the DataAdapter and fill the DataSet     Dim da As SqlDataAdapter = New SqlDataAdapter()     da.SelectCommand = cmd     Dim ds As DataSet = New DataSet()     da.Fill(ds, "Customers")     ' And return it to the client     GetCustomers = ds End Function 
  5. Select Build, Build Solution to create the Web service on the server.

  6. Now you can build the client application. Open your Windows application for this chapter and add a new Form to the project.

  7. Place a Label control, a TextBox control named txtCountry , a Button control named btnGetCustomers , and a DataGrid control named dgCustomers on the form. Figure 4.8 shows the design of this form.

    Figure 4.8. Designing a form to test the GetCustomers Web method.

  8. Right-click the References folder in Solution Explorer and select Add Web Reference. This opens the Add Web reference dialog box.

  9. Type http:// YourServerName /Northwind/Customer.asmx (substituting your own Web server name) into the Address bar of the Add Web Reference dialog box and press Enter. This connects to the server and downloads the information about the Northwind Web service.

  10. Click the Add Reference button.

  11. Double-click the Button control on the Form to open the Form's module. Enter this code at the top of the module:

     Imports System.Data Imports System.Data.SqlClient 
  12. Enter this code to handle the Button's click event. You'll need to replace " hourglass " with the name of your own Web server:

     Private Sub btnGetCustomers_Click(_  ByVal sender As System.Object, _  ByVal e As System.EventArgs) _  Handles btnGetCustomers.Click    ' Create a DataSet to hold    ' the customers of interest    Dim dsCustomers As DataSet    ' Connect to the Web service and retrieve customers    Dim cust As  hourglass  .Customer = _     New  hourglass  .Customer()    dsCustomers = cust.GetCustomers(txtCountry.Text)    ' Bind the results to the user interface    dgCustomers.DataSource = dsCustomers    dgCustomers.DataMember = "Customers" End Sub 
  13. Set the Form as the startup object for the project.

  14. Run the project and enter a country name (such as France). Click the button. After a brief delay while the project contacts the Web service, the DataGrid will fill with data as shown in Figure 4.9.

    Figure 4.9. Data supplied by the GetCustomers Web method.

As this exercise shows, you can return complex objects from a Web service as easily as you can return simple types. The Web service takes care of all the details of converting the DataSet to an XML representation, wrapping it in a SOAP message, sending it to the client, and reconstituting the DataSet there.

REVIEW BREAK

  • Visual Studio .NET includes an ASP.NET Web Service template that you can use to build your own Web services.

  • To make a class available via a Web service, you mark the class with the WebService attribute.

  • To make a method available via a Web service, you mark the method with the WebMethod attribute.

  • To test a Web service, run the project in the Visual Studio .NET IDE.


   
Top


MCAD. MCSD Training Guide (Exam 70-310. Developing XML Web Services and Server Components with Visual Basic. NET and the. NET Framework)
MCAD/MCSD Training Guide (70-310): Developing XML Web Services and Server Components with Visual Basic(R) .NET and the .NET Framework
ISBN: 0789728206
EAN: 2147483647
Year: 2002
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net