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.

clip_image002

Delete all the default codes created by WCF.

Step 2

Modify Operation Contract as below,

clip_image003

Implement the service as below

clip_image005

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.

clip_image007

Step 4

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

clip_image009

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.

clip_image011

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.

clip_image012

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.

clip_image014

Add the below code in newly added .svc file.

clip_image016

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 .

clip_image017

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

clip_image019

Now add the service behavior to enable meta data exchange endpoint

clip_image021

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

clip_image023

For reference config file will look like below,

Web.Config


<?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>
 -->

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

<system.web>
 <compilation debug="true" targetFramework="4.0" />

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

<membership>
 <providers>
 <clear/>
 <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="/" />
 </providers>
 </membership>

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

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

</system.web>
 <system.serviceModel>
 <services>
 <service name ="WcfServiceLibrary2.Service1"
 behaviorConfiguration="WcfServiceLibrary2.Service1Behavior">
 <endpoint address =""
 binding ="wsHttpBinding"
 contract ="WcfServiceLibrary2.IService1" />
 <endpoint address="mex"
 binding="mexHttpBinding"
 contract="IMetadataExchange" />
 </service>
 </services>
 <behaviors>
 <serviceBehaviors>
 <behavior name="WcfServiceLibrary2.Service1Behavior">
 <serviceMetadata httpGetEnabled="True"/>
 <serviceDebug includeExceptionDetailInFaults="False" />
 </behavior>
 </serviceBehaviors>
 </behaviors>
 </system.serviceModel>
 <system.webServer>
 <modules runAllManagedModulesForAllRequests="true"/>
 </system.webServer>
</configuration>

 

Press F5 to run the service

clip_image025

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

clip_image026

Now call the service as below,

Programs.cs


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();
 Console.WriteLine(r);
 Console.ReadKey(true);
 }
 }
}

 

On running we will get the expected output as below,

clip_image028

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.

    Mehul

  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