UnderGround Forums
 

ITGround >> HOWTO: Call Web Service w/ .NET


5/15/04 9:32 PM
Ignore | Quote | Vote Down | Vote Up
theseanster
261 The total sum of your votes up and votes down Send Private Message Add Comment To Profile

Edited: 15-May-04
Member Since: 05/13/2002
Posts: 4720
 
Someone brought up the idea of having forum lessons. Here's one of my contributions for those interested in learning C# and .NET in general. First, you will need to download the .NET SDK and runtime (free).

First, I made a directory C:\Temp\SampleApp.

From there you will need to use the .NET framework SDK tools CSC.EXE and WSDL.EXE.

NOTE: I already have a DOS mapped path, but feel free to alter the command line paths or use different folders.

OK, the first you thing you need to do is build a proxy class using the wsdl.exe tool that comes free with the .NET SDK. We will reference a stock Web Service and tell .NET to build us a C# class based on the WSDL (Web Service Description Language). The following command does this:

C:\temp\sampleapp>wsdl.exe /out:stocksvc.cs http://www.gama-system.com/webservices/stockquotes.asmx?wsdl

Feel free at this point to open the generated stocksvc.cs file with notepad or your favorite text editor. It is not necessary however.

Now, we will build the stocksvc.cs into a .NET assembly, specifically the library format .dll using the CSC (C# compiler):

C:\temp\sampleapp>csc /target:library stocksvc.cs

Now that we have a proxy dll to call our Web Service, we can write a simple client application in C# to call the Web Service. Type the following code into notepad or your favorite text editor and save to your chosen dev directory (in my case C:\temp\sampleapp) as App.cs:

using System;

class App {
    static void Main(string[] args) {
        StockQuotes webService = new StockQuotes();
        webService.Credentials = System.Net.CredentialCache.DefaultCredentials;
        Console.WriteLine("Getting quote for JDSU...");
        decimal result = webService.GetLatestStockDailyValue("NASDAQ", "JDSU");
        Console.WriteLine("Current JDSU quote is {0}", result);
    }
}

After you save this code as App.cs, you can use the CSC utility to compile the app. This time we will reference the stocksvc.dll proxy assembly we created earlier:

C:\temp\sampleapp>csc /reference:stocksvc.dll /out:app.exe app.cs

That's it! Now you can run your application at the DOS prompt to see the value of that wonderful JDSU stock:

C:\temp\sampleapp>app.exe
Getting quote for JDSU...
Current JDSU quote is 3.12

C:\temp\sampleapp>

 

Let me know if you have any questions. Have fun.

---theseanster

5/16/04 8:30 PM
Ignore | Quote | Vote Down | Vote Up
Drift
Send Private Message Add Comment To Profile

Edited: 16-May-04
Member Since: 01/01/2001
Posts: 8187
these lessons are a good idea
5/17/04 9:20 AM
Ignore | Quote | Vote Down | Vote Up
Andrew Yao
Send Private Message Add Comment To Profile

Edited: 17-May-04
Member Since: 01/01/2001
Posts: 1858
Cool.
6/8/04 12:41 AM
Ignore | Quote | Vote Down | Vote Up
theseanster
261 The total sum of your votes up and votes down Send Private Message Add Comment To Profile

Edited: 08-Jun-04
Member Since: 05/13/2002
Posts: 4752
ttt
6/23/05 1:58 AM
Ignore | Quote | Vote Down | Vote Up
theseanster
261 The total sum of your votes up and votes down Send Private Message Add Comment To Profile

Edited: 23-Jun-05
Member Since: 05/13/2002
Posts: 7530
ttt again
6/23/05 9:21 AM
Ignore | Quote | Vote Down | Vote Up
reagan123
96 The total sum of your votes up and votes down Send Private Message Add Comment To Profile

Edited: 23-Jun-05
Member Since: 01/01/2001
Posts: 726
awesome stuff.. thanks
11/13/12 11:38 AM
Ignore | Quote | Vote Down | Vote Up
theseanster
261 The total sum of your votes up and votes down Send Private Message Add Comment To Profile

Member Since: 5/13/02
Posts: 21202

this tutorial is old but it still works if you want to give it a try


Reply Post

You must log in to post a reply. Click here to login.