Microsoft Visual C#.NET 2003 Kick Start
We've seen Windows services, and many people are familiar with them on a day-to-day basis. But what about Web services? They're less familiar than Windows services to most people, but the idea is the samethey're code componentson Web servers this timethat your code can call and that can return data, much as you'd work with a Windows service. For example, Web services can connect to databases, letting you retrieve data from data sources, and you can connect to Web services from either Windows or Web applications. Using Web services, you can implement custom logic such as performing a credit check before approving a loan. Web services are often used as middle-tier business objects, which work with and transfer data in three- tier data Web applications. Writing the Web Service
As an example, we're going to create a three-tier data application here. This example is going to use its middle tierour Web serviceto connect to the authors example database and fetch or update data from that database. The lowest tier is a Windows application, the middle tier is our Web service that fetches or updates data on demand (although note you can also implement business rules, such as only returning authors for whom you have books in stock, in the middle tier), and the top tier is the authors database in SQL Server. This Web service example is named ch02_12. In Web services, you can implement methods callable from Windows or Web applications (and, in fact, from applications running on other platforms as well); in this example, we're going to write two methods : GetAuthors to return a dataset holding the authors table, and UpdateAuthors to update that table as needed. We're going to use a Windows application to call these Web service methods, and we're going to use a data grid to display the authors table. As long as the user 's machine is connected to the Internet, our operations will appear as though they're taking place entirely in the Windows application. To follow along, create a new Web service project named ch12_02 with File, New, Project, selecting the ASP.NET Web Service icon. Create the new Web service project you see in Figure 12.7. Figure 12.7. A new Web service project.
The default name of our new Web service is Service1 , and we'll keep that name. The C# code for this service is stored in Service1.asmx.cs , and in that file you'll see that this new Web service is derived from the WebService class:
public class Service1 : System.Web.Services.WebService { public Service1() { . . . Because we intend to use the authors database, we'll need to connect to it. As we've done before, drag a SqlDataAdapter object from the toolbox; in this case, drag that object onto the Web service designer. Doing so opens the Data Adapter Configuration Wizard; use that tool to connect the data adapter to the authors table. Finally, use Data, Generate Dataset to create a new dataset class, DataSet1 (no dataset object are created, just the class DataSet1 ). This is the dataset class we'll use to access the authors table in the Web service. (Alternatively, you can connect and create a dataset in code, storing the connection string in the project's Web.config file.) How does the Windows application connect to this new Web service? The Web service will expose methods that we'll write, and the Windows application can call those methods after we create a reference to our Web service. To expose methods in a Web service, you use the [WebMethod] attribute. For example, in the GetAuthors method, we want to return a dataset filled with the authors table, so we add this code to Service1.asmx.cs now:
[WebMethod] public DataSet1 GetAuthors() { DataSet1 authors = new DataSet1(); sqlDataAdapter1.Fill(authors); return authors; } This code makes the GetAuthors method return a dataset object of our new DataSet1 class that will hold the authors table. As you can see, all we need to do is to create a new object of that class and use the data adapter to fill the object before returning it to the calling code. The UpdateAuthors method, which updates the authors table when the user makes changes in the data grid, is easy to write. We'll pass this method a dataset holding the changed records in the authors table and update the authors table using the data adapter's Update method like this:
[WebMethod] public DataSet1 UpdateAuthors(DataSet1 UpdatedRecords) { sqlDataAdapter1.Update(UpdatedRecords); return UpdatedRecords; } We've created our two Web methods, GetAuthors and UpdateAuthors , at this point. To make this Web service available to our Windows application, you can build the Web service now using Build, Build ch12_02 in the IDE. Our Web service is ready for use. Writing a Windows Application to Connect to Our Web Service
The next step involves writing the Windows application that will call the Web service's GetAuthors and UpdateAuthors methods. To create this Windows application, add a new Windows Application project to the current solution with File, Add Project, New Project. (This application doesn't have to be part of the current solution to connect to the Web service. You can add a reference to the Web service in any Windows application.) Select the Windows Application icon in the Add New Project dialog box, name this new Windows application ch12_03, and click OK to create the Windows application as you see in Figure 12.8. Also, make this Windows application the startup project by selecting it in the Solution Explorer and using Project, Set as StartUp Project. Figure 12.8. A new Windows application.
Now that we've created our Windows application, the next step is to connect to the Web service, which you do by adding a Web reference to the Web service. Right-click the ch12_03 entry in the Solution Explorer and select Add Web Reference, opening the Add Web Reference dialog box. This dialog box lists the available Web services. To add a reference to a Web service, you can enter the URL for the service's .VSDISCO (Visual Studio Discovery) file in the Add Web Reference dialog box's Address box. You can also browse to the service by clicking the link in the Add Web Reference dialog box for the server you want and then clicking the name of the service, which is Service1 in this example. When you select a Web service this way, that Web service appears in the Add Web Reference dialog box, as you see in Figure 12.9, where the Add Web Reference dialog box is displaying the Service1 Web service. To add a Web reference to this service to the Windows application, click the Add Reference button. Figure 12.9. The Add Web Reference dialog box.
At this point, we have a Web reference to Service1 , which means our Windows application will know how to find the GetAuthors and UpdateAuthors methods. To put those methods to work in the Windows application, add a data grid to the application and put two buttons above the data grid with the captions Get Authors and Update Authors. To handle data from the Web service, we'll need to let the Windows application know about the DataSet1 class in the Web service. To do that, drag a new DataSet objectnot a data adapterfrom the Data tab of the toolbox to the Windows application. When you do, the Add DataSet dialog box opens. In the Add DataSet dialog box, make sure the Typed Dataset radio button is selected, and select the dataset class in our Web service, DataSet1 , from the drop-down list. Note that the fully qualified name of DataSet1 is ch12_03.localhost.DataSet1 , which is the way it appears in the Add DataSet dialog box, as shown in Figure 12.10. Figure 12.10. The Add DataSet dialog box.
To create a new dataset object of the DataSet1 class, dataSet11 , click the OK button in the Add DataSet dialog box. Now we have a dataset that matches the DataSet1 class in the Web server, which means we can use that dataset as a repository for the data sent to us from the Web service. We bind dataSet11 to the data grid in the Windows application by setting the data grid's DataSource property to dataSet11 , and its DataMember property to authors . We want to fill dataSet11 with data when the user clicks the Get Authors button. When the user clicks that button we'll start by creating an object, service1 , of our Web service:
private void button1_Click(object sender, System.EventArgs e) { ch12_03.localhost.Service1 service1 = new ch12_03.localhost.Service1(); . . . } Using this new object, service1 , you now have access to the methods built into our Web service. For example, if you wanted to fetch data using the GetAuthors method, you just have to call that method like this: service1.GetAuthors() , which returns a dataset filled with the authors table. You can store the data from that dataset in dataSet11 with the Merge method like this:
private void button1_Click(object sender, System.EventArgs e) { ch12_03.localhost.Service1 service1 = new ch12_03.localhost.Service1(); dataSet11.Merge(service1.GetAuthors()); } That's how the process worksyou create a new object corresponding to the Web service, much as you would with any reference added to your application. Then you can use that object's methods to call the Web service directly. Besides the GetAuthors method, we can also call the UpdateAuthors method when the user clicks the Update Authors button. When the user makes changes in the data grid, those changes are sent to our local dataset. We need to send those changes back to the data store when the user clicks the Update Authors button. You can do that by creating a dataset holding just the changed records using the dataset's GetChanges method and the DataSet Merge method:
private void button2_Click(object sender, System.EventArgs e) { ch12_03.localhost.DataSet1 update1 = new ch12_03.localhost.DataSet1(); update1.Merge(dataSet11.GetChanges()); . . . } We'll call the Web service's UpdateAuthors method to update the authors table. That method returns the changed records, and we will merge them back into the Windows application's dataset object, making sure those records will no longer be marked as newly changed:
private void button2_Click(object sender, System.EventArgs e) { ch12_03.localhost.Service1 service1 = new ch12_03.localhost.Service1(); ch12_03.localhost.DataSet1 update1 = new ch12_03.localhost.DataSet1(); update1.Merge(dataSet11.GetChanges()); dataSet11.Merge(service1.UpdateAuthors(update1)); } Finally, add a data grid to the Windows application and bind it to dataSet11 to show our results. That completes the code we need for both the Web service and the Windows application that connects to it. Run this example now and click the Get Authors button. When you do, the authors table is retrieved from the Web service and displayed in the bound data grid, as you see in Figure 12.11. Figure 12.11. Connecting to a Web service from a Windows application.
In addition, when you edit the data in the data grid and click the Update Authors button, those changes will be sent back to the data store. And that's itwe've created a Web service and connected to that Web service in code from a Windows application. As far as the user is concerned , the whole connection process to the Web service was transparent. Web services like this are great for many uses. Employees out in the field using Web services, for example, can connect immediately to the latest version of the home office's business logic as posted to the Internet. Working with the WebService Class
As we've seen, the System.Web.Services.WebService class is the base class for Web Services. You can find the significant public properties of objects in the Webservice class in Table 12.13. Table 12.13. Significant Public Properties of Webservice Objects
|