Introduction
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:
- In Visual Studio 2010, create a WCF sample Web service application project called "
WcfWebService
" by the WCF service application template of VS 2010. - 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. - 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. - 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.
Test
Below are the steps for testing the WCF Web service project in VS 2010:
- Rebuild projects
WcfServiceLibrary
and WcfWebService
. - Right click project
WcfWebService
under its solution view, then Debug->Start new instance, a browser will launch this application. - In the launched web page, click link Service1.svc, the help page for
Service Service1
will show up.
Discussion
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:
- 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.
- 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.
- 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.
- 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:
- A small monolithic project with budget limitation
- A project for adapting legacy applications
- 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.
More
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.
Conclusion
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.
History
- 9th February, 2011: Initial post