Naveen's Weblog

Bridge to future

Posts Tagged ‘WCF’

Hosting WCF and ASP.NET application under same virtual directory in IIS

Posted by codingsense on October 28, 2009

Hi,

I had created a WCF service and an ASP.NET application which would consume the service, I hosted both in separate virtual directories and it was working very fine. After some days and lots of changes I noticed that the dlls and other common files used in both WCF and ASP.net application, had to be replaced in both the virtual directories to synchronize them.

So I planned to see if it’s possible to host both the application under same virtual directory and got a solution of merging the web.Config files of both the application. Now I have to replace the necessary assemblies or files in only one virtual directory and it has made my job lot easier.

Following are the steps that were followed.
Note: At the end I removed the WCF virtual directory and made my ASP.Net virtual directory to host both the application.

  • Copy the required files (Except Config file) from WCF virtual directory and place them appropriately in the structure in ASP.net virtual directory. (Eg: .svc file in the main folder and other required dlls into the bin folder)
  • Now we have all files in appropriate locations, just we need to do is to merge the web.config files to contain both Service as well as client configuration.
  • Open the web.Config file that was in WCF virtual directory and copy the <services> tag and <behaviour> tag with all its contents.
  • WebConfig

  • Open the web.Config file of ASP.net application and paste the copied contents in the <system.serviceModel> tag.
  • Copy the new path of the service by brosing the .svc file from IIS manager (inetmgr.exe) Note: If it wont run successfully check for required setting for the service in web.Config.
  • In the web.Config in the ASP.net virtual directory and change the address of all the endpoints to point to the new location of service.
  • Run the main application.

After following the above procedure I successfully hosted both applications under same virtual directory.

If you have any problem while following the steps or any other problem related to WCF or IIS you can please share it with me.

Happy Learning 馃檪
Naveen Prabhu

Posted in WCF | Tagged: , , | Leave a Comment »

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 馃檪

Posted in WCF | Tagged: , | Leave a Comment »