Consume Horoscope Web Service in VS.NET
Add Comment<span class="wboxheado">Introduction</span><br> 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.<br> 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. <a href="article.aspx?ArticleID=15&&TopicID=7" class="wbox">Click here</a> 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. <p ><span class="wboxheado">Some Assumptions</span><br> 1) The Horoscope Web Service is deployed on my Web Server called "localhost" in a Virtual Directory called "<b>horoservice</b>", 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. <br> 2) I am using a ASP.NET application to consume the Horoscope Web Service, you can use a GUI Application too.<br> 3) I am using Visual Studio.NET Beta1 (v7.0.9466)<b> </b>to create the Client Application.</p> <p ><span class="wboxheado">Lets Start!</span><br> <span class="wboxhead">1) Create a Web Application Project</span><br> 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</p> <p ><span class="wboxhead">2) Add a Web Reference</span><br> 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".</p> <p ><img border="0" src="../../img/conar1.jpg" width="220" height="380"><br> <b> Figure 1:</b> Project Menu in Visual Studio.NET</p> <p ><span class="wboxhead">3) Web Reference</span><br> 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 <b>Add Reference</b> button on the dialog to add a reference to the Web Service you want to consume. This will automatically create the <b>Proxy</b> class required to communicate with the Web Service. You can view this from the "Server Explorer" under the "Web References" folder.</p> <p ><img border="0" src="../../img/conar2.jpg" width="208" height="255"><br> <b>Figure 2:</b> Solution Explorer Window</p> <p ><span class="wboxhead">4) Create the Web Form (consumer)</span><br> 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.</p> <div align=center> <table border="1" cellspacing="0" cellpadding="0"> <tr> <td width="100%" class="wbox"><img border="0" src="../../img/conar3.jpg" width="448" height="145"></td> </tr> </table></div> <p align="center"><b>Figure 3:</b> Designer Window</p> <p><span class="wboxhead">5) Scripting the Button</span><br> 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.</p> <div align="center"> <table cellSpacing="0" cellPadding="0" border="1"> <tbody> <tr> <td width="100%"><span class=codetext>public void but_Click (object sender, System.EventArgs e)<br> {<br> }</span></td> </tr> </tbody> </table> </div> <p><span class="wboxhead">6) Handling the OnClick code.</span><br> 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".<br> 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.</p><div align=center> <table border="1" class="wbox" cellspacing="0" cellpadding="0"> <tr> <td width="100%"><b><i>Note: Comments in Red have been written by me.</i></b></td> </tr> <tr> <td width="100%"><img border="0" src="../../img/conar4.jpg" width="390" height="241"></td> </tr> </table></div> <p align=center><b>Figure 4:</b> Class View Window</p> <p>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.</p> <div align="center"> <table cellSpacing="0" cellPadding="0" border="1"> <tbody> <tr> <td width="100%"><span class=codetext>public void but_Click (object sender, System.EventArgs e)<br> {<br> localhost.DailyHoro dh = new localhost.DailyHoro();<br> result.Text+=dh.GetHoro(zo.Text);<br> }</span></td> </tr> </tbody> </table> </div> <p><span class="wboxhead">7) Deploy The Web Application</span><br> 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 ...</p>