This documentation is archived and is not being maintained.

Service Reusability

Visual Studio 2010

Applies to: Windows Communication Foundation

Published: June 2011

Author: Robert Dizon

Enterprise applications generally focus on specific functional areas, such as corporate, human resources, finance, or sales and marketing. This practice often leads to redundant application components. A successful enterprise integration project establishes communication not only between computer systems, but also between business units. Separate departments will not control individual applications. Instead, they use a shared group of services. A service is a well-defined function that is universally available, and responds to requests from "service consumers." After an enterprise assembles a collection of useful services, managing those services becomes critical.

The following reference architecture illustrates, at a high-level, an integration scenario.

Referenced Screen

Communication begins with the service consumer, which is typically the presentation layer of either a web or mobile application with which users interact. The data from the service consumer goes to the collections of WCF integrated services. These services send the business service messages to their appropriate destination(s). Destinations might be an ERP system, a legacy provider, an off-site subsidiary, or remote partners. The figure also illustrates sets of services that are commonly found in enterprise integration scenarios. These services include flexible, reusable message handling (both the transformation and constituent logic, which can be customized) and other common reusable services, such as an error or exception handler, a notification handler, and a logging handler.

The ability to reuse services is one of the principal benefits of a Service-Oriented Architecture (SOA). The Microsoft Enterprise Library encapsulates the Microsoft recommended practices for Microsoft® .NET Framework-based applications development. In addition:

  • It provides sufficient functionality to support the common scenarios that enterprise-level applications must address.

  • Its application blocks can serve as the basis for a custom library. You can either modify an application block's existing source code or use its extensibility points.

  • Application blocks are independent of each other. You can add application blocks as you need them.

For more information on Enterprise Library 5.0, see "Enterprise Library 5.0" on MSDN at

When you install Enterprise Library, it automatically adds itself to the Visual Studio development environment. The following procedure explains how to add the Logging Application Block to the WCFServiceApplicationA sample project that accompanies this article.

  1. Open the WCF solution in Visual Studio on a computer that has Enterprise Library installed. In Solution Explorer, right-click the web.config file. Click Edit Enterprise Library V5 Configuration.

  2. The Enterprise Library configuration tool launches.

  3. On the Blocks menu, click Add Logging Settings.

  4. Click the Logging Target Listeners expander, (the plus sign on the right), point to Add Logging Target Listeners, and then click Rolling Flat File Trace Listener. This option automatically rolls the trace log into a new file once the maximum size per trace file is reached.

  5. Click the ellipsis button (…) use the default file name, and then click Open. This adds the path to the rolling file name.

To add the Logging Application Block to the WCF service, provide the following assembly reference.

using Microsoft.Practices.EnterpriseLibrary.Logging;

The following code is an example of how to add logging functionality. The relevant method is in italics.

C# Code Version

public string GetData(int value)
    // if the user enters 2 we need to pass this value to web service residing in http://win-5uv7qim0esb:8080/Service2.svc
    if (value == 2)
        WCFServiceApplicationB.Service1Client wsPort8080 = new WCFServiceApplicationB.Service1Client();
        logEnterpriseLibrary("Invoke Web Service 2");
        return string.Format("WS #2: {0}", value);

VB .NET Code Version

Public Function GetData(value As Integer) As String
     ' if the user enters 2 we need to pass this value to web service residing in http://win-5uv7qim0esb:8080/Service2.svc
     If value = 2 Then
          Dim wsPort8080 As New WCFServiceApplicationB.Service1Client()
          logEnterpriseLibrary("Invoke Web Service 2")
          Return String.Format("WS #2: {0}", value)
     End If
End Function

The following code is the logging interface that is used by the Logging Application Block.

C# Code Version

public void logEnterpriseLibrary(string strmessage)
    LogEntry log = new LogEntry();
    log.EventId = 1;
    log.Message = strmessage;

VB.NET Code Version

Public Sub logEnterpriseLibrary(strmessage As String)
     Dim log As New LogEntry()
     log.EventId = 1
     log.Message = strmessage
End Sub

Enterprise Library has other configurable services besides logging. For example, the Policy Injection Application Block enables you to change the behavior of custom objects, and other .NET classes. In the current version of Enterprise Library, policy injection is implemented through the Unity Application Block (Unity). Unity is a dependency injection container that allows you to create reusable services.

Unity includes a service locator component. To see how it works, consider an application that uses a class named FileLogger that logs error messages. Without Unity, the application must create an instance of the FileLogger class whenever a log record needs to be written. The following code is an example of how to do this.

C# Code Version

FileLogger myLogger = new FileLogger();

VB.NET Code Version

Dim myLogger As New FileLogger ()

Unfortunately, if the application needs to use a logging service other than the FileLogger class at some point in the future, you will need to make many code changes. With Unity, you can map the ILogger interface to the FileLogger class in just one place in your application, which makes it much easier to choose a different logging service if you ever need to. The following code shows how to use Unity to map the interface to the FileLogger class.

C# Code Version

IUnityContainer myContainer = new UnityContainer();
myContainer.RegisterType<ILogger, FileLogger>(); 

VB.NET Code Version

Dim myContainer As IUnityContainer = New UnityContainer()
myContainer.RegisterType(Of ILogger, FileLogger)()

You only need to modify the second line of code if you decide to use a different logger.

After the FileLogger class is registered as the desired provider of the ILogger interface, the application can use the following code to obtain a reference to the default logging component wherever it needs a logging service.

C# Code Version

ILogger myLogger = myContainer.Resolve<ILogger>();

VB.NET Code Version

ILogger myLogger = myContainer.Resolve<ILogger>();

For more information about Unity, see the following articles on MSDN.

Use the Enterprise Library Configuration Tool to configure policy settings. Here are the steps.

  1. Open the WCF solution in Visual Studio on a computer that has Enterprise Library installed. In Solution Explorer, right-click the web.config file. Click Edit Enterprise Library V5 Configuration.

  2. The Enterprise Library configuration tool launches.

  3. On the Blocks menu, click Add Policy Injection Settings. Right-click on the expander (the plus sign) in the Policies pane to add a default policy, named Policy.

  4. Right-click on the Policy toolbar in the Policies pane. A Call Handlers dialog box opens that gives you the option to add a handler.

  5. In the Name text field, find the handler named Logging Call Handler and add it. This call handler allows you to inject a custom logging handler into the service.

The following figure illustrates how to add a logging call handler.

Referenced Screen

For more information on how to create a policy injection call handler see "Creating Interception Policy Injection Call Handlers" on MSDN at

For more information on advanced configuration scenarios in Enterprise Library, see "Advanced Configuration Scenarios using Enterprise Library" on MSDN at

Previous article: WCF Configuration Tools

Continue on to the next article: Performance and Scalability