Created
May 11, 2012 10:19
-
-
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.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class WebWindsorInstaller : IWindsorInstaller | |
{ | |
public void Install(IWindsorContainer container, IConfigurationStore store) | |
{ | |
container.Register(Component | |
.For<..>() | |
.ImplementedBy<..>() | |
.LifestyleScoped()); | |
// ... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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