Click here to Skip to main content
15,314,843 members
Articles / Programming Languages / C#
Posted 9 Feb 2011

Tagged as


47 bookmarked

WCF Service Library: A Good Approach for WCF Service Application

Rate me:
Please Sign up or sign in to vote.
4.27/5 (20 votes)
9 Feb 2011CPOL5 min read
WCF Service Library: A Good Approach for WCF Service Application


When creating WCF projects, it is a good architectural approach to put all of the actual implementation into a WCF library, then use this library in a separate WCF service application project or in a non-WCF project. As such, the WCF library can be reused in many other places either due to regular class references from another non-WCF library/project, or due to different WCF deployment strategies either by web service in IIS, by windows service, or by command line launching. This article first introduces some simple steps to use a WCF service library in a WCF web service application. Later in the discussion section, it elaborates the reasons for doing so.

The effort of this article is to try to promote this seemingly-simple but really overlooked good approach. For most developers, this seemingly-simple approach is always ignored. For all of the existing WCF web applications I have seen, I rarely saw one which is implemented with the WCF library approach. After reading the elaborated and detailed reasons of using WCF library in the discussion section of this article, hopefully developers will become serious about this approach and use it thereafter.

The auto-generated code from the Visual Studio 2010 template will be used as the sample code with minimal modification so that readers can easily reproduce them.

Using the Code and Steps

The sample WCF Service library project called "WcfServiceLibrary" is automatically generated by the Visual Studio 2010 WCF Service Library template without any modification. Now, let us use it in a WCF Service web application project called "WcfWebService". Below are the simple steps:

  1. In Visual Studio 2010, create a WCF sample Web service application project called "WcfWebService" by the WCF service application template of VS 2010.
  2. Under the solution view of project WcfWebService, add a reference to project WcfServiceLibrary by "References->Add References->Projects", or by "References->Add References->Browser" to its DLL.
  3. Under the solution view of project WcfWebService, delete IService1.cs, also delete Service1.svc.cs under Service1.svc because we will use service implementation from WcfServiceLibrary. But keep Service1.svc.
  4. Under the solution view of project WcfWebService, double click Service1.svc to open it, replace its following marker line:
    <%@ ServiceHost Language="C#" Debug="true" Service="WcfWebService.Service1" 
        CodeBehind="Service1.svc.cs" %> 

    with the following marker lines:

    <%@ ServiceHost Language="C#" Debug="true" Service="WcfServiceLibrary.Service1" %>
    <%@ Assembly Name="WcfServiceLibrary" %>

Save project WcfWebService, then done.


Below are the steps for testing the WCF Web service project in VS 2010:

  1. Rebuild projects WcfServiceLibrary and WcfWebService.
  2. Right click project WcfWebService under its solution view, then Debug->Start new instance, a browser will launch this application.
  3. In the launched web page, click link Service1.svc, the help page for Service Service1 will show up.


You may argue: why should I put all WCF implementation into a WCF library if I will use it only in a web service application? The answer is that you still need to use the WCF library approach. There are several reasons as follows for doing so:

  1. Reality is volatile; things are always changing beyond you can predict. Today you are still dealing with a project, tomorrow you may leave it for a new project. The person who takes over your project may want to refactor or re-architecture it to another WCF deployment strategy.
  2. Even if you are still in this project, requirements may change so that you must re-architecture your project to fit a different WCF deployment strategy. For example, if the good scalability of web service isn't important in your application, you can move web service deployment to windows service deployment for easy security handling and easy maintenance.
  3. Once in a while, applications usually need to be re-architectured and be reimplemented to adapter the changing world and the new technologies. This seems to be inevitable to any application I met: from JDK1.0 to J2EE 6.0, from Borland Delphi and Microsoft Visual Studio 6 to Microsoft Visual Studio 2010/.NET 4. During these processes, libraries are easiest things to be reused or ported from one architecture to another. We should design and develop something ready for changing.
  4. Don't make the architecture too rigid; decoupling and separation is a good and basic design principle. WCF Implementation and deployment are two different categories of tasks which should be decoupled in terms of a good architecture principle. Encapsulating all WCF implementation into a WCF library is just a very good fitting for this decoupling. Furthermore, a WCF service interface usually contains some high level functions, which is actually a service facade layer. This high-level service facade layer will highly possibly need to be reused somewhere, somehow in some ways.

