Consume Horoscope Web Service in VS.NET
Add CommentIntroduction
I was recently browsing some newsgroups and found that people were finding it very difficult to consume Web Services from the Visual Studio.NET IDE. So I sat down to write a tutorial to help them.
There are some considerations before that. Firstly, you have to have a Web Service already made and deployed. For the sake of this tutorial I am taking the example of the Horoscope Web Service which I have created in my previous example. Click here to read the previous article. To give you a brief introduction about the Horoscope Web Service, its an example I have created where the consumer sends in his "Zodiac sign" in string format to the Web Service. The Web Service returns the Horoscope prediction for the "Zodiac Sign" provided by the client.
Some Assumptions
1) The Horoscope Web Service is deployed on my Web Server called
"localhost" in a Virtual Directory called "horoservice",
but it really does not matter what is your Web Server name or the Virtual
Directory only remember to substitute your names with the ones given in this
example.
2) I am using a ASP.NET application to consume the Horoscope Web
Service, you can use a GUI Application too.
3) I am using Visual Studio.NET Beta1 (v7.0.9466) to create the Client
Application.
Lets Start!
1) Create a Web Application Project
Start VS.NET if you haven't yet and create a new Web
Application from "File -> New -> Projects -> Visual
C# Projects -> Web Application". So by default a
WebForm1.aspx will be created for you by VS.NET
2) Add a Web
Reference
This is the most important step to be able to consume your
Web Service from any other Application. Go to the "Solution
Explorer". If you can't see it then go to View menu "View
-> Solution Explorer" the short cut key for this is
"Ctrl + Alt + S". Once you are in the Solution
Explorer "right-click" the current Project and
select "Add Web Reference " or go to the Project
menu and select "Project -> Add Web Reference".

Figure 1: Project Menu in Visual Studio.NET
3) Web Reference
In the Web Reference dialog, you have the "Address"
textbox. Here enter the full address of the Web Service you want to
Consume. I used the string "http://localhost/horoservice/wshoro.asmx" . If
you have entered the right URL to the Web Service then the dialog will browse and
show the Web Service page. Now click the Add Reference
button on the dialog to add a reference to the Web Service you want to consume.
This will automatically create the Proxy class required to communicate
with the Web Service. You
can view this from the "Server Explorer" under the
"Web References" folder.

Figure 2: Solution Explorer Window
4) Create the Web
Form (consumer)
Now let's create the Web Form which will consume this Web
Service. To make the Form drop a TextBox (variable "zo"),
Button (variable "but") and a Label (variable
"result") on to the Web Form "WebForm1.aspx" as
shown below.
![]() |
Figure 3: Designer Window
5) Scripting the Button
Double - Click the button ('but' in our example) on the Designer form to wire-up the OnClick event for the Button.
VS.NET will attach a event-handler to the button and show the scripting form,
from where you can handle the OnClick code.
| public void
but_Click (object
sender, System.EventArgs e) { } |
6) Handling the OnClick code.
To get more info about the methods supported by the Web
Service view the "Class View" window. If you can't
see it then enable it from the view menu "View -> Class
View" or use the short-cut key "Ctrl - Shift -
C".
The Class View window gives the details about all the classes that you have
referenced or have written. We will use the "Class View" window to inspect the Web Service methods.
| Note: Comments in Red have been written by me. |
![]() |
Figure 4: Class View Window
From the above diagram it is clear that "localhost.DailyHoro" is the fully qualified name of the Horoscope Web Service proxy class. Also "GetHoro()" is the method that I will use to retrive the prediction from the Web Service. The code for the OnClick event will be.
| public void
but_Click (object
sender, System.EventArgs e) { localhost.DailyHoro dh = new localhost.DailyHoro(); result.Text+=dh.GetHoro(zo.Text); } |
7) Deploy The Web Application
Once you have done the above changes, its time to save your
project and deploy it. I guess if you have followed everything
properly then you should be able to consume your Web Service right
away ...



