Creating Your First .NET Web Service


Listing 9.1 gets you started with your first Web service.

Listing 9.1 Your First .NET Web Service
  <%@ WebService Language="C#" Class="hks.HelloService"%>  using System; using System.Web; using System.Web.Services; namespace hks {  [WebService(Namespace="http://www.hiteshseth.com/webservices")]  public class HelloService    {  [WebMethod(Description="Simple Hello Method")]  public string sayHello()       {          return "Returned from a C# Web Service";       }    } } 

Apart from the bold text in the preceding code, everything perhaps looks similar to your first C# program, without the Main() method. Creating a .NET Web service is similar to creating an ASP.NET page, which explains the first line; the directive WebService states that this page is really a Web service, has been implemented in C#, and the Web service represented by this page is manifested in the HelloService class in the hks namespace.

Next, the WebService[] attribute lets the .NET Framework compiler know the HelloService class is a Web service and the WebMethod[] attribute explains that the sayHello() method should be exposed as a Web services method (in short, a WebMethod), also known as an endpoint.

Then, you save this file (for example, HelloService.asmx, in the Web root directory; for example, C:\inetpub\ wwwroot ). Open your favorite browser and go to http://localhost/HelloService.asmx .

As part of the development of the Web service, you didn't really develop any user interface to invoke it. So the first page (Figure 9.1) may come as a surprise, but pretty soon you will realize that this is actually pleasant because the .NET Framework has done several things:

  • It validated and compiled the Web service into MSIL code (similar to an ASP.NET page).

  • Because a browser request indicated the code to be executed, the .NET Framework used the JIT compiler to convert the MSIL code for the Web service into machine code.

  • As part of the Web services runtime, the .NET Framework automatically creates a test Web page, which allows the users to list the various Web services methods and test the functionality of the methods and the associated XML interfaces.

Figure 9.1. Your first .NET Web service.

If you go ahead and click the sayHello link, it will bring up an actual test page for invoking the Web services method. Next, if you click the Invoke button, you should see the actual XML returned, which contained the string returned by the Web service (Figure 9.2).

Figure 9.2. Running your first .NET Web service.

You must be thinking that Web services are for intercommunication with programs. Indeed, that is the overall objective. Let's get started with actually using this Web service in a distributed application. The .NET Framework SDK contains a tool, WSDL.EXE (Web Services Description Language Utility), which allows developers to create the necessary code for running Web services remotely and locally from another .NET component. The .NET component can be a Web or Windows application, another Web service, or any other middle- tier .NET managed component.

 
 wsdl http://localhost/WebServices/HelloService.asmx?WSDL 

This generates the Web service proxy, HelloService.cs (there are command-line options to WSDL to specify a namespace for the generated proxy as well).

 
[View full width]
 
[View full width]
using System.Web.Services; ... [System.Web.Services.WebServiceBindingAttribute(Name="HelloServiceSoap", Namespace="http: //www.hiteshseth.com/webservices")] public class HelloService : SoapHttpClientProtocol { ... public HelloService() { this.Url = "http://localhost/WebServices/HelloService.asmx"; } ... }

After the proxy for the Web service has been generated and compiled into a DLL, the Web service can be invoked by another application and developed in any .NET Framework supported programming language. For instance, following is the code snippet using the HelloService in a C# application.

 
 using System; public class Invoke {    public static void Main() {       HelloService hs = new HelloService();       Console.WriteLine(hs.sayHello());    } } 


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