[ Home | Syllabus | Course Notes | Assignments | Search]
Demonstrate building a simple web service and client using visual studio .net
Start Visual Studio
File/New/Project
c# Projects/ASP.Net Web Service (give folder and name)
In "Service1.asmx" code view add the web method
[WebMethod (CacheDuration=10)] public string GetTime() { return DateTime.Now.ToShortTimeString (); }
Build Solution
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>
Open Visual Studio .net
File/New/Projects
c#/Windows Applications
Create a form with one label and one button to look like
this
Double Click on the button to show the event handler
Project/Add Web Reference
Put the URL of the previously defined TimeServer into the dialog box and let it find the reference
Add Reference
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(); }
Start Visual Studio .net
File/New/Project
ASP.NET Web Application
Project/Add Web Reference (as above)
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.