Building a Web Service
To illustrate the techniques used to implement a web service in C# using the services classes of the .NET Framework, let’s build a simple calculator and then make use of its functions over the Web.
You begin by specifying the web service. You do so by defining a
class that inherits from
System.Web.Services.WebService. The easiest way to
create this class is to open Visual Studio and create a new C# Web
Service project. The default name that Visual Studio provides is
WebService1, but you might want to choose
something more appropriate.
Visual Studio .NET creates a skeleton web service and even provides a Web Service example method for you to replace with your own code, as shown in Example 16-1.
Example 16-1. Skeleton web class generated by Visual Studio .NET
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services; namespace WSCalc { /// <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 /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent( ) { } #endregion /// <summary> /// Clean up any resources being used. /// </summary> ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access