Naveen's Weblog

Bridge to future

Amount To Words using WCF

Posted by codingsense on February 2, 2009


Windows Communication Foundation (WCF) is an SDK for developing and deploying services on Windows. WCF provides a runtime environment for your services, enabling you to expose CLR types as services. You can build services without WCF but in practice building services is easier with WCF. WCF can be implemented using the assembly called System.ServiceModel.dll in the System.ServiceModel namespace.

We shall continue with a simple example of Converting given amounts to words.

conceptamounttowords

Concept

Download Project – 51.3 Kb

The steps that will be followed in this project are
1) Create Server Application
2) Compile Server Application to generated files needed by client using SVCUTIL.EXE.
3) Create Client Application
4) Add Server files in Client Applications
5) Run the Project

1) Create Server Application :
So we shall start the session, Open Visual studio 2008 -> New Project -> Select WCF Service Application -> enter name StringServer -> Press OK.

 

Create Server Project

Create Server Project

So we have created a project StringServer that will act as a service.

By default we get files named as IService1.cs and Service1.svc for better understanding we will replace all service1 to AmountToWords at the following places

1) Rename svc and cs file in solution explore with AmountToWords.svc and IAmountToWords.cs
2) Open the AmountToWords.svc and change the class name from Service1 to AmountToWords
3) Right click on AmountToWords.svc in solution explorer and click on view markup and rename service1 to AmountToWords
4) Open Web.config file , navigate to system.servicemodel node in the xml file and rename all service1 to AmountToWords and Iservice1 to IAmountToWords as below

 

Web.Config After renaming

Web.Config After renaming

 

 

By the changes we remove all the references to service1 or Iservice1.

Now open the IAmountToWords.cs and modify to

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace StringServer
{
    [ServiceContract]
public interface IAmountToWords····
{
[OperationContract]
string ToWords(double value);
}
}

Open the AmountToWords.svc and modify to

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace StringServer
{
public class AmountToWords : IAmountToWords····
{

        #region IStringOperation Members

/// ········
/// Converts the given numerci amount to Words········
/// ········
/// Amount to be conveted········
/// ········
public string ToWords(double Rupees)
{
return ToWords(Rupees, true);
}

/// ········
/// Converts the given numerci amount to Words········
/// ········
/// Amount to be converted········
/// True/False for the string Ruppes Only has ········
/// to be attached to the converted amount or not········
/// ········
public string ToWords(double Rupees, bool AttachRupeesOnly)
{
string Crore, Lakh, Thousand, Hundred, TensUnits, Paisa, strRupees;
ToWords(Rupees, out Crore, out Lakh, out Thousand, out Hundred, out TensUnits, out Paisa);
strRupees = Crore + Lakh + Thousand + Hundred + TensUnits + Paisa;
return (AttachRupeesOnly ? "Rupees " + strRupees + " Only" : strRupees);
}

/// ········
/// Returns string in rupees in words format of given reupees value········
/// Usign Mid(string, int, int), Overloaded Mid(string, int); Methods of clsSupport.cs class········
/// Using System.Math.Abs(double); Methods of System.Math class········
/// Using FnStrDigits(string, string, string); Methods of this calss········
/// ········
/// ········
/// string········
public void ToWords(double Rupees, out string Crore, out string Lakh, out string Thousand, out string Hundred, out string TensUnits, out string Paisa)
{
Crore = Lakh = Thousand = Hundred = TensUnits = Paisa = string.Empty;
Rupees = Math.Abs(Rupees);
if (Rupees == 0)
{
TensUnits = "Zero";
}
else············
{
string Rupee_Str = String.Format("{0:0.00}", Convert.ToDouble(Rupees));
string strPaise = Rupee_Str.Substring(Rupee_Str.IndexOf(".") + 1, 2);
Rupee_Str = String.Format("{0:000000000000000}", Convert.ToDouble(Rupee_Str.Substring(0, Rupee_Str.IndexOf("."))));
double Crores = Convert.ToDouble(Rupee_Str.Substring(0, Rupee_Str.Length - 7));

Crore = Crores > 99 ? ToWords(Crores, false) + " Crore " :
JoinDigits(string.Empty, " Crore ", Crores.ToString());

Lakh = JoinDigits(string.Empty, " Lakhs ", Rupee_Str.Substring(Rupee_Str.Length - 7, 2));
Thousand = JoinDigits(string.Empty, " Thousand ", Rupee_Str.Substring(Rupee_Str.Length - 5, 2));
Hundred = JoinDigits(string.Empty, " Hundred ", Rupee_Str.Substring(Rupee_Str.Length - 3, 1));
TensUnits = JoinDigits(string.Empty, string.Empty, Rupee_Str.Substring(Rupee_Str.Length - 2, 2));
Paisa = strPaise.Length == 0 ? string.Empty : JoinDigits(" and ", " Paise", Convert.ToDouble(strPaise).ToString());
}
}

/// ········
/// Returns string equivalent to given rupees value········
/// Using ConvertDigit(int); Methods of this class········
/// ········
/// ········
/// ········
/// ········
/// string········
private string JoinDigits(string PreText, string PostText, string Dig_Val)
{
Int32 Amount = Convert.ToInt32(Dig_Val);
return (Amount == 0) ? string.Empty : (PreText + ConvertDigit(Amount) + PostText);
}

/// ········
/// Returns string equivalent to number.········
/// ········
/// ········
/// string········
public static string ConvertDigit(int Amount)
{
string[] AmountToWords = { string.Empty, "One", "Two", "Three", "Four", "Five", "Six"
, "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", 
"Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Thirty", "Forty", "Fifty"
, "Sixty", "Seventy", "Eighty", "Ninety" };

return Amount > 20 ? AmountToWords[20 + Amount / 10 - 2] + " " + AmountToWords[Amount % 10]
: AmountToWords[Amount];
}

        #endregion····
}
}

