Creating Web Services


A key highlight of the .NET Framework and associated toolsets is development of Web services. Web services development will be discussed in further detail in Chapter 9, "Developing Web Services." In this section, however, you'll become familiar with how Visual Studio .NET supports development of Web services by developing a very simple Web service. After selecting a language of choice (in this case Visual C#), we select the ASP.NET Web Service project type in the New Project Wizard to develop a Web service, as shown in Figure 5.20.

Figure 5.20. Visual C#/ASP.NET Web service project wizard.

The project location parameter in the case of Web service is similar to that of a Web application; Web services are also hosted on an application server. You can use the default URL, http://localhost/WebService1 . Similar to other designer views, the Web service project begins with a visual designer view (Figure 5.21).

Figure 5.21. Web service designer.

However, in this case, you should choose to go to the code view to add a Web service method to your Web service. Notice that a simple code for a demo Web service has already been added; however, the code for the method HelloWorld has been commented out. You can uncomment that method to make the service code look similar to the following:

 
 using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services; namespace WebService1 {    /// <summary>    /// Summary description for Service1.    /// </summary>    public class Service1 : System.Web.Services.WebService    {       public Service1()       {          //CODEGEN: This call is required by the ASP.NET Web Services Designer          InitializeComponent();       }       ...       [WebMethod]       public string HelloWorld()       {          return "Hello World";       }    } } 

Similar to other projects, you can then go ahead and build the project. Again, if you want to execute the project from within this solution, you need to set this project as the Startup Project and then select Start Without Debugging. A Web browser is again invoked, but unlike the Web application where your Web form was displayed, a Web service test page is shown instead. This page is automatically generated by ASP.NET/.NET Framework to test the defined Web service. Because the Web service is really an HTTP/SOAP endpoint, it doesn't have any GUI. ASP.NET helps in the testing process of the Web service by providing this page.

If you select the HelloWorld method, you will be able to invoke the method and get the corresponding result, which should be something like the following:

 
 <?xml version="1.0" encoding="utf-8" ?> <string xmlns="http://tempuri.org/">Hello World</string> 

This is really what is involved to develop a basic Web service. Of course the capabilities available in .NET for Web services development go far beyond. In Chapter 9, you will learn about some of these features when you drill deeper into the mechanisms of creating Web services using .NET.



Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

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