The same service now can be used from an ASP.NET Client application. Referencing the Web service can be done the same way as with the Windows application.
Try It Out – Creating an ASP.NET Client Application
Create a new C# ASP.NET Website, call it ASPNETClient, and add two text boxes and a button to the Web form, as shown in Figure 20-17.
Figure 20-17
Add a Web reference to http://localhost/webservicesample/service.asmx in the same way you did with the Windows application.
With the Web reference added, a client proxy class was again generated. Add a click handler to the button and write the following lines of code for this handler:
private void Button1_Click(object sender, System.EventArgs e) { WebServicesSample.Service ws = new WebServicesSample.Service(); TextBox2.Text = ws.ReverseString(TextBox1.Text); }
Now build the project and with Debug Start, you can start the browser and enter a test message in the first text box. When you press the button, the Web service is invoked, and you get the reversed message returned in the second text box, as shown in Figure 20-18. With a multi- project solution, you have to set the startup project to the project you want started.
Figure 20-18
As you have just seen, using Web services is as easy in Web applications as it is in Windows applications!