Search Chirayu Software Solutions Blog

Wednesday, January 30, 2013

wcf application

Creating simple application using WCF

First open Visual Studio and click file --> Select New --> Website Under that select WCF Service and give name for WCF Service and click OK 

Once you created application you will get default class files including Service.cs and IService.cs

Here IService.cs is an interface it does contain Service contracts and Data Contracts and Service.cs is a normal class inherited by IService where you can all the methods and other stuff.

Now open IService.cs write the following code

public interface IService
string SampleMethod(string Name);

After that open Service.cs class file and write the following code 

public class Service : IService
public string SampleMethod(string Name)
return "First WCF Sample Program " + Name;

Here we are using basicHttpBinding for that our web.config file system.serviceModel code should be like this and I hope no need to write any code because this code already exists in your web.config file insystem.serviceModel

<service name="Service" behaviorConfiguration="ServiceBehavior">
<!-- Service Endpoints -->
<endpoint address="" binding="wsHttpBinding" contract="IService">
<dns value="localhost"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>

Our WCF service ready to use with basicHttpBinding. Now we can call this WCF Service method console applications

After completion of WCF service creation publish or deploy your WCF Service in your system. If you don’t’ have idea on deploy tell me

After completion of deploy webservice now we can see how to use WCF Service in our console application

Calling WCF Service using Console Application

To call WCF service we have many ways like using console app, windows app and web app but here I am going for console application.

Create new console app from visual studio select project type as console application gives some name as you like.

After Creation Console application now we need to add WCF reference to our console application for that right click on your windows application and select Add Service Reference

Now one wizard will open in that give your WCF service link and click Go after add your service click OK button.

After completion of adding WCF Service write the following code in Program.cs class file Main method

static void Main(string[] args)
ServiceReference1.ServiceClient objService = new ServiceClient();
Console.WriteLine("Please Enter your Name");
string Message = objService.SampleMethod(Console.ReadLine());
After that open your app.config file and check your endpoint connection for WCF Service reference that should be like this

<endpoint address=" http://localhost/WCFServiceSample/Service.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService"
contract="ServiceReference1.IService" name="WSHttpBinding_IService">
<dns value="localhost" />
Now everything is ready run your application that output should be like this

Post a Comment