The following example illustrates consuming the QuoteService via a small command line program.
Add a new subproject to the solution. Make the new project a console application by selecting the Console Application template. Name the project ConsumeWebService.
Highlight the ConsumeWebService project in the solution explorer and right-click. Select Add Web Reference from the local menu.
Click on the Web services in this solution link:
Select the QuoteService link. You should see all the available methods. Click the Add Reference button. Visual Studio will generate a proxy for you.
Another way to generate a proxy is to surf to the Web service, ask for the WSDL manually, and the run the WSDL code through a utility named WSDL.exe.
For example, the following query string fetches the WSDL:
http://localhost/WebServiceORama/QuoteService.ASMX?wsdl
Save the document sent back by the service and run it through the WSDL command line utility:
C:\>WSDL quoteservice.wsdl
This will generate a C Sharp file you may use in your application. (WSDL has command line parameters for generated proxies in VB, too.)
The namespace in which the new proxy lives reflects the origin of the Web service. Because this proxy came from a service on this machine, the namespace is localhost. To access types from the new service proxy, you must preface the types with localhost. You could also include the localhost in the using block where the other namespaces are imported into the project.
The name of the QuoteService proxy is (strangely enough) QuoteService. You instantiate it like you would any other class. When you call methods, the proxy will wrap the call in a SOAP envelope and send the request to the destination specified within the proxy. Try making calls to GetAQuote, AddQuote, and GetAllQuotes, as shown below.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace ConsumeWebService
{
class Program
{
static void Main(string[] args)
{
localhost.Quote quote;
localhost.QuoteService quoteService;
quoteService = new localhost.QuoteService();
for(int i = 0; i < 10; i++)
{
quote = quoteService.GetAQuote();
System.Console.WriteLine("Quote: " + quote._strQuote);
System.Console.WriteLine( "Originator: " +
quote._strOriginatorFirstName + "" +
quote._strOriginatorLastName);
System.Console.WriteLine();
}
}
}
}
When you run the application, you should see some output like this:
Remember, the beauty of Web services is that they're not tied to a particular platform. The previous example shows how to consume the QuoteService (which is an ASP.NET application). However, Visual Studio builds proxies for any normal Web service. You could easily have searched around for other sites implementing Web services for which Visual Studio will build you a suitable proxy.