Creating, Hosting and Consuming WCF Service with WCF Service Library Project Template

Posted by Dhananjay Kumar Articles | WCF with C# December 27, 2010
In this article we will walk through creation of a WCF Service by choosing the WCF Service Library project template.
Reader Level:


In this article we will walk through creation of a WCF Service by choosing the WCF Service Library project template.

Step 1

Create a WCF Service Library project.

1.gif

Delete all the default codes created by WCF.

Step 2

Modify Operation Contract as below:

2.gif

Implement the service as below:

3.gif

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.

4.gif

Step 4

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

5.gif

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.

6.gif

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

Step 6

Right click on the web application project and add a reference to the WCF Service library project that we created in step 1.

7.gif

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.

8.gif

Add the following code to the newly added .svc file.

9.gif

In the 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 the web application project and add the System.serviceModel.

10.gif


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

11.gif

Now add the service behavior to enable meta data exchange endpoint

12.gif


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

For reference the config file will look like:

13.gif

Web.Config

<?xml version="1.0"?>

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

<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
14.gif

Step 9

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

Add the service reference

15.gif

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:

16.gif

COMMENT USING

Trending up