Master C# Logo banner
Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 


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&nbsp; 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&amp;&amp;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 &quot;Zodiac sign&quot; in string format to the Web Service. The Web Service returns the Horoscope prediction for the &quot;Zodiac Sign&quot; provided by the client. <p ><span class="wboxheado">Some Assumptions</span><br> 1) The Horoscope Web Service is deployed on my Web Server called &quot;localhost&quot; in a Virtual Directory called &quot;<b>horoservice</b>&quot;, 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 &quot;File -&gt; New -&gt; Projects -&gt; Visual C# Projects -&gt; Web Application&quot;. 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 &quot;Solution Explorer&quot;. If you can't see it then go to View menu &quot;View -&gt; Solution Explorer&quot; the short cut key for this is &quot;Ctrl&nbsp; + Alt + S&quot;. Once you are in the Solution Explorer &quot;right-click&quot; the current Project and select &quot;Add Web Reference &quot; or go to the Project menu and select &quot;Project -&gt; Add Web Reference&quot;.</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 &quot;Address&quot; textbox. Here enter the full address of the Web Service you want to Consume. I used the string &quot;http://localhost/horoservice/wshoro.asmx&quot; . 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 &quot;Server Explorer&quot; under the &quot;Web References&quot; 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 &quot;zo&quot;), Button (variable &quot;but&quot;) and a Label (variable &quot;result&quot;) on to the Web Form &quot;WebForm1.aspx&quot; 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 &quot;Class View&quot; window. If you can't see it then enable it from the view menu &quot;View -&gt; Class View&quot; or use the short-cut key &quot;Ctrl - Shift - C&quot;.<br> The Class View window gives the details about all the classes that you have referenced or have written. We will use the &quot;Class View&quot; 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 &quot;localhost.DailyHoro&quot; is the fully qualified name of the Horoscope Web Service proxy class. Also &quot;GetHoro()&quot; 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>

Comments

Add Comment