WCF Service library: Creating, Hosting and consuming WCF service with WCF Service Library project template

In this article we will walkthrough on creating a WCF Service by choosing WCF Service Library project template.

Step 1

Create a WCF Service Library project.


Delete all the default codes created by WCF.

Step 2

Modify Operation Contract as below,


Implement the service as below


Step 3

Leave App.config file as it is with EndPoint with wsHttpBinding .

If needed, modify default port number to some other port to avoid getting run time exception while testing the service in WCF Service client.


Step 4

Now run the WCF service library. Just press F5. In WCF Test client service will be running.


Now we can see our service is tested and running in WCF Test client.

Step 5

Add a Web Application to the same solution. We will be hosting the WCF Service library to this web application.


Make web application project as startup project. To do so right click and make the project as start up project.

Step 6

Right click on the web application project and add reference of WCF Service library project , we created in step 1.


Step 7

Now right click on the Web Application project and from general tab add a text file. Give any name to the text file but make sure extension is .svc.


Add the below code in newly added .svc file.


In above code WcfServiceLibrary2 is the namespace of the service we created in step 1.

Step 8

Now we need to modify the Web.Config file of web application project and add the System.serviceModel .


We need to enable the Metadata exchange end point. So add below markup


Now add the service behavior to enable meta data exchange endpoint


And now add the service configuration behavior to the service. After adding that System.serviceModel will look like below


For reference config file will look like below,


<?xml version="1.0"?>

 For more information on how to configure your ASP.NET application, please visit
 <a href="http://go.microsoft.com/fwlink/?LinkId=169433">http://go.microsoft.com/fwlink/?LinkId=169433</a>

 <add name="ApplicationServices"
 connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
 providerName="System.Data.SqlClient" />

 <compilation debug="true" targetFramework="4.0" />

<authentication mode="Forms">
 <forms loginUrl="~/Account/Login.aspx" timeout="2880" />

 <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
 enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
 maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
 applicationName="/" />

 <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>

<roleManager enabled="false">
 <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
 <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />

 <service name ="WcfServiceLibrary2.Service1"
 <endpoint address =""
 binding ="wsHttpBinding"
 contract ="WcfServiceLibrary2.IService1" />
 <endpoint address="mex"
 contract="IMetadataExchange" />
 <behavior name="WcfServiceLibrary2.Service1Behavior">
 <serviceMetadata httpGetEnabled="True"/>
 <serviceDebug includeExceptionDetailInFaults="False" />
 <modules runAllManagedModulesForAllRequests="true"/>


Press F5 to run the service


Step 9

Now we will create a client to call the service. Right click on the same solution and add console project. Make console application as start project.

Add the service reference


Now call the service as below,


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

namespace ConsoleApplication1
 class Program
 static void Main(string[] args)

Service1Client proxy = new Service1Client();
 var r = proxy.GetMessage();


On running we will get the expected output as below,


2 responses to “WCF Service library: Creating, Hosting and consuming WCF service with WCF Service Library project template”

  1. Hello Sir,

    I was in there at your presentation in ahmedabad.I have a question about wcf about which I m not getting clear answer. But after attending your presentation I believe that only you can give clear answer.

    what is difference between webservice and wcf?

    In answer of this question I have read somewhere that wcf service is more flexible. we can create new methods with same name as old method name and we can expose it through new endpoint.

    can you explain me this practically.I will be thankful to you.


  2. Everyone loves what you guys tend to be up too. This sort of
    clever work and reporting! Keep up the fantastic works guys
    I’ve incorporated you guys to our blogroll.

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 )

Facebook photo

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

Connecting to %s

Create a website or blog at WordPress.com