WCF and ASP.NET Web Service Applications

Microsoft Enterprise Library 5.0

DropDown image DropDownHover image Collapse image Expand image CollapseAll image ExpandAll image Copy image CopyHover image

To initialize the container and populate dependencies in a Web service application requires a different approach from the types of applications that expose a user interface (such as Windows Forms, WPF, and ASP.NET Web Forms). This topic describes a possible solution for ASP.NET Web services (ASMX), and points to resources that will help you implement the process in a Windows Communication Foundation (WCF) application.

ASP.NET Web Service Applications

ASP.NET Web services can partially use the technique shown in the topic ASP.NET Web Forms Applications. You can add an extension method to the HttpApplicationState class that creates and exposes the container, and add code to the application's Global.asax file to load the Enterprise Library extension into the container. However, as there are no UI controls in an ASMX application, you cannot use an HTTP module in the same manner as described for ASP.NET Web Forms applications.

Instead, to access the container, you can call the HttpContext.Current.Application.GetContainer() extension method. You can use this reference to the container to pass the current class to the BuildUp method to populate any dependencies you define by applying attributes. For example, the following code uses dependency injection to resolve a concrete instance of a class that implements the IMyService interface (for which the container must contain a mapping) and uses it to calculate the return value from the UseDataService Web method.

C# Copy Code
using System.Web;

public class MyWebService : System.Web.Services.WebService
  private IMyService theService;

  public MyWebService()
    // Pass this class through the container using the BuildUp method    

  public IMyService DataService
    get { return theService; }
    set { theService = value; }

  public string UseDataService()
    return "The value you require is " + theService.DoSomething();
Visual Basic Copy Code
Imports System.Web

Public Class MyWebService
  Inherits System.Web.Services.WebService

  Private theService As IMyService

  Public Sub New()
    ' Pass this class through the container using the BuildUp method    
  End Sub

  <Dependency> _
  Public Property DataService() As IMyService
      Return theService
    End Get
      theService = value
    End Set
  End Property

  <WebMethod> _
  Public Function UseDataService() As String
    Return "The value you require is " & theService.DoSomething()
  End Function

End Class

Alternatively, you can use the basic approach shown in the topic ASP.NET Web Forms Applications to create and populate the container in the Application dictionary object, and then access it from your Web methods when you need to resolve any types.

WCF Applications

In Windows Communication Foundation (WCF) applications, you can use a technique similar to the basic approach described in this topic. However, there is no HttpApplication state store in WCF. Instead, you might use the InstanceContext with the InstanceContextMode set to Single so that only a single instance of the state exists, and is available to all requests. You must also create a custom extension to the InstanceContext by implementing the IExtension interface. For more information, see IExtension(T) Interface on MSDN®.

Alternatively, consider using the WCF Integration Pack for Enterprise Library. It contains classes that you can use directly to enable storage of and access to the container in WCF applications, and it can be used to automatically populate dependencies in your classes. For more information, see the Enterprise Library Community Web site on CodePlex.