This article will go through the steps needed to build a fully functional web service by developing a C# class that queries a database. It will also show how to build a forms client application that consumes this service.
To start, I will describe the development environment that I used. While yours may not be exactly the same, hopefully you can make whatever changes are needed to accommodate for this difference. I developed all of this on Windows 2000 Professional with Internet Information Services and the .NET Framework SDK v1.1 also installed. I also have Oracle 9.2 installed as the database. If you have to install any of these products, I would recommend that you install IIS prior to the .NET Framework SDK. The reason for this is that the installation of ASP.NET will also run a program to register and update some scriptmaps for IIS, but only if IIS is already installed. If IIS is not installed then you will need to execute the following command in order for the scriptmaps to be updated.
Additionally, when IIS is installed it will create a local user, ASPNET and put this user in the local USERS group. As a result, when the service executes, it may not have access to every thing it needs. In my case, I needed to grant specific read and execute permissions to the local USERS group for the ORACLE_HOME directory. You may have to do something similar. :)
Building Database and Service
Download the source file and unzip everything to a local directory. You should find that two directories have been created. These being ForwardPriceCalculator for the client and ForwardRateService for the service.
In the ForwardRateService directory, another directory has been created, called Database. This directory contains all the scripts you need in order to create all the procedures, tables, indexes, etc. for the database. Me being an old timer weaned on Oracle 6, I still use SQL*Plus for everything. You will need to change the CreateDB.bat file, to contain the correct userid/password combination along with changing the name of the database. If you are using Oracle, you should be able to change just this file and then run it from a command line and the database objects that the service needs will be created. If you are not using Oracle, all of the scripts are there and you will have to use whatever command line or script-processing tool you normally use to create database objects.
After the database scripts are complete, you now need to modify the class file for the service, the ForwardRate.cs file. On line 25 you should supply the appropriate userid/password combination along with the database name.
m_FXConn = new OracleConnection(
"Data Source=DATABASENAME;User ID=USERID;Password=PASSWORD");
After the change, you now run the BuildServiceDLL.bat file and this will compile the source and produce a DLL which it will place in the /bin directory under wwwroot of IIS. You may need to change this in the .bat file as I have IIS installed on my C: drive, but your installation may be different. This .bat also assumes that you are building the service on the IIS server and that the Oracle database is accessible from the server. Again in my case, the database is installed on the server also.
At this point you can test that the service is working by opening the following URL, http://localhost/ForwardRate.asmx. This should show you a page that displays the name of the service and the different methods that it supports. Click on one of the methods, and you should see a page that describes what each service method does as well as an Invoke button in order to test each service. If you click the GetAllCurrencyCodes service, and then click the invoke button, the service will return the array of strings of all the currency codes.
CalculateFXRate is the important one as the application will use this the most. This method takes in four strings as input parameters and will return a double value as the result. The first two strings are currency codes, such as 'CAD', 'JPY', or 'USD'. The final two strings are dates and these can be entered in any format. Since the underlying database procedure is expecting date types, the method will create date types by parsing these strings. These internal date types are then converted back to strings using a specific format. Knowing the format of these date strings, they can then be converted to the database date type as required by the database procedure.
Building Client (WinForms app)
The first part of building a client application is putting together a DLL that 'talks' to the web service. This has been greatly simplified by executing the following command.
This will create a file called "ForwardPriceCalculator.cs" in the directory where you have executed the command. The classes generated handle all of the communication between your client and the IIS server. It just needs to be compiled into a library and then referenced when building the application.
For the WinForms based client, I have included the source file that was produced using the SharpDevelop IDE. I use this tool to build the form I need and then use a batch file to compile all the stuff together. The form itself consists of two comboboxes that display all the currency codes that can be selected. These are filled by calls to the first web service GetAllCurrencyCodes. I also included two date controls for the input dates to be passed to the CalculateFXRate web service method.
Points of Interest
I see this type of remoting with .NET as a significant benefit to developers over having to implement this through the use of COM or DCOM. It assists in 'hiding' a lot of code that you really don't need to be aware of. As an aside, I got all this working without using Visual Studio, just the .NET SDK and the free IDE from SharpDevelop to help with the form.
- February 26, 2004. - First published.