Saturday, May 16, 2009

Client program to consume Selfhosted WCF Services in Microsoft Visual Studio 2008

1. add new project by Right clicking on Solution
2. go to Windows tab
3. select Console Application then give some name for consuming name: e.g. CalCAreaClient then click ok.
4. Add System.ServiceModel as reference and also Add WCF Service as a Project reference
5. add using System.ServiceModel; on the program.cs
6. Add service Reference, and discover the service and add it.
7. Add that service reference on the code eg.
using CalcAreaClient.ServiceReference1;
8. Create proxy to access service.
eg.
namespace CalcAreaClient
{
class Program
{
static void Main(string[] args)
{
try
{
CalcAreaWcfServiceClient proxy = new CalcAreaWcfServiceClient();
double calcVal = proxy.CalcAreaSquare(5);
Console.WriteLine("Area of Square = {0}", calcVal);
Console.WriteLine("Press enter to terminate client");
Console.ReadLine();
}
catch (Exception e) {
Console.WriteLine("Error on client program");
Console.WriteLine("error {0}", e);
Console.ReadLine();
}
}
}
}

i thinkall te createria is fulfill. Now just run the serviceHost program first and then build this client program.
from visual studio command promt which is diffferent than normal command promt.

No comments:

Post a Comment