Build the application and check if it does not have any error. Wow Rebuild all succeded.
Now run the application you it will open in a web browser and give the insructions that are to be followed. Lets follow it.

2) Compile Server Application to generated files needed by client using SVCUTIL.EXE.
Copy the path that is given in the browser like : svcutil.exe http://localhost:2786/AmountToWords.svc?wsdl
Open Visual studiio 2008 command prompt and navigate to C:Program FilesMicrosoft SDKs
paste the above copied path and press enter
The exe will download the metadata from the server application and create 2 files AmountToWords.cs and OutPut.config in c:Program FIlesMicrosoft SKDs.

 

Using svcutil.exe

Using svcutil.exe

This completes the work with server application

3) Create Client Application
Open the same StringServer solution, In Solution explore right click on solution StirngServer and click Add ->
New Project -> Console Application -> StringClient -> Ok

 

Add Client Project

Add Client Project

By this we have created a client project that will access the server to get desired result.

Now we shall add the 2 files those were created by SVCUtil.exe in the above step to the StringClient Project
Right click on the StringClient Project -> Add -> Existing files -> in object Name enter *.* -> enter ->
Select both files AmountToWords.cs and Output.config -> Ok

Right click on references in StringClient project -> Add Reference -> Select .Net Tab -> Goto System.ServiceModel.dll -> Ok

Right Click on references in StringClient Project -> Add Service Reference -> Click on discover ->
you will get the address of the service that is running -> Enter namespace as StringServerReference -> OK

By following the above steps the Service Reference will be added to your StringClient Project.

Now finally in the program.cs paste the following code to check the application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringClient
{
class Program    
{
static void Main(string[] args)
{
StringServerReference.AmountToWordsClient amtToWords = new StringClient.StringServerReference.AmountToWordsClient();
Console.WriteLine(amtToWords.ToWords(98765432.87));
Console.Read();
}
}
}

Right click StringClient Project in solution explorer and click set as start up project and Run the application.

Output:

 

Output

Output

Depending on the input, the amount will be converted into words and sent back to the cilent application.

WCF is very simple and any sample can be implemented in a similar way as we do in windows application, we just need to concentrate much on the steps that is required to communicate between the client and server.

References:
http://msdn.microsoft.com/en-us/library/ms734712.aspx

:) Happy learning :)

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: