Consuming a Web Service from a Win Form Application 来源: 时间:2002-09-13 11:09 作者:AMTeam.org Consuming a Web Service from a Win Form Application
Saurabh Nandu Level: Beginner/Intermediate Introduction In the previous .NET101 article Building a Stock-Quotes Web
Service, we saw how to build a Stock-Quotes Web Service and consume it from a
ASP.NET web page. In this article we will concentrate on consuming the
Stock-Quotes Web Service from a Win Form Application. Building a Win Form Application Create a new Project Start-up VS.NET and Start a new Project from File ->New ->Project ->Visual C# Projects->Windows Application. In the Name field for the Application enter StockQuoteConsume. The Design View of VS.NET changes as shown below. Figure 1 Adding Components Now drop a TextBox, a Button and a ListBox on the Win Form in Design view from the Toolbox as shown below. Figure 2 Adding a Web Reference To consume a Web Service from VS.NET you have to add a Web Reference to the Web Service. To add a Web Reference to our Stock-Quotes Web Service, go to the Project menu -> Add Web Reference. A dialog box which shows up is shown below. Enter the Location (URI) path to the Stock-Quotes Web Service in the Address textbox and click Enter. Once VS.NET discovers the Web Service click Add Reference to add a Web Reference to the Stock-Quotes to our Project. While adding Web Reference, VS.NET automatically generates all the necessary plumbing code like Proxy class generation etc required to consume the Web Service Figure 3
Figure 4 Wire-Up the Button Event-Handler Double-Click on the button on the Win Form in design window to write the event handling code for the button. The event handling code for the button is as under. protected void button1_Click (object sender, System.EventArgs e)
{ //Create a Instance of the Autogenerated Proxy
class //Call the WebMethod GetQuote and
pass // The returned string has values which are separated
// Check if the string array returned has more than
15 Compile the Project Figure 5 |
关键词:
|
相关文章 |