Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

Creating First WCF Service

Viewed:  3153 
Posted On:  20/02/2013 07:18:28 
In this article I am going to explain how we can create WCF service step by step. 

In this article I am going to show how we can write our first WCF service step by step.

Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. It is based on WSDL.

The Windows Communication Foundation (or WCF) is an application programming interface (API) in the .NET Framework for building connected, service-oriented applications.

WCF is meant for designing and deploying distributed applications under service-oriented architecture (SOA) implementation.

Now Step by Step implementation...

Open Visual Studio -> File -> New -> Web Site


Image 1.

Check Your Solution Explorer


Image 2.

IService.cs is an interface which contain Service contracts and Data Contracts and Service.cs is a normal class inherited by IService where we can give definition for all the methods.

Open  IService.cs   and do the below code


Image 3.

Now open Service.cs

Give the definition of your interface like below.


Image 4.

In this article I am going to use basicHttpBinding so if you see your WCF service web.config file that should be like below.

      <service name="Service" behaviorConfiguration="ServiceBehavior">
        <!-- Service Endpoints -->
        <endpoint address="" binding="wsHttpBinding" contract="IService">
              Upon deployment, the following identity element should be removed or replaced to reflect the
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity
            <dns value="localhost"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <behavior name="ServiceBehavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing
exception information
          <serviceDebug includeExceptionDetailInFaults="false"/>

After this our WCF service is ready to use.

Now You can check your WCF service by running the solution. It'll be look like.


Image 5.

Now Click on Service.svc


Image 6.

Address of your servcie is


I am going to make a Simple WPF application and I will add the reference of this WCF service.

Open VisualStudio -> File -> New ->


Image 7.

After this add WCF service reference.


Image 8.


Image 9.

Type service Adress and click on Go button, Give NameSpace and click on OK.

Now You WPF application solution explorer will be look like below...


Image 10.

Now check you WPF application app.config file here you will find endpoint entry..

<?xml version="1.0" encoding="utf-8" ?>
                <binding name="WSHttpBinding_IService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" establishSecurityContext="true" />
            <endpoint address="http://localhost:51220/WCF_Service/Service.svc"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService"
                contract="ServiceReference1.IService" name="WSHttpBinding_IService">
                    <dns value="localhost" />

Below is my XAML code in which I getting name in a text box and on click of button I am calling WCF service method.


Image 11.

Run the Application


Image 13.

After click on Button


Image 14.

Now enjoy WCF....

In this solution I added both project in one solution.

 both project in one solution.


Image 15.

         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us