Windows Systems Programming: Spring 2004

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


Simple Web Service and Client

Demonstrate building a simple web service and client using visual studio .net


Building the Service

  1. Start Visual Studio

  2. File/New/Project

  3. c# Projects/ASP.Net Web Service (give folder and name)

  4. In "Service1.asmx" code view add the web method

    [WebMethod (CacheDuration=10)]
    public string GetTime() {
       return DateTime.Now.ToShortTimeString ();
    }
  5. Build Solution

  6. Export to Server (eg sensor.cs.odu.edu)
    Set application
    Set web config to include
    <webServices>
    <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
    </protocols>
    </webServices>


Building a Windows Form Client

  1. Open Visual Studio .net

  2. File/New/Projects

  3. c#/Windows Applications

  4. Create a form with one label and one button to look like this

  5. Double Click on the button to show the event handler

  6. Project/Add Web Reference

  7. Put the URL of the previously defined TimeServer into the dialog box and let it find the reference

  8. Add Reference

  9. Add the following code to the button click handler

    private void button1_Click(object sender, System.EventArgs e){
       edu.odu.cs.sensor.Service1 service = new edu.odu.cs.sensor.Service1();
        label1.Text = service.GetTime();
    }

Creating a Web Form Client

  1. Start Visual Studio .net

  2. File/New/Project

  3. ASP.NET Web Application

  4. Project/Add Web Reference (as above)

  5. Add the following code in the PageLoad event handler

    private void Page_Load(object sender, System.EventArgs e)
    {
    	// Put user code to initialize the page here
    	edu.odu.cs.sensor.Service1 service = new edu.odu.cs.sensor.Service1();
    	Label2.Text = "The time is now is " +  service.GetTime();
    }
     
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 22, 2004.