Skip to content

Instantly share code, notes, and snippets.

@lkaczanowski
Created June 22, 2017 14:01
Show Gist options
  • Save lkaczanowski/db303cf0060bac0b98558c93151adfe7 to your computer and use it in GitHub Desktop.
Save lkaczanowski/db303cf0060bac0b98558c93151adfe7 to your computer and use it in GitHub Desktop.
Extensions for Castle Windstor ie: verifying registration of dependencies
using System;
using System.Linq;
using System.Text;
using Castle.MicroKernel;
using Castle.MicroKernel.Handlers;
using Castle.Windsor;
using Castle.Windsor.Diagnostics;
namespace Extensions
{
public static class WindsorContainerExtensions
{
public static void VerifyInstallationIsValid(this IWindsorContainer container)
{
var host = (IDiagnosticsHost)container.Kernel.GetSubSystem(SubSystemConstants.DiagnosticsKey);
var diagnostics = host.GetDiagnostic<IPotentiallyMisconfiguredComponentsDiagnostic>();
var handlers = diagnostics.Inspect();
if (handlers.Any() == false)
{
return;
}
var message = new StringBuilder();
message.AppendLine("Castle Windsor did not register all dependencies properly!");
var inspector = new DependencyInspector(message);
foreach (var handler in handlers)
{
((IExposeDependencyInfo)handler).ObtainDependencyDetails(inspector);
}
throw new InvalidOperationException(message.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment