Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WPF

Currency Converter Application using WPF and WCF in Windows Vista

3.82/5 (5 votes)
11 Apr 20073 min read 1   2.1K  
This article is about implementing a simple Currency Converter Application using Windows Presentation Foundation (WPF) and Windows Communication Foundation (WCF) technology.

Introduction

This article is about implementing a simple Currency Converter Application using Windows Presentation Foundation (WPF) and Windows Communication Foundation (WCF) technology.

The Currency Converter Application uses WPF for building an application that provides lending application UI experience of Windows Vista and uses WCF for consuming the web service using Microsoft's unified programming model for building SOA applications.

Background

Giving a brief introduction about Windows Presentation Foundation (WPF) and Windows Communication Foundation (WCF) technology is beyond the scope of this article. In a few words, Windows Presentation Foundation (WPF) provides developers with a unified programming model for building rich Windows smart client user experiences that incorporate UI, media, and documents. Windows Communication Foundation (WCF) is Microsoft's unified programming model for building service-oriented applications. It enables developers to build secure, reliable, transacted solutions that integrate across platforms and interoperate with existing investments.

Getting Started

Following are the prerequisites for creating the Currency Converter Application:

  1. Visual Studio 2005
  2. .NET 3.0 Framework
  3. Visual Studio 2005 Extensions for WCF, WPF

In this article, the Currency Converter Application consumes www.webservicex.net currency converter web service http://www.webservicex.net/CurrencyConvertor.asmx) using WCF web service client. But if you are familiar with WCF, you can create your own Currency converter web service and point this Application to your WCF web service.

Building the Application

Currency Converter uses XAML front-end build using Windows Application (WPF) template to create a WPF client in Visual Studio .NET 2005. The Currency Converter application will invoke ConversionRate() web method using WCF web service client. The ConversionRate() web method takes two parameters "From Currency Country code" and "To Currency Country code".

Figure 1: Here is the simple SOAP Request

XML
POST /CurrencyConvertor.asmx HTTP/1.1
Host: www.webservicex.net
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.webserviceX.NET/ConversionRate"
<envelope xmlns:soap=http://schemas.xmlsoap.org/soap/envelope/ 
        xmlns:xsd=http://www.w3.org/2001/XMLSchema 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<body><conversionrate xmlns="http://www.webserviceX.NET/" />                    
  <fromcurrency />
  AFA or ALL or DZD or ARS or AWG or AUD or BSD or BHD or BDT or BBD or BZD or 
  BMD or BTN or BOB or BWP or BRL or GBP or BND or BIF or XOF or XAF or KHR or 
  CAD or CVE or KYD or CLP or CNY or COP or KMF or CRC or HRK or CUP or CYP or 
  CZK or DKK or DJF or DOP or XCD or EGP or SVC or EEK or ETB or EUR or FKP or 
  GMD or GHC or GIP or XAU or GTQ or GNF or GYD or HTG or HNL or HKD or HUF or 
  ISK or INR or IDR or IQD or ILS or JMD or JPY or JOD or KZT or KES or KRW or 
  KWD or LAK or LVL or LBP or LSL or LRD or LYD or LTL or MOP or MKD or MGF or 
  MWK or MYR or MVR or MTL or MRO or MUR or MXN or MDL or MNT or MAD or MZM or 
  MMK or NAD or NPR or ANG or NZD or NIO or NGN or KPW or NOK or OMR or XPF or
  PKR or XPD or PAB or PGK or PYG or PEN or PHP or XPT or PLN or QAR or ROL or 
  RUB or WST or STD or SAR or SCR or SLL or XAG or SGD or SKK or SIT or SBD or 
  SOS or ZAR or LKR or SHP or SDD or SRG or SZL or SEK or CHF or SYP or TWD or 
  TZS or THB or TOP or TTD or TND or TRL or USD or AED or UGX or UAH or UYU or 
  VUV or VEB or VND or YER or YUM or ZMK or ZWD or TRY
  </fromcurrency />
  <tocurrency />
  AFA or ALL or DZD or ARS or AWG or AUD or BSD or BHD or BDT or BBD or BZD or 
  BMD or BTN or BOB or BWP or BRL or GBP or BND or BIF or XOF or XAF or KHR or 
  CAD or CVE or KYD or CLP or CNY or COP or KMF or CRC or HRK or CUP or CYP or 
  CZK or DKK or DJF or DOP or XCD or EGP or SVC or EEK or ETB or EUR or FKP or 
  GMD or GHC or GIP or XAU or GTQ or GNF or GYD or HTG or HNL or HKD or HUF or 
  ISK or INR or IDR or IQD or ILS or JMD or JPY or JOD or KZT or KES or KRW or 
  KWD or LAK or LVL or LBP or LSL or LRD or LYD or LTL or MOP or MKD or MGF or 
  MWK or MYR or MVR or MTL or MRO or MUR or MXN or MDL or MNT or MAD or MZM or 
  MMK or NAD or NPR or ANG or NZD or NIO or NGN or KPW or NOK or OMR or XPF or
  PKR or XPD or PAB or PGK or PYG or PEN or PHP or XPT or PLN or QAR or ROL or 
  RUB or WST or STD or SAR or SCR or SLL or XAG or SGD or SKK or SIT or SBD or 
  SOS or ZAR or LKR or SHP or SDD or SRG or SZL or SEK or CHF or SYP or TWD or 
  TZS or THB or TOP or TTD or TND or TRL or USD or AED or UGX or UAH or UYU or 
  VUV or VEB or VND or YER or YUM or ZMK or ZWD or TRY
</body>
</envelope>

And ConversionRate() web method returns the currency difference in double.

Figure 2: Here is the SOAP response

XML
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ConversionRateResponse xmlns="http://www.webserviceX.NET/">
<ConversionRateResult>double</ConversionRateResult>
</ConversionRateResponse>
</soap:Body>
</soap:Envelope>

In the Currency Converter Application, the XAML window takes two inputs "From Currency" and "To Currency" as shown in the figure below.

Figure 3: Currency Convertor Application Window

Screenshot - ccapp.jpg

The above form is created using WPF client in Visual Studio .NET by selecting the Windows Application (WPF) template application project type.

Below is the complete XAML for the Currency Converter Application User Interface.

Figure 4: CurrencyConverter.xaml

XML
<Window x:Class="CurrencyConvertorApp.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Currency Convertor" Height="260" Width="497"
Icon =".\Icon1.ico">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.776951672862453*" />
<RowDefinition Height="0*" />
<RowDefinition Height="0.223048327137546*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.185267857142857*" />
<ColumnDefinition Width="0.339285714285714*" />
<ColumnDefinition Width="0.0334821428571429*" />
<ColumnDefinition Width="0.441964285714286*" />
</Grid.ColumnDefinitions>
<Label Height="25.96" HorizontalAlignment="Left" Margin="14.0066666666667,25.04,0,0" 
    Name="label1" VerticalAlignment="Top" Width="98.9933333333333" 
    Grid.ColumnSpan="2">From Currency:</Label>
<Label Height="25.96" HorizontalAlignment="Left" Margin="15.0066666666667,60.04,0,0" 
    Name="label2" VerticalAlignment="Top" Width="83.9933333333333" 
    Grid.ColumnSpan="2">To Currency:</Label>
<ComboBox Height="26" Margin="39,25,23,0" Name="cmbFromCurrency" 
    VerticalAlignment="Top" Grid.Column="1" Grid.ColumnSpan="3" />
<ComboBox Height="26" Margin="38,62,24,0" Name="cmbToCurrency" 
    VerticalAlignment="Top" Grid.Column="1" Grid.ColumnSpan="3" />
<Button Margin="96.0000000000001,0,26.9999999999999,49.8141263940521" 
    Name="btnConvert" Click="OnClick_Convert" Grid.Column="3" 
    Height="28.7397769516729" VerticalAlignment="Bottom">Convert</Button>
<TextBox Margin="22,0,17,25.3940520446097" Name="txtResults" Grid.ColumnSpan="4" 
    Height="59.4758364312266" Grid.RowSpan="3" VerticalAlignment="Bottom"></TextBox>
</Grid>
</Window>

To create a client application for a WCF service, the following steps are required:

  1. Obtain the service contract, binding, and address information for a service endpoint.
  2. Create a WCF client using that information.
  3. Call operations.
  4. Close the WCF client object.

If you are going to create a WCF client object to invoke a CurrenyConverter service and you know that the metadata for that service is published at http://www.webservicex.net/CurrencyConvertor.asmx?wsdl, the svcutil.exe tool comes as part of the WinFX SDK. To use the tool, open the command shell under Microsoft windows SDK programs and run svcutil.exe passing the currencyconverter.asmx WSDL URI as a parameter. This will generate two files one is app.config and clientcode.cs.

Figure 5: WCF client application ServiceModel Metadata Utility

svcutil /language:cs /out:ClientCode.cs /config:app.config 
        http://www.webservicex.net/CurrencyConvertor.asmx?wsdl
  • app.config contains the configuration information needed to set up the communication channel & bindings.
  • ClientCode.cs is the proxy implementation and has all the service contract in managed code for currencyconverter.asmx

Include these two files in the Currency Converter Application Project and create the instance of CurrencyConverterSoapClient class and call the ConversionRate() web method. Create a try/catch block, call operations in the same way that you would if the object were local, and close the WCF client object. When the first operation is called by the client application, WCF opens the underlying channel automatically and the underlying channel is closed when the object is recycled.

Using the code

The code snippet below shows an invocation using WCF client when clicked from WPF convert button.

C#
void OnClick_Convert(object sender, RoutedEventArgs e)
{
    try
    {
        double dResult = 0;
        //store the default brush for the convert button before call
        Brush dbrush=this.btnConvert.Background;
        //change the background of convert button before call
        this.btnConvert.Background = Brushes.LightBlue;
        //create the CurrencyConvertor webservice 
        CurrencyConvertorSoapClient wsCurrencyConverter = 
                        new CurrencyConvertorSoapClient();
        //call the webmethod
        dResult = wsCurrencyConverter.ConversionRate
                    ((Currency)Enum.Parse(typeof(Currency), 
        cmbFromCurrency.SelectionBoxItem.ToString().Substring(0, 3)), 
        (Currency)Enum.Parse(typeof(Currency), 
        cmbToCurrency.SelectionBoxItem.ToString().Substring(0, 3)));
        txtResults.Text = cmbFromCurrency.SelectionBoxItem.ToString() + 
            " To " + cmbToCurrency.SelectionBoxItem.ToString() + 
            " = " + Convert.ToString(dResult);
        //change back the to default brush
        this.btnConvert.Background = dbrush;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
    }     
}

References

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here