Writing a Web Service


Web Services, unlike other ASP.NET files, have .asmx extensions. They are essentially all code with little or no user interface.

Each Web unlike Service can be discovered from a remote server via a .DISCO file, which is on the server containing the Web Service. This .DISCO has all the information necessary to enable the remote server to find out about available Web Services and how to use them.

Web Services make available a lot of information about themselves . They use the service description language (SDL) to do so. Although SDL is what .NET is using now, it is moving to Service Control Language (SCL) very soon. This is a more expressive version of the SDL. It will probably replace SDL by the final release of .NET.

Now I would like to show you examples of what I have been talking about. The first thing I would like you to take a look at is the contents of a discovery file as follows :

 <?xml version="1.0" ?>  <dynamicDiscovery    xmlns="urn:schemas-dynamicdiscovery:disco.2000-03-17">  <exclude path="_vti_cnf" />  </dynamicDiscovery> 

The next thing you should look at, just so you can familiarize yourself with it, is an SDL file. The following is a simple SDL file from a Web Service named HelloWorld.

 <?xml version="1.0"?>  <serviceDescription>    <soap >      <service>        <addresses>          <address uri="http://localhost//HelloWorld.asmx"/>        </addresses>        <requestResponse name="SayHelloWorld" soapAction= "http://tempuri.org/ graphics/ccc.gif SayHelloWorld">          <request ref="s0:SayHelloWorld"/>          <response ref="s0:SayHelloWorldResult"/>        </requestResponse>      </service>    </soap>  </serviceDescription> 

Finally, I would like to show you the SOAP payload that a simple Web Service might return. In the following example the SOAP envelope contains a credit card number and a credit card PIN.

 <?xml version="1.0"?>      <Envelope>          <Body>             <GetCCInfo>              <CCNum>12345</CCNum>          <CCPin>1234</CCPin>         </GetCCInfo>      </Body>  </Envelope> 

Now, I am going to walk through the creation of a simple Web Service. Start by running Visual Studio.NET. Go to the file menu, select New Project, and set up for a C# Web Service project.

VisualStudio.NET creates the project and all its support files. If you open the project's default .asmx (which is usually named WebServicex.asmx) file and then right-click on it to view the code, you can see the boilerplate code for the Web Service. In this code you can find a HelloWorld() method. The HelloWorld() method gives you an idea of how to create methods for Web Services.

The HelloWorld() method is actually remarked out when the code is first created. If you unremarked it out you could make the following observations. First, there is a Web method attribute above it. This is all that is necessary to make any method in a Web Service implement SOAP to communicate across the Internet. The rest of the method looks pretty standard. It is declared as public. It might or might not have a return type and it might or might not have parameters.

I am going to create a Web Service for this discussion that will have a method that multiplies two numbers . When I create the project I will open up the multiply.asmx and find the HelloWorld() method. Next I will remove the HelloWorld() method and create my own method called multiply .

The multiply method takes two parameters and they are both integers. I named the first parameter nFirst and the second nSecond . The multiply method returns an integer, and this integer will be the result of multiplying the first and second parameters. You can see this Web method ( marked with a WebMethod attribute) in the following code:

 [WebMethod]  public int Multiply(int nFirst, int nSecond)  {      return( nFirst * nSecond );  } 

One of the problems with writing Web Services is that there is no user interface ”this means you can't run them and see anything. Visual Studio.NET takes care of this for you, and gives you a way to actually invoke Web Services. By right-clicking on the default .asmx file you can run the Web Service in a browser. Before doing this, though, you must build it. What happens when the browser first runs is that it queries the Web Service and finds out what the parameters are. So when it examines my multiply Web Service it finds that the Web Service takes two parameters, both of them integers, and returns an integer. Visual Studio .NET actually creates a user interface for you in HTML so that you can type in the parameters and then invoke your Web Service. Figure 14.1 shows my Web Service running in a browser.

Figure 14.1. When you view a service's .asmx file in a browser, you see something like this.


When you invoke the service what you get back is XML. This XML is the result of invoking the Web Service. For instance, if my first integer were 5 and my second integer were 6, the resulting answer would be 30. Figure 14.2 shows this result; I entered 5 for the first integer and 6 for the second integer, and then clicked on the Invoke button.

Figure 14.2. Invoking a service from a browser displays the resulting XML.



Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233

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