Easy .Net Web Service

Create a hello world web service

  • Create a directory: c:\HelloServices
  • Create an asmx file: HelloService.asmx
  • <%@ WebService Language="C#" 
      Class="HelloService"%>
     
    using System;
    using System.Web.Services;
    using System.Xml.Serialization;
     
    [WebService(
      Namespace="http://localhost/HelloServices/")]
    public class HelloService : WebService
    {
        [WebMethod]
        public string SayHello()
        {
            return "Hello World!";
        }
    }
  • Create an IIS virtual directory: HelloServices and point to c:\HelloServices
  • Point browser to http://localhost/HelloServices/HelloService.asmx
  • Create an aspx web page to consume HelloService

  • Create a directory: c:\HelloServiceWeb
  • Create a subdirectory c:\HelloServiceWeb\bin
  • Generate HelloService proxy
    • wsdl http://localhost/HelloServices/HelloService.asmx
    • csc /t:library HelloService.cs
  • Copy generated HelloService.dll to bin directory
  • Create an aspx page: HelloServiceWeb.aspx
  • <%@ Page Language="C#" %>
    <script runat=server>
      void runService_Click(
        Object sender, EventArgs e)
      {
        HelloService hs = new HelloService();
        Label1.Text = hs.SayHello();
      }
    </script>
    <html>
      <body>
        <form id="Form1" runat=server>
          <asp:Label id="Label1" 
            runat="server">Label</asp:Label>
          <br />
          <asp:Button id="runService" 
            onclick="runService_Click"
            runat="server" 
            Text="Hello"/>
        </form>
      </body>
    </html>
  • Create an IIS virtual directory: HelloServiceWeb and point to c:\HelloServiceWeb
  • Point browser to http://localhost/HelloServiceWeb/HelloServiceWeb.aspx
  • This entry was posted in c#. Bookmark the permalink.