Friday, September 21, 2012

Web Service in Windows .Net

I followed a Shiv Pal Singh's post in CodeGuru that create a web service with .net in windows without a studio tools,here is the steps and content that recorded to help me remember.

1) install the IIS from windows 7,after installation the server is up,default page(actually is a html with one picture 

embedded with lot of links) can be see in browser.

2) add a default html in server's newly created virtual directory can also be see in browser,but the asmx web service file 

can not be see in browser because of the script handler is not properly registered in system

go to c:\windows\Microsoft.NET\Framework64\v.2.0.50727,run following command

ASPNET_REGIIS.exe -I 

remember this is 64 bit,not 32,running 32 in 64 will get a error

then stop and start the server:

net stop w3svc

net start w3svc

3) copy following content in to virtual directory,name is as FirstService.asmx, language is CS,not C


 <%@ WebService language="CS" class="FirstService" %>

  using System;
  using System.Web.Services;
  using System.Xml.Serialization;

  [WebService(Namespace="http://localhost/WebService/")]
  public class FirstService : WebService
  {
      [WebMethod]
      public int Add(int a, int b)
      {
          return a + b;
      }

      [WebMethod]
      public String SayHello()
      {
          return "Hello World";
      }
  }


we will get list of operations,use link can test the service,url http://localhost/WebService/FirstService.asmx?WSDL can 

return the wsdl file of the web service

now web service serverside is done

following step will test web service from local or another machine by web and standalone windows app

1) create following asp.net web page to test the web service,name it  as WebApp.aspx



<%@ 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>



This client is some code running from server to test web service,then display result in client page

2) standalone windows app,net point to a server that web service stay,we need use WSDL file from server to create a 

dll, link this dll with client code will make client test code connect with server and successfully test the operation


use following step to create DLL

WSDL http://localhost/WebService/FirstService.asmx?WSDL

this will download the server's WSDl file and create a agent file(C#) called FirstService.cs,use following command to 

compile the source code to a DLL,then put this dll in virtual dir\bin folder,IIS looking for this agent at run time.

csc /t:library FirstService.cs

create source code of the 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());
        }
 }
}

using following to compile the client

csc /r:FirstService.dll WinApp.cs

exe file created,it will test our  web service created in previous steps








No comments:

Post a Comment