Creating .NET Web Services


Simple C# Web Service and Visual Studio.NET

Open Visual Studio.NET. From the “File” menu, select “New Project.” On the lefthand side, select “Visual C# Projects” and on the righthand side select “ASP.NET Web Service.”

Next, select the location under your Web root in which you want the Web Service to appear. Give the server an appropriate name such as “SimpleStockQuote.” Figure 6.3 displays the project selection dialogue box.

click to expand
Figure 6.3: A demonstration of how to create a C# Web Service project in Visual Studio.NET.

Once you name the service, Visual Studio.NET brings you into design mode, which looks like Figure 6.4. Look for the link in the center of the screen to be brought into the code window.

click to expand
Figure 6.4: Design mode for creating Web Services in Visual Studio.

Once you are in the code window, there is a lot of code already generated for you. The following code sample shows this generated code.

The first things to notice in the sample are all the “using” statements. These are the namespaces (known as “includes” in C++ or “imports” in Java) that Microsoft uses to make the services possible. These namespaces are used by the code generated for you; you don’t really need to worry about them.

Next there’s the namespace definition for this service. Right now this code sample uses the default that Microsoft assigned, which is WebService1. You can ignore much of the rest of the code because it initializes and destroys the objects that work behind the scenes to make the Web Service possible.

Notice the hello world example Microsoft included in the code.

    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();             }             #region Component Designer generated code             //Required by the Web Services Designer             private IContainer components = null;             /// <summary>             /// Required method for Designer support - do not modify             /// the contents of this method with the code editor.             /// </summary>             private void InitializeComponent()             {             }             /// <summary>             /// Clean up any resources being used.             /// </summary>             protected override void Dispose( bool disposing )             {                   if(disposing && components != null)                   {                         components.Dispose();                   }                   base.Dispose(disposing);             }             #endregion             // WEB SERVICE EXAMPLE             // The HelloWorld() example service returns the string             //  Hello World             // To build, uncomment the following lines then save and             //build the project             // To test this Web Service, press F5             //[WebMethod]             //public string HelloWorld()             //{             //    return "Hello World";             //}           }     }

The stock quote example in this chapter really won’t look up stock quotes, but will just provide you with a value for one particular symbol and then a negative number for all other values. The examples in this chapter are simple and meant only to introduce you to the basic concepts.

To modify the service Microsoft creates for you, first change the namespace to something that is appropriate for your environment. In this example, the namespace chosen is StockQuote. Then skip down to where the code states [WebMethod]. You can comment out the code at that point and add the following code.

     [WebMethod]     public double GetTestQuote(string symbol)     {       double stockValue = 55.95;       double empty = -1;      if (symbol == "C")      {       return stockValue;      }      else      {       return empty;      }     }

Notice that in the preceding code example the only real difference between this code and the code example in Chapter 1 is the addition of [WebMethod] to the definition of the method. Other than that, the code is a standard C# method.

The code simply returns a numeric value or price for the symbol C and -1 for every other symbol passed to it.

After adding the last code snippet to your Web Services code, the entire Web Service code should now look like the following.

    using System;     using System.Collections;     using System.ComponentModel;     using System.Data;     using System.Diagnostics;     using System.Web;     using System.Web.Services;     //modified namespace --Xplatform book     namespace StockQuote     {       /// <summary>       /// The first C# Web Service       /// </summary>       public class Service1 : System.Web.Services.WebService       {             public Service1()             {                   //CODEGEN: This call is required by the ASP.NET Web                   //Services Designer                   InitializeComponent();             }             #region Component Designer generated code             //Required by the Web Services Designer             private IContainer components = null;             /// <summary>             /// Required method for Designer support - do not modify             /// the contents of this method with the code editor.             /// </summary>             private void InitializeComponent()             {             }             /// <summary>             /// Clean up any resources being used.             /// </summary>             protected override void Dispose( bool disposing )             {                   if(disposing && components != null)                   {                         components.Dispose();                   }                   base.Dispose(disposing);             }             #endregion             [WebMethod]             public double GetTestQuote(string symbol)             {                   double stockValue = 55.95;                   double empty = -1;                   if (symbol == "C")                   {                         return stockValue;                   }                   else                   {                         return empty;                   }             }           }     } 

Under “Debug” in Studio, click on “Start without Debugging,” which is symbolized with “!”. This executes the Web Service and opens a browser window that looks like Figure 6.5.

click to expand
Figure 6.5: The information made available to the browser from Microsoft’s .NET Web Services implementation.

The first link available is the link to the service description, which is the WSDL file. Any Microsoft Web Services that have the “asmx” extension simply need WSDL passed as the query string in order to see the description. In this case, this URL gives us the WSDL file: http://localhost/XPlatform/StockQuote/Service1.asmx?WSDL. Figure 6.6 shows the WSDL information in Internet Explorer.

click to expand
Figure 6.6: How the Web Service displays its WSDL information.

Note

Remember WSDL describes the Web Service so a consumer, such as a Web page or application, knows what methods and variables are available. This is used by .NET to create a proxy. Chapter 4 covers WSDL in detail.

After the link for WSDL, there is a link for the one method available in this service and that’s GetTestQuote. This link reveals methods of testing the service. Figure 6.7 displays how this output looks in the browser.

click to expand
Figure 6.7: Output in Internet Explorer for testing the GetTestQuote method.

The information on the method page shows you the SOAP requests and response, along with a dialogue to test the implementation. For the simple stock quote example, the request looks like the following:

    POST /XPlatform/StockQuote/Service1.asmx HTTP/1.1     Host: localhost     Content-Type: text/xml; charset=utf-8     Content-Length: length     SOAPAction: "http://tempuri.org/GetTestQuote"     <?xml version="1.0" encoding="utf-8"?>     <soap:Envelope      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns:xsd="http://www.w3.org/2001/XMLSchema"      xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">     <soap:Body>     <GetTestQuote xmlns="http://tempuri.org/">       <symbol>string</symbol>     </GetTestQuote>     </soap:Body>     </soap:Envelope>

Notice that in this example, the Web Service leaves out things like the actual length and the value being sent. Both of these values vary depending on the request you make.

Note that if you’re calling this Web Service from another language, such as Java, you could send the request to the address of the Web Service and get the following response back.

:     HTTP/1.1 200 OK     Content-Type: text/xml; charset=utf-8     Content-Length: length     <?xml version="1.0" encoding="utf-8"?>     <soap:Envelope      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns:xsd="http://www.w3.org/2001/XMLSchema"     xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">       <soap:Body>         <GetTestQuoteResponse xmlns="http://tempuri.org/">           <GetTestQuoteResult>double</GetTestQuoteResult>         </GetTestQuoteResponse>       </soap:Body>      </soap:Envelope>
Note

Remember that the SOAP standard describes how to encapsulate data in XML during transmission to a node along with the protocols, such as HTTP, that move the data to the appropriate node. For more information, refer back to Chapter 3.

If you enter the symbol “C” into the text box on this page, you’ll get the following response in Internet Explorer.

       <?xml version="1.0" encoding="utf-8" ?>     <double xmlns="http://tempuri.org/">55.95</double>

Note that tempura.org is the default namespace that Visual Studio.NET defines.




Cross-Platform Web Services Using C# and Java
Cross-Platform Web Services Using C# & JAVA (Charles River Media Internet & Web Design)
ISBN: 1584502622
EAN: 2147483647
Year: 2005
Pages: 128

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