A new project may need the same operations as the WCF service does, but doesn't want to access it by a WCF service way for some reasons. In this type project, reusing an existing WCF library in the same way as using a regular class library will save the efforts to reinvent the wheel. Particularly, if the WCF library contains some good heavyweight facade operations, such as operations dealing with the ADO.NET database domain layer. There are several scenarios in which this type of projects will occur:

  1. A small monolithic project with budget limitation
  2. A project for adapting legacy applications
  3. A monolithic project targeting standalone or offline platforms without the need of a multiple tier architecture

Several times, I experienced the need to reuse the WCF service libraries in a monolithic or non-WCF environment. The good thing is that I always use the WCF library approach.

There is a misconception that WCF library can only be consumed by WCF service application projects. This isn't true. WCF library can be used in the same way as using a regular .NET library by any other type of .NET project. For example, in the demo solution attached in this article, the WPF project WPFAppUsingWCFLib uses the WcfServiceLibrary in the same way as using a regular class library. You can test this by launching WPFAppUsingWCFLib through its folder: WPFAppUsingWCFLib\bin\Debug\WPFAppUsingWCFLib.exe. Don't launch project WPFAppUsingWCFLib through Visual Studio 2010 because Visual Studio will still host WcfServiceLibrary as a WCF service in its WCF development environment and let project WPFAppUsingWCFLib use the WCF service.


For further deployment of project WcfWebService to IIS, you need to modify the web.config in project WcfWebService as the same way as you handle a regular WCF Service application deployment.


Using WCF library is a good approach for any WCF project because doing so makes the WCF service implementation code easily reused flexibly under different practical scenarios. Also using WCF library in a WCF web service application is doable and easy.


  • 9th February, 2011: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ws Hu
United States United States
No Biography provided

Comments and Discussions

QuestionRest WCF need to host in windows service Pin
RamanIndia12-Sep-13 20:42
MemberRamanIndia12-Sep-13 20:42 
GeneralMy vote of 2 Pin
Greg Arrigotti23-Jan-13 13:39
MemberGreg Arrigotti23-Jan-13 13:39 
GeneralMy vote of 4 Pin
nilesh.nildata1-Nov-12 0:59
Membernilesh.nildata1-Nov-12 0:59 
GeneralMy vote of 5 Pin
Yrjo10-Jun-12 23:08
MemberYrjo10-Jun-12 23:08 
QuestionI must admin I'm confused Pin
mwpowellhtx1-Jun-12 23:40
Membermwpowellhtx1-Jun-12 23:40 
AnswerRe: I must admin I'm confused Pin
Ws Hu 6-Jun-12 13:42
MemberWs Hu 6-Jun-12 13:42 
QuestionNot able to implement this approach in VS2008 Pin
haroonyousuf23-Sep-11 5:43
Memberharoonyousuf23-Sep-11 5:43 
AnswerRe: Not able to implement this approach in VS2008 Pin
Ws Hu 18-Apr-12 8:34
MemberWs Hu 18-Apr-12 8:34 
QuestionShould the interface be separated out of the implementation ? Pin
Deecodeproject26-Jun-11 3:39
MemberDeecodeproject26-Jun-11 3:39 
AnswerRe: Should the interface be separated out of the implementation ? Pin
Ws Hu 18-Apr-12 8:45
MemberWs Hu 18-Apr-12 8:45 
GeneralWhy have the ServiceContract attribute if we also intend to use it directly as a class library ? [modified] Pin
Deecodeproject12-May-11 10:29
MemberDeecodeproject12-May-11 10:29 
GeneralRe: Why have the ServiceContract attribute if we also intend to use it directly as a class library ? Pin
Ws Hu 16-Jun-11 10:21
MemberWs Hu 16-Jun-11 10:21 
GeneralRe: Why have the ServiceContract attribute if we also intend to use it directly as a class library ? Pin
Deecodeproject23-Jun-11 12:06
MemberDeecodeproject23-Jun-11 12:06 
GeneralGood Example! Pin
Member 228563622-Apr-11 7:24
MemberMember 228563622-Apr-11 7:24 
GeneralMy vote of 3 Pin
maq_rohit9-Feb-11 18:55
professionalmaq_rohit9-Feb-11 18:55 
GeneralMy vote of 5 Pin
tony hg9-Feb-11 8:01
Membertony hg9-Feb-11 8:01 
GeneralMy vote of 3 Pin
Paulo Zemek9-Feb-11 6:01
MemberPaulo Zemek9-Feb-11 6:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.