/// <summary>
/// Interface for components that wish to be started by the container
/// </summary>
public interface IStartable
{
/// Starts this instance.
void Start();
/// Stops this instance.
void Stop();
}
public class LameHttpFileServer : IStartable
private string _path;
private HttpListener _listener;
private Thread _listenerThread;
private ILogger _logger;
public LameHttpFileServer(string prefix, string path)
_path = path;
_listener = new HttpListener();
_listener.Prefixes.Add(prefix);
public ILogger Logger
get
if (_logger == null) _logger = NullLogger.Instance;
return _logger;
set { _logger = value; }
public void Start()
Logger.Debug("Starting LameWebService...");
_listener.Start();
_listenerThread = new Thread(RequestWorker);
_listenerThread.Start();
Logger.Info("LameWebService Started.");
public void Stop()
Logger.Debug("Stopping LameWebService...");
_listenerThread.Abort();
_listener.Stop();
Logger.Info("Stopped LameWebService.");
private void RequestWorker()
while (true)
HttpListenerContext context = null;
try
context = _listener.GetContext();
catch (ObjectDisposedException)
return;
ServeContent(context.Response, _path);
private void ServeContent(HttpListenerResponse response, string path)
string content = File.ReadAllText(path);
byte[] buffer = Encoding.UTF8.GetBytes(content);
response.ContentLength64 = buffer.Length;
Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
output.Close();
<configuration>
<configSections>
<section name="castle"
type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</configSections>
<castle>
<facilities>
<facility id="startable.facility"
type="Castle.Facilities.Startable.StartableFacility, Castle.MicroKernel" />
<facility id="logging.facility"
type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging"
loggingApi="console" />
</facilities>
<components>
<component id="serves->lame.html"
type="IoC.Tutorials.Part14.LameHttpFileServer, IoC.Tutorials.Part14">
<parameters>
<prefix>http://*:8089/</prefix>
<path>lame.html</path>
</parameters>
</component>
</components>
</castle>
</configuration>
<html>
<body>
<h1>Lame.html</h1>
<h2>Welcome to lame.html file</h2>
<p>This is the contents of the lame.html file, neat huh?</p>
</body>
</html>
private static void Main(string[] args)
WindsorContainer container = new WindsorContainer(new XmlInterpreter());
GetPage();
container.Dispose();
Console.Read();
private static void GetPage()
Console.WriteLine("\r\nClient: requesting http://localhost:8089/ ...");
WebClient client = new WebClient();
string content = client.DownloadString("http://localhost:8089/");
Console.WriteLine("Client: success, content follows.\r\n\r\n {0}", content);
catch (WebException ex)
Console.WriteLine("Client: Exception occured, message: {0}", ex.Message);
LameHttpFileServer server = container.Resolve<LameHttpFileServer>();
container.Release(server);
[Transient]
public class StartableExperiment : IStartable
private static int _count;
private int _number;
public StartableExperiment()
_number = ++_count;
Console.WriteLine("Started #{0}", _number);
Console.WriteLine("Stopped #{0}", _number);
container.AddComponent("exp", typeof(StartableExperiment));
StartableExperiment exp1 = container.Resolve<StartableExperiment>();
container.Release(exp1);
StartableExperiment exp2 = container.Resolve<StartableExperiment>();
container.Release(exp2);