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








Friday, September 14, 2012

easy way to remember the parts of wsdl file

Tags:
definations: Root WSDL element
types: What data types will be transmitted
message:What messages will be transmitted
portType: what operations(functions) will be supported
binding: how will the message be transmitted on the wire? what SOAP-specific details are there.
service: Where is the service located?

Thursday, September 13, 2012

change the cygwin terminal prompt

the prompt controlled by variable PS1,so put following code to .bash_profile or .bashrc sould works


function prompt {
local WHITE="\[\033[1;37m\]"
local GREEN="\[\033[0;32m\]"
local CYAN="\[\033[0;36m\]"
local GRAY="\[\033[0;37m\]"
local BLUE="\[\033[0;34m\]"
export PS1=" ${GREEN}\u${CYAN}@${BLUE}\h ${CYAN}\w${GRAY} $ "
}
prompt

see the sqlstatement been run in java code from ibatis

add following tag in log4j,all statement been run and their parameter,result column name and data will write into log file



       
       
     

Tuesday, September 11, 2012

ANT WINDOWS style path and POSIX style path in cygwin

while using ant in windows cygwin, we can not simplely pass a posix style path in ant script,because posix style path like "/cygdrive/c/test" or "/home/atc/" can not be recognized by ant,only recognized by cygwin shell.
so in order to use those posix style path in ant script, we need convert this string into a windows style path,then path to ant,utility cygpath will help to do this kind of converting.


<exec executable="cygpath" outputproperty="app.home.dir">
  <arg value="--windows"/>
  <arg value="/home/atc/chryslerds"/>
</exec>
<echo message="${app.home.dir}"/>

<exec executable="cygpath" outputproperty="ant.dir">
  <arg value="--windows"/>
  <arg value="/cygdrive/c/ant182"/>
</exec>

<echo message="${ant.dir}"/>