Creating a Simple Hello World Service


Creating a Simple "Hello World" Service

To see how web services work in action, this section walks you through creating the simplest web service you can create: the "Hello World" service.

Start Visual Studio and select File, then New, and then Website. Choose the ASP.NET Web Service template from the menu and call it HelloWorldService.

The newly created project contains a C# class file called Service.cs in the App_Code directory and a Service.asmx file in the web application's root directory.

The Service.asmx file is the main point of interface for a web service. It can be used to obtain the WSDL contract for the web service as well as to test the service. Highlight the Service.asmx file and run the application.

When you run the application in debug mode for the first time, you might be prompted to add a Web.config file to the project and enable debugging mode. You will see a new web browser containing a list of the services available within the application. Click the HelloWorld link to bring up a web page that looks similar to the one shown in Figure 32.2.

Figure 32.2. The HelloWorld web service page.


Click the Invoke button to test-run the web service. This web service simply returns the phrase "Hello World" to any application invoking the HelloWorld() method. The results of testing this web service are shown in the following lines:

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


The output from the web service is in an XML format that can be interpreted by any client aware of the Web Services XML format. To see how .NET, and specifically Visual Studio 2005, handles web services, you need to create a web service client.

To do this, add a new console application project to the existing solution called WSClient. Right-click the new console application project and select Add Web Reference. You will be prompted with a dialog asking for the location of the web service. In this case, the web service is part of the current solution, so you can click the "Web Services in This Solution" link. Then click the displayed link to add a web reference to the "Hello World" web service. Accept localhost as the web service name and click Add Reference.

With the web reference in place, you can modify the code in Program.cs to create an instance of the web service and invoke it, as shown in the following example:

using System; using System.Collections.Generic; using System.Text; namespace WSClient { class Program { static void Main(string[] args) {     localhost.Service svc = new localhost.Service();     Console.WriteLine(svc.HelloWorld()); } } } 


When you run this application, you will see the phrase "Hello World." This phrase comes from the web service in the solution. When the client represented by the localhost.Service wrapper class invokes the HelloWorld() method, the code contained in the Service.cs file on the server (shown in Listing 32.1) is executed.

Listing 32.1. Service.cs

