A Hello World Web Service Tutorial

A Hello World Web Service

This is the ten-minute web service tutorial. No parameters are sent and nothing interesting happens, but you can get a taste of how easy XML/SOAP web services are with .Net.
1. Choose File | New Project, then choose Visual C# Projects as the type and ASP.Net Web Service as the template. Before clicking “”OK””, change the location of the project to a directory named “”HelloService”” within the localhost web directory.
Note that “”localhost”” corresponds to the directory “”c:\inetpub\wwwroot”” on most machines, so you can always find your project in a subdirectory of that, e.g., “”c:\inetpub\wwwroot\HelloService””
2. The Solution View should be selected in the right panel of the development environment. Right click on Service1.asmx to open the file containing the code. There is a sample helloworld method within comments. Just uncomment this method including the “”[Web Method] directive above it. Then select Debug | Start Without Debugging.
3. The service will execute in a browser with the URLhttp://localhost/HelloService/Service1.asmx. Note that the page that appears is just a test page that .Net provides– you have only created a distributed method, so there’s really no gui yet. The first test page lists all the web methods of the class. Choose the “”HelloWorld”” method which causes http://localhost/HelloService/Service1.asmx?op=HelloWorld to run. This is a page that asks if you’d like to invoke the service “”Hello World””. Click on “”Invoke”” and the actual web service gets run: http://localhost/WebService1/Service1.asmx/HelloWorld. This causes some xml to appear with “”Hello World”” in it.
You can look at the XML/SOAP at the various stages, but it is not really neccessary. .Net, like other development environments, provides tools that allow programmers to always talk in a high-level language, and never deal with XML. In the next section, you’ll provide the url of this web service you just created to .Net, and it will create a C# client for you that calls the service. You could also use a Java IDE to create the client and things would work fine as well. Web services do provide a multi-language and platform environment.
A HelloWorld Client
So now let’s create a client application that calls your HelloService over the net. We’ll first run the client on the same machine as the service, and then we’ll have you switch computers and run it remotely.
4. Choose File | New Project, Visual C# Applications as the type, and ASP.Net Web Application as the template. Before clicking “”OK””, change the location of the project to a directory named “”HelloClient”” within the localhost web directory.
5. Now let’s have .Net generate code that makes the distributed call to our HelloService web service. In the Solution Panel, right click your project (e.g., HelloClient) and choose Add Web Reference. Enter http://localhost/HelloService.Service1.asmx as the URL (alternatively, you can choose to browse for services on the local machine). The system will provide a default name of localhost to the web reference. Change this name to “”HelloServiceWrapper”” and click Add Reference.
6. In the Solution Explorer panel, rename the file WebForm.aspx to HelloForm.aspx. Double click on this file to open it. If the toolbox of UI components doesn’t appear, choose View | Toolbox. Then add a button and a label from the web forms section of the toolbox to the UI. Using the properties dialog, change the id of the button to submitButton and the id of the label to resultLabel (this is good practice as it makes the resulting generated code easier to read).
7. Double click the button to view its event-handler which should be called “”SubmitButton_Click””. Enter the following code in the function:
HelloServiceWrapper.Service1 helloService = new HelloServiceWrapper.Service1();
string s = helloHost.HelloWorld();
resultLabel.Text=s;
8. Select Debug | Start Without Debugging. Clicking on the button should cause “”Hello World”” to appear.
9. Now let’s try it in a distributed manner. Ask another student to enter a browser and go to the URL of your HelloClient. For instance, if your computer name is “”hrn23512.cs.usfca.edu””, the student should enter the url:
hrn23512.cs.usfca.edu/HelloClient/WebForm1.aspx
at the browser prompt. You should be able to click the button and receive the same reply as before.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s