Saturday, June 14, 2008

C# Web Services

This Post illustrates how to write a simple C# Soap Client.

For more information about C# I recommend the following book: Programming .NET Web Services.

C# is not an interpreted language like Perl or Python, so before you can consume a web service you must first generate a stub class. In Visual Studio click Tools->Visual Studio Command Window

In the command window type wsdl this will generate a c# stub file your current directory. Copy this file into your c# project.

For an example lets use the following web service: http://www.webservicex.net/stockquote.asmx?wsdl.

This will create a stub file called StockQuote.cs.

To call this service you just need the following code




using System;

using System.Collections.Generic;
using System.Text;
namespace TestWebService
{
class Program
{
static void Main(string[] args)
{
StockQuote stockQuote = new StockQuote();
Console.WriteLine(stockQuote.GetQuote("nvda").);
}
}
}

Thats it! Make sure to include the System.web.Services references by right click on your project name and selected Add References.

No comments: