Unity & friends: The WCF service host side
November 2nd, 2008
I want to share with you some pieces of code that you can use to achieve a more flexible design on your applications. What you can achieve with this is that your WCF service instances are built taking advantage of Dependency Injection by using the Unity Application Block (from Microsoft P&P).
Setting up the sample
I will be using a sample project (called DoppleBock) which you can run in your machine and see how it works. Note that to be able to experiment with the whole solution you will need:
To run the solution tests:
-
- Microsoft Visual Studio 2008 Professional (or higher version)
- Moq (assemblies provided in the solution)
Do you have the products? Download the DoppleBock sample from my SkyDrive and let’s start!
Add Unity to your project
Once you installed Unity, its assemblies will be registered in the GAC (Global Assembly Cache). The references that your host projects will need to contain will be the following:
Unity has containers that holds the type-mapping and parameter resolution configuration and the instances of the resolved types that it will take care of during the objects lifetime. There are two ways of configure these containers, but I will explain about the configuration file configuration via. This method for configuring the containers is more flexible when you work with Web applications, since it lets you to reconfigure the dependency resolution without recompiling the published website.
First, you will need to add the Unity configuration section, so it can be recognized by the application. Add this mapping to the host configuration file. This is taken from our sample, you can find this in the DoppleBock.Services.Host/web.config file.

Talking with interfaces
One of the possible Dependency Injection resolving methods that Unity allows you to use is the constructor injection. The sample solution (DoppleBock) uses interface implementation resolution in two moments:
- When the Website logic needs to resolve a service client
- When the services needs to resolve a data access layer repository
By default, WCF doesn’t provide a way to use service types without parameter-less constructors, but our sample provides you a way to accomplish this that I will explain later in this post.
You can find a service implementation class that contains the following constructor:
The service host configuration file (DoppleBock.Services.Host/web.config) contains the type mapping definition that tells Unity how to resolve this type in run-time.
The ‘external’ lifetime manager tells Unity to maintain weak references with the object instances, so if the instance has been collected by the garbage collector a new one will be resolved.
Also, I am telling to Unity that DoppleBock.Data.SqlProjectsRepository has to be constructed with a System.String connectionString parameter that will contain certain value.
Let WCF inject
If you take a look to the system.serviceModel configuration section of our services host configuration file (DoppleBock.Services.Host/web.config), it contains a behavior extension element. This element register a <unity /> tag that you can add to a service behavior.
This <unity /> tag can also recieve additional parameters:
- unityConfigurationSectionPath: If you placed your Unity configuration section in a different path than “unity”, you can set your path through this attribute
- containerName: Unity name-less constructor is the default, but if you want to use a named one for the services with this behavior you can set its name here.
Its implementation basically replaces the service IInstanceFactory by our own instance factory that resolves the services instances through Unity. If you want to take a look at its code you can find its in the Unity.ServiceModel project.
Next time: Unity & friends: The MVC application side
I plan to write another post about how I am using the injection in MVC. If you browse the sample code, the controllers are also created through injection, so I expect to be able to clarify how this is being done in the next post, which means… add me to your feeds, and let me know your feedback! Thanks!
Preserving Object Reference in WCF
June 16th, 2008
I found a very useful post about WCF contracts and object references. For those who need to serialize an object graph with cyclic references check out this: http://blogs.msdn.com/sowmy/archive/2006/03/26/561188.aspx