Our open source Solution Development Management System (SDMS) will use WCF services as a data source for both it's Silverlight and Desktop applications. To maximize reusability it was necessary to implement Unity so that we can utilize dependency injection within our services.
Initial google research quickly revealed that WCF Behaviors would provide the necessary hooks so that a Unity Container could be instantiated, configured and used to resolve the applicable service.
Of the resources available I found the following to be instrumental to understanding and integrating unity into WCF via behaviors; the MSDN article provides details on WCF Behaviors:
Integrating the Policy Injection Application Block with WCF Services
http://msdn.microsoft.com/en-us/magazine/cc136759.aspx
In this Webcast (Windows 7 users may have to right click and Save Target as to view it) I demonstrate how we can easily switch between the Services and ServicesStub classes below with a simple Web.Config configuration change. Note: both of these classes implement IServices.
Below we show the "Stubs" configuration being utilized:
We'll change "Stubs" to "Services" (see arrow below) to utilize the other class.
And receive the following results:
To implement the Unity Behavior all that was required was to add a reference to the UnityBehavior project and make the following Web.Config changes within the Service to utilize the behavior.
Note: If you do not want to provide a Unity Configuration in the Web.Config all you would have to do is change the line below (pointed to by the arrow) to:
return container.Resolve(type);
And it will resolve the default service - in this case SDMSService.Services.
Source code available HERE
Tags:
wcf,
unity
Categories:
Unity