using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService { public Service () {     //Uncomment the following line if using designed components     //InitializeComponent(); } [WebMethod] public string HelloWorld() {     return "Hello World"; } } 

The WebMethodAttribute code attribute class is what marks a method as being exposed to clients via the web service. The WebServiceAttribute class allows you to define additional properties of the web service directly within the code.

Despite how often the "Hello World" sample is used to illustrate the use of new technology, it isn't very practical. The next sample shows you how to create a web service that takes input parameters and returns values both as a method return value and through output parameters.

First, add a new web method to the Service.cs file as shown in Listing 32.2.

Listing 32.2. Service.cs Containing an Additional Web Method

using System; using System.Text; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService { public Service () {     //Uncomment the following line if using designed components     //InitializeComponent(); } [WebMethod] public string HelloWorld() {     return "Hello World"; } [WebMethod(Description = "Concatenates two strings, returning the result and length.")] public int StringConcat(string string1, string string2,     out string stringTotal) {     StringBuilder sb = new StringBuilder();     sb.Append(string1);     sb.Append(string2);     stringTotal = sb.ToString();     return stringTotal.Length; } } 

The new method takes two input parameters and supplies the concatenation of those two parameters as the third parameter and then returns the length of the new string as the return value.

If you have worked with web services in previous versions of Visual Studio, you will be pleasantly surprised to see that just a few seconds after you add the new method to the web service, the web reference in the console application you created is automatically updated. If it doesn't automatically update, you can just right-click the localhost item in the Solution Explorer and choose Update Web Reference.

Run the web application again and get to the test page for the StringConcat method. Note that you can't test-fire this method from the web any more because of the output parameter.

To see this new method in action, modify the source code to the Program.cs file in the console application to look like the code in Listing 32.3.

Listing 32.3. Consuming a Web Service Method with Output Parameters

using System; using System.Collections.Generic; using System.Text; namespace WSClient { class Program { static void Main(string[] args) {     localhost.Service svc = new localhost.Service();     Console.WriteLine(svc.HelloWorld());     string conCatResult = string.Empty;     int conCatLength = svc.StringConcat("The quick brown ",         "fox ran over the slow 486 DX2/66", out conCatResult);     Console.WriteLine(string.Format(         "Concat Result: {0} length {1}", conCatResult, conCatLength));     Console.ReadLine(); } } } 

The complexity of converting the request for method execution into a portable XML format, transmitting that XML to the remote host, waiting for a response, retrieving the response XML, and finally decoding the response XML into .NET native types is all handled automatically by the Web Service wrapper class. The output of the preceding code is as follows:

Hello World Concat Result: The quick brown fox ran over the slow 486 DX2/66 length 48 


This book will not spend much time on the details of WSDL, but it does help to see how everything works, including the WSDL describing the service you just created. To see the WSDL for any .NET service, simply append ?WSDL to the end of the URL. The WSDL for the service created in this section is shown in Listing 32.4.

Listing 32.4. WSDL for a Two-Method Web Service

<?xml version="1.0" encoding="utf-8"?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <wsdl:types> <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">     <s:element name="HelloWorld">       <s:complexType />     </s:element>     <s:element name="HelloWorldResponse">       <s:complexType>       <s:sequence>         <s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />       </s:sequence>     </s:complexType>   </s:element>   <s:element name="StringConcat">     <s:complexType>       <s:sequence>         <s:element minOccurs="0" maxOccurs="1" name="string1" type="s:string" />         <s:element minOccurs="0" maxOccurs="1" name="string2" type="s:string" />       </s:sequence>     </s:complexType>   </s:element>   <s:element name="StringConcatResponse">     <s:complexType>       <s:sequence>         <s:element minOccurs="1" maxOccurs="1" name="StringConcatResult" type="s:int" />         <s:element minOccurs="0" maxOccurs="1" name="stringTotal" type="s:string" />       </s:sequence>     </s:complexType>   </s:element> </s:schema> </wsdl:types> <wsdl:message name="HelloWorldSoapIn"> <wsdl:part name="parameters" element="tns:HelloWorld" /> </wsdl:message> <wsdl:message name="HelloWorldSoapOut"> <wsdl:part name="parameters" element="tns:HelloWorldResponse" /> </wsdl:message> <wsdl:message name="StringConcatSoapIn"> <wsdl:part name="parameters" element="tns:StringConcat" /> </wsdl:message> <wsdl:message name="StringConcatSoapOut"> <wsdl:part name="parameters" element="tns:StringConcatResponse" /> </wsdl:message> <wsdl:portType name="ServiceSoap"> <wsdl:operation name="HelloWorld">   <wsdl:input message="tns:HelloWorldSoapIn" />   <wsdl:output message="tns:HelloWorldSoapOut" /> </wsdl:operation> <wsdl:operation name="StringConcat">   <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> Concatenates two strings, returning the result and length.</wsdl:documentation>   <wsdl:input message="tns:StringConcatSoapIn" />   <wsdl:output message="tns:StringConcatSoapOut" /> </wsdl:operation> </wsdl:portType> <wsdl:binding name="ServiceSoap" type="tns:ServiceSoap"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="HelloWorld">   <soap:operation soapAction="http://tempuri.org/HelloWorld" style="document" />   <wsdl:input>     <soap:body use="literal" />   </wsdl:input>   <wsdl:output>     <soap:body use="literal" />   </wsdl:output> </wsdl:operation> <wsdl:operation name="StringConcat">   <soap:operation soapAction="http://tempuri.org/StringConcat" style="document" />   <wsdl:input>     <soap:body use="literal" />   </wsdl:input>   <wsdl:output>     <soap:body use="literal" />   </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:binding name="ServiceSoap12" type="tns:ServiceSoap"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="HelloWorld">   <soap12:operation soapAction="http://tempuri.org/HelloWorld" style="document" />   <wsdl:input>     <soap12:body use="literal" />   </wsdl:input>   <wsdl:output>     <soap12:body use="literal" />   </wsdl:output> </wsdl:operation> <wsdl:operation name="StringConcat">   <soap12:operation soapAction="http://tempuri.org/StringConcat" style="document" />   <wsdl:input>     <soap12:body use="literal" />   </wsdl:input>   <wsdl:output>     <soap12:body use="literal" />   </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="Service"> <wsdl:port name="ServiceSoap" binding="tns:ServiceSoap">   <soap:address location="http://localhost:3179/HelloWorldService/Service.asmx" /> </wsdl:port> <wsdl:port name="ServiceSoap12" binding="tns:ServiceSoap12">   <soap12:address location="http://localhost:3179/HelloWorldService/Service.asmx" /> </wsdl:port> </wsdl:service> </wsdl:definitions> 

There is a lot going on in the preceding WSDL document. The basic idea is that operations (remote method invocations) are defined and associated with specific ports (URLs). For each operation, there are elements defined as input and elements defined as output. By consuming this WSDL document, a web service client is able to format messages sent to the service and interpret response messages from the service.



Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

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