Windows Systems Programming: Spring 2004

[ Home | Syllabus | Course Notes | Assignments | Search]


Simple Web Service and Client - Built without Visual Studio

Demonstrate building a simple web service and client using just a text editor and command prompt


Building the Service

  1. Start .NET command prompt window

  2. With your favorite editor create "TimeServiceByHand.asmx file containing

    <%@ WebService Language="c#"  Class="TimeServiceByHand" %>
    using System;
    using System.Web.Services;
    
    public class TimeServiceByHand : System.Web.Services.WebService
    {
    	public TimeServiceByHand()
    	{
    		
    	}
    	[WebMethod (CacheDuration=10)]
    	public string GetTime()
    	{
    		return DateTime.Now.ToShortTimeString ();
    	}
    
    }
    
  3. Define a web.config file to contain at least

    <configuration>
        <system.web>
        <webServices>
            <protocols>
                <add name="HttpGet"/>
                <add name="HttpPost"/>
            </protocols>
        </webServices>
        </system.web>
    </configuration>
  4. Export both files to Server (eg sensor.cs.odu.edu) using ftp
    I installed mine in http://sensor.cs.odu.edu:8000/cwild/Examples/TimeServerByHand/TimeServiceByHand.asmx 
    NOTE: does not need to be an application


Building a Windows Form Client

  1. Using your favorite text editor create a Windows Form source file

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    public class TimeForm : System.Windows.Forms.Form
    {
    	private System.Windows.Forms.Label theTime;
    	private System.Windows.Forms.Button getTime;
    	public TimeForm()
    	{
    			this.theTime = new System.Windows.Forms.Label();
    			this.getTime = new System.Windows.Forms.Button();
    			// 
    			// theTime
    			// 
    			this.theTime.Location = new System.Drawing.Point(48, 24);
    			this.theTime.Name = "theTime";
    			this.theTime.Size = new System.Drawing.Size(128, 48);
    			this.theTime.TabIndex = 0;
    			this.theTime.Text = "The time goes here";
    			// 
    			// getTime
    			// 
    			this.getTime.Location = new System.Drawing.Point(88, 96);
    			this.getTime.Name = "getTime";
    			this.getTime.Size = new System.Drawing.Size(112, 72);
    			this.getTime.TabIndex = 1;
    			this.getTime.Text = "GetCurrentTime";
    			this.getTime.Click += new System.EventHandler(this.getTime_Click);
    			// 
    			// Form1
    			// 
    			this.ClientSize = new System.Drawing.Size(292, 266);
    			this.Controls.Add(this.getTime);
    			this.Controls.Add(this.theTime);
    			this.Name = "Get Time";
    			this.Text = "Get Current Time";
    
    	}
    
    	static void Main() 
    	{
    		Application.Run(new TimeForm());
    	}
    
    	private void getTime_Click(object sender, System.EventArgs e)
    	{
    		TimeServiceByHand service = new TimeServiceByHand();
    		theTime.Text = service.GetTime();
    	}
    }
    
  2. Create the proxy class using the wsdl command in the command prompt window (I have left the username and password parts blanks)

    wsdl /username: /password: http://sensor.cs.odu.edu:8000/Examples/TimeServerByHand/TimeServiceByHand.asmx
  3. Compile the form and proxy

    csc TimeForm.cs TimeServiceByHand.cs
  4. Run the form "TimeForm"

  5. NOTE: I put the service in a unsecure part of the web server. Need to figure out permissions issue.


Creating a Web Form Client

  1. Using your favorite text editor, create two files, the aspx file

    <%@ Page language="c#" Inherits="TimeClient" %>
    <HTML>
    	<HEAD>
    		<title>Time Client</title>
    	</HEAD>
    	<body>
    		<form id="Form1" method="post" runat="server">
    			<asp:Label id="Label1" runat="server">Web Time Client</asp:Label>
    			<br/>
    			<asp:Label id="Label2" runat="server"></asp:Label>
    		</form>
    	</body>
    </HTML>
    
    
  2. Also the source file 

    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    
    public class TimeClient : System.Web.UI.Page
    {
    	protected System.Web.UI.WebControls.Label Label2;
    	protected System.Web.UI.WebControls.Label Label1;
    
    	private void Page_Load(object sender, System.EventArgs e)
    	{
    		TimeServiceByHand service = new TimeServiceByHand();
    		Label2.Text = "The time is now is " +  service.GetTime();
    	}
    
    }
  3. Create the proxy class using the wsdl command in the command prompt window (I have left the username and password parts blanks)

    wsdl /username: /password: http://sensor.cs.odu.edu:8000/Examples/TimeServerByHand/TimeServiceByHand.asmx
  4. Compile the form and proxy and place the DLL in the "bin" directory

    csc  /t:library TimeServiceByHand.cs TimeClient.aspx.cs
  5. Move aspx page and "TimeServiceByHand.dll" to web server

Notice the architecture in this last example

Client Web Browser (computer 1 - front end) calls aspx page (computer 2- middle tier) which calls asmx web service (computer 3 - back end service)

 

The folders for these examples are here.

 

 

 


Copyright chris wild 1999-2004.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: March 29, 2004.