.NET Web Services Tutorial
Posted by sidikalang on November 9, 2008
NET Web Services Tutorial
Environment: .NET 1.0.3705, IIS 5.0
Introduction
Visual Studio .NET makes .NET programming simple and accelerates the development process. It hides a lot of repetitive and configuration details from the user and improves productivity. However, sometimes you would like to program for .NET without using VS .NET; for example, you want to learn .NET framework programming and do not have access to VS .NET or you want to know what is actually going on under the hood.
(continued)
Writing a Web Service in .NET using VS .NET very is easy. However, it is possible to write Web Services using the plain .NET SDK. I struggled a lot to write Web Services without VS .NET and tried to find help on the Net. There are a lot of examples available for writing Web Services using VS.NET, but you will rarely find any examples of writing Web Services using only the .NET SDK. This article is exactly for this purpose. It looks at Web Services development using the .NET SDK alone.
Writing a Web Service
FirstService.asmx <%@ WebService language="C" class="FirstService" %> using System; using System.Web.Services; using System.Xml.Serialization; [WebService(Namespace="http://localhost/MyWebServices/")] public class FirstService : WebService { [WebMethod] public int Add(int a, int b) { return a + b; } [WebMethod] public String SayHello() { return "Hello World"; } }
- Open Start->Settings->Control Panel->Administrative tools->Internet Services Manager.
- Expand and right-click on [Default Web Site]; select New ->Virtual Directory.
- The Virtual Directory Creation Wizard opens. Click Next.
- The “Virtual Directory Alias” screen opens. Type the virtual directory name—for example, MyWebServices—and click Next.
- The “Web Site Content Directory” screen opens. Here, enter the directory path name for the virtual directory—for example, c:\MyWebServices—and click Next.
- The “Access Permission” screen opens. Change the settings as per your requirements. Let’s keep the default settings for this exercise. Click the Next button. It completes the IIS configuration. Click Finish to complete the configuration.
Testing the Web Service
Web-Based Service Consumer
WebApp.axpx <%@ Page Language="C#" %> <script runat="server"> void runSrvice_Click(Object sender, EventArgs e) { FirstService mySvc = new FirstService(); Label1.Text = mySvc.SayHello(); Label2.Text = mySvc.Add(Int32.Parse(txtNum1.Text), Int32.Parse(txtNum2.Text)).ToString(); } </script> <html> <head> </head> <body> <form runat="server"> <p> <em>First Number to Add </em>: <asp:TextBox id="txtNum1" runat="server" Width="43px">4</asp:TextBox> </p> <p> <em>Second Number To Add </em>: <asp:TextBox id="txtNum2" runat="server" Width="44px">5</asp:TextBox> </p> <p> <strong><u>Web Service Result -</u></strong> </p> <p> <em>Hello world Service</em> : <asp:Label id="Label1" runat="server" Font-Underline="True">Label</asp:Label> </p> <p> <em>Add Service</em> : & <asp:Label id="Label2" runat="server" Font-Underline="True">Label</asp:Label> </p> <p align="left"> <asp:Button id="runSrvice" onclick="runSrvice_Click" runat="server" Text="Execute"></asp:Button> </p> </form> </body> </html>
- Create a proxy for the Web Service to be consumed. The proxy is created using the wsdl utility supplied with the .NET SDK. This utility extracts information from the Web Service and creates a proxy. Thus, the proxy created is valid only for a particular Web Service. If you need to consume other Web Services, you need to create a proxy for this service as well. VS .NET creates a proxy automatically for you when the reference for the Web Service is added. Create a proxy for the Web Service using the wsdl utility supplied with the .NET SDK. It will create FirstSevice.cs in the current directory. We need to compile it to create FirstService.dll (proxy) for the Web Service.
c:> WSDL http://localhost/MyWebServices/ FirstService.asmx?WSDL c:> csc /t:library FirstService.cs
- Put the compiled proxy in the bin directory of the virtual directory of the Web Service (c:\MyWebServices\bin). IIS looks for the proxy in this directory.
- Create the service consumer, which we have already done. Note that I have instantiated an object of the Web Service proxy in the consumer. This proxy takes care of interacting with the service.
- Type the URL of the consumer in IE to test it (for example, http://localhost/MyWebServices/WebApp.aspx).
Windows Application-Based Web Service Consumer
WinApp.cs using System; using System.IO; namespace SvcConsumer{ class SvcEater { public static void Main(String[] args) { FirstService mySvc = new FirstService(); Console.WriteLine("Calling Hello World Service: " + mySvc.SayHello()); Console.WriteLine("Calling Add(2, 3) Service: " + mySvc.Add(2, 3).ToString()); } } }
References :http://www.codeguru.com/csharp/csharp/cs_webservices/tutorials/article.php/c5477