Skip to content

Instantly share code, notes, and snippets.

@moodmosaic
Created May 11, 2012 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moodmosaic/2658773 to your computer and use it in GitHub Desktop.
Save moodmosaic/2658773 to your computer and use it in GitHub Desktop.
Draft: Minimum code required to initialize Windsor and return a new HttpSelfHostServer instance. (Work in-progress.)
private static HttpSelfHostServer BuildHttpSelfHostServer(string baseAddress)
{
var configuration = new HttpSelfHostConfiguration(baseAddress);
configuration.DependencyResolver =
new WindsorDependencyResolver(
new WindsorContainer()
.Install(new WebWindsorInstaller()));
configuration.Routes.MapHttpRoute(
"DefaultApi",
"api/{controller}/{id}",
new
{
id = RouteParameter.Optional,
namespaces = new[] { typeof(ValuesController).Namespace }
});
return new HttpSelfHostServer(configuration);
}
private static void Main()
{
const string baseAddress = "http://localhost:8080/";
BuildHttpSelfHostServer(baseAddress)
.OpenAsync();
Array.ForEach(
new[]
{
"Listening directly to HTTP at " + baseAddress,
null,
"Press any key to exit ..."
},
Console.WriteLine);
Console.ReadKey();
}
internal class WebWindsorInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Component
.For<..>()
.ImplementedBy<..>()
.LifestyleScoped());
// ...
}
}
internal sealed class WindsorDependencyResolver : IDependencyResolver
{
private readonly IWindsorContainer container;
public WindsorDependencyResolver(IWindsorContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
}
public object GetService(Type t)
{
return this.container.Kernel.HasComponent(t)
? this.container.Resolve(t) : null;
}
public IEnumerable<object> GetServices(Type t)
{
return this.container.ResolveAll(t).Cast<object>().ToArray();
}
public IDependencyScope BeginScope()
{
return new WindsorDependencyScope(this.container);
}
public void Dispose()
{
}
}
internal sealed class WindsorDependencyScope : IDependencyScope
{
private readonly IWindsorContainer container;
private readonly IDisposable scope;
public WindsorDependencyScope(IWindsorContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
this.scope = container.BeginScope();
}
public object GetService(Type t)
{
return this.container.Kernel.HasComponent(t)
? this.container.Resolve(t) : null;
}
public IEnumerable<object> GetServices(Type t)
{
return this.container.ResolveAll(t).Cast<object>().ToArray();
}
public void Dispose()
{
this.scope.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment