Switched to MEF to register http handlers

This commit is contained in:
LukePulverenti Luke Pulverenti luke pulverenti
2012-09-08 10:52:13 -04:00
parent a95e868300
commit 93b42641d2
26 changed files with 240 additions and 154 deletions

View File

@@ -1,5 +1,6 @@
using MediaBrowser.Common.Logging;
using MediaBrowser.Common.Net;
using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Common.Serialization;
using MediaBrowser.Model.Configuration;
@@ -35,12 +36,23 @@ namespace MediaBrowser.Common.Kernel
[ImportMany(typeof(BasePlugin))]
public IEnumerable<BasePlugin> Plugins { get; private set; }
/// <summary>
/// Gets the list of currently registered http handlers
/// </summary>
[ImportMany(typeof(BaseHandler))]
private IEnumerable<BaseHandler> HttpHandlers { get; set; }
/// <summary>
/// Both the UI and server will have a built-in HttpServer.
/// People will inevitably want remote control apps so it's needed in the UI too.
/// </summary>
public HttpServer HttpServer { get; private set; }
/// <summary>
/// This subscribes to HttpListener requests and finds the appropate BaseHandler to process it
/// </summary>
private IDisposable HttpListener { get; set; }
protected virtual string HttpServerUrlPrefix
{
get
@@ -186,6 +198,21 @@ namespace MediaBrowser.Common.Kernel
DisposeHttpServer();
HttpServer = new HttpServer(HttpServerUrlPrefix);
HttpListener = HttpServer.Subscribe((ctx) =>
{
BaseHandler handler = HttpHandlers.FirstOrDefault(h => h.HandlesRequest(ctx.Request));
// Find the appropiate http handler
if (handler != null)
{
// Need to create a new instance because handlers are currently stateful
handler = Activator.CreateInstance(handler.GetType()) as BaseHandler;
// No need to await this, despite the compiler warning
handler.ProcessRequest(ctx);
}
});
}
/// <summary>
@@ -249,6 +276,11 @@ namespace MediaBrowser.Common.Kernel
{
HttpServer.Dispose();
}
if (HttpListener != null)
{
HttpListener.Dispose();
}
}
/// <summary>