Simple C Web Service and Visual Studio.NET


Creating Web Services with the .NET Framework SDK

If Visual Studio.NET is unavailable to you, you can easily download the .NET Framework SDK from Microsoft. This provides you with command line compilers for C# and Visual Basic, along with all the libraries to create ASP.NET pages and Web Services.

You can take the code from the previous example, put it in a directory under your Web root, and name the file with an “.asmx” extension on the end. Then, by simply pointing a browser to the Web Service, the code compiles.

There is one minor modification to be made to the code that involves adding the following line to the beginning of the page.

   <%@ WebService Language="C#"  %>

This tag indicates to ASP.NET that this is a Web Service written in C#. Just to ensure that you have the entire picture of how Web Service codes need to look, the following is the entire code listing with the ASP.NET tag included.

    <%@ WebService Language="C#"  %>     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;                   }             }           }     }

When Visual Studio.NET generates a C# Web Service, it separates the C# code from the ASP.NET page with an additional attribute in the WebService tag. Consider the following example.

    <%@ WebService      Language="c#"      Codebehind="Service1.asmx.cs"       %>

The Codebehind attribute imports the C# code into the ASP.NET page. This is all the code that’s needed for ASP.NET.

Documenting the Service

Microsoft added features to allow you to provide documentation to users who browse with Internet Explorer. The WebService and WebMethod elements in the C# code allow you to customize the output of the browser and set a namespace that is unique for your project.

Note

The namespace is just a unique identifier for a particular project. By setting it to something useful to you, you eliminate the message from Microsoft that would have told you to change the namespace. You also make the service unique to your project.

The WebService attribute provides a method of creating a description and a unique namespace by simply adding the following code about the class definition.

    [WebService(Description="Web Services for XPlatform Book",               Namespace="http://www.advocatemedia.com/")]

The WebMethod attribute, which defines a method as being part of a Web Service, also allows you to provide extra information to the browser simply by adding the following code.

  [WebMethod(Description="Simple Test Method. Submit C else get -1")]

By adding this extra information you provide the user a better view to what the service does. Figure 6.8 shows how the browser displays this information.

click to expand
Figure 6.8: How Internet Explorer displays the description information. Also notice that the warning from Microsoft that you need to change the namespace is no longer present.

Changing the name of the class to XPlatformServices makes the Web Service slightly more descriptive and this ends up in the Web page as well.

The following complete code example shows the WebService and WebMethod tags in the proper positions.

    using System;     using System.Collections;     using System.ComponentModel;     using System.Data;     using System.Diagnostics;     using System.Web;     using System.Web.Services;     namespace StockQuote     {       /// <summary>       /// Web Services for XPlatform Web Services Book       /// </summary>       [WebService(Description="Web Services for XPlatform Book",                   Namespace="http://www.advocatemedia.com/")]       public class XPlatformServices: System.Web.Services.WebService       {             public XPlatformServices()             {                   //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(Description="Simple Test Method. Submit C else                 get -1")]             public double GetTestQuote(string symbol)             {                   double stockValue = 55.95;                   double empty = -1;                   if (symbol == "C")                   {                         return stockValue;                   }                   else                   {                         return empty;                   }             }       } }




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