Seems the documentation is badly organised.
In response to (2), what wasn't explained is that the HttpContext.Current.Application.GetContainer() method is actually an extension method, which is implemented like the code shown here
.
To use this extension method, you simply have to import the 'Unity.Web' namespace.
Here is a copy of the extension method:
using System.Web;
using Microsoft.Practices.Unity;
namespace Unity.Web
{
public static class HttpApplicationStateExtensions
{
private const string GlobalContainerKey = "EntLibContainer";
public static IUnityContainer GetContainer(this HttpApplicationState appState)
{
appState.Lock();
try
{
var myContainer = appState[GlobalContainerKey] as IUnityContainer;
if (myContainer == null)
{
myContainer = new UnityContainer();
appState[GlobalContainerKey] = myContainer;
}
return myContainer;
}
finally
{
appState.UnLock();
}
}
}
}
With regards to the the dependency injection module code, I actually just used the basic method for getting an instance of the container, which as far as I'm concerned works just as well. The documentation says that the dependency injection HTTP module code improves 'testability' and 'discoverability', which is a little vague.
Anyway, here's the code for the basic approach:
protected void Application_Start(object sender, EventArgs e)
{
Application.Lock();
try
{
var myContainer = Application["EntLibContainer"] as IUnityContainer;
if (myContainer == null)
{
myContainer = new UnityContainer();
myContainer.AddExtension(new EnterpriseLibraryCoreExtension());
// Add your own custom registrations and mappings here as required
Application["EntLibContainer"] = myContainer;
}
}
finally
{
Application.UnLock();
}
}
So with the extension code in place, and code in my global.asax file to create an instance of the Enterprise Library container, the only thing left to do is write the code to get an instance of the container as needed. So when I want to get an instance of the LogWriter class, I would write this:
using Unity.Web;
public LogWriter getLogWriter()
{
var container = HttpContext.Current.Application.GetContainer();
return container.Resolve<LogWriter>();
}
The Unity.Web namespace is needed to allow us to invoke the GetContainer() extension method.