C# Web Services: Stock Quotes
Notes:

Web Service

<%@ WebService Language="C#" Class="StockTicker"%>
using System.Web.Services;
using System.Collections;

public class StockTicker: WebService {
Hashtable tab = new Hashtable();

public StockTicker() {
tab["AMZN"] = 57.74; // Amazon
tab["CSCO"] = 28.50; // Cisco
tab["IBM"] = 99.67; // IBM
tab["MSFT"] = 28.72; // Microsoft
tab["SUNW"] = 5.66; // Sun Microsystems
}

// return the current quote for the requested symbol
[WebMethod]
public double GetQuote(string symbol) {
object quote = tab[symbol];
if (quote == null) return 0.0; else return (double)quote;
}
}

 

If this file is stored in a virtual directory with the name "samples"
one can generate a proxy with the command-line tool wsdl:

  wsdl /out:StockTicker.cs http://localhost/samples/StockTicker.asmx?WSDL

The source code of the resulting proxy class is shown here:

//------------------------------------------------------------------------------
//
// This code was generated by a tool.
// Runtime Version: 1.0.3705.288
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------

//
// This source code was auto-generated by wsdl, Version=1.0.3705.288.
//
using System.Diagnostics;
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.Services;


///
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="StockTickerSoap", Namespace="http://tempuri.org/")]
public class StockTicker : System.Web.Services.Protocols.SoapHttpClientProtocol {

///
public StockTicker() {
this.Url = "http://localhost/Samples/StockTicker.asmx";
}

///
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/GetQuote", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public System.Double GetQuote(string symbol) {
object[] results = this.Invoke("GetQuote", new object[] {
symbol});
return ((System.Double)(results[0]));
}

///
public System.IAsyncResult BeginGetQuote(string symbol, System.AsyncCallback callback, object asyncState) {
return this.BeginInvoke("GetQuote", new object[] {
symbol}, callback, asyncState);
}

///
public System.Double EndGetQuote(System.IAsyncResult asyncResult) {
object[] results = this.EndInvoke(asyncResult);
return ((System.Double)(results[0]));
}
}
 

Client Class for Invoking the Proxy

 

using System;

class StockTickerClient {

public static void Main(string[] arg) {
StockTicker ticker = new StockTicker();
double val = ticker.GetQuote(arg[0]);
Console.WriteLine(arg[0] + " = " + val);
}
}

 

Compile the client and the proxy as follows:

csc StockTickerClient.cs StockTicker.cs

and call the client like this, for example:

StockTicker MSFT

 

No votes yet
Related Links:C# AssembliesC#Web Services in C#: Several scenarios in which web services could be useful.C# Web Services: Fail-safe web service usageC# Web Services: Using the Google Web Service
© Copyright 2008. All Rights Reserved.