Introducing Dragonfly – another .NET HTTP server
dragonfly, internet, opensource, OWIN, programming, tech January 24th, 2012Two posts in a month! Hard to believe.
This one is to introduce Dragonfly – which is another 100% C# HTTP server. It’s designed from the ground up to be an OWIN host, of course, and is entirely non-blocking. This is similar to other open source managed web servers like Kayak and to some extent others like Manos.
Why start another C# HTTP server? Well, to some extent this started as an exercise to have another test harness to help troubleshoot and validate Gate and OWIN, rather than as a functional HTTP server of its own. It has from that grown into something that I felt had enough value to share as a standalone project.
So! To try this in a console project:
PM> Install-Package Dragonfly Attempting to resolve dependency 'Gate.Owin (≥ 0.2.1)'. Successfully installed 'Gate.Owin 0.2.1'. Successfully installed 'Dragonfly 0.2'. PM> Install-Package Gate Attempting to resolve dependency 'Gate.Owin (≥ 0.2.1)'. Successfully installed 'Gate 0.2.1'.
And here’s a little sample.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Gate.Owin;
namespace DragonflySample
{
class Program
{
static void Main(string[] args)
{
// Sinking the trace is optional, just wanted to show it
var server = new Dragonfly.Http.ServerFactory(new ConsoleTrace());
// Create takes an OWIN AppDelegate and port or endpoint.
// It returns an IDisposable.
using (server.Create(App, 8080))
{
Console.WriteLine("Running at http://localhost:8080/");
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
}
class ConsoleTrace : Dragonfly.Utils.IServerTrace
{
public void Event(TraceEventType type, Dragonfly.Utils.TraceMessage message)
{
Console.WriteLine("[{0} {1}]", type, message);
}
}
static void App(
IDictionary<string, object> env,
ResultDelegate result,
Action<Exception> fault)
{
// some classes in Gate.dll make using OWIN directly a bit easier
// but in practice you'll probably use a web framework rather than
// write directly like this
var request = new Gate.Request(env);
var response = new Gate.Response(result);
response.Headers["X-Foo"] = new[] { "Bar" };
response.ContentType = "text/html";
response
.Write("<html>")
.Write("<head><title>Hello Dragonfly</title></head>")
.Write("<body>")
.Write("<h1>Hello Dragonfly</h1>")
.Write("<p>You are looking at ")
.Write(request.Path) // NOTE: this is not url encoded!!!
.Write("</p> <!-- thanks, Nick -->")
.Write("</body>")
.Write("</html>");
response.Finish();
}
}
}
January 24th, 2012 at 6:41 am
Could you point to some more elaborate articles/tutorials on
Gate and OWIN and then I believe I can start understanding and having fun with Dragon :)… Keep it up….Cheers
January 24th, 2012 at 10:57 am
Nice! Very clean and clear API.. BTW the example is missing a closing paragraph tag :) cheers!
January 24th, 2012 at 11:38 am
@Bilal Gate and OWIN don’t have a large body of work behind them yet (still pre-1.0). Starting with the links at http://owin.org#discuss might be your best bet.
@Nickolas Thanks! And D’oh! This is why people like the haml family of view engines.
January 24th, 2012 at 11:40 am
Is it just me, or is this blog’s Submit Comment button white-on-white on Chrome?
January 24th, 2012 at 11:43 am
Just you I think – looks fine (blue-on-white) here.
January 25th, 2012 at 3:28 am
[...] Introducing Dragonfly – another .NET HTTP server – Louis DeJardin introduces a new .NET HTTP Server implementation which provides the OWIN functionality to allow it to act as an application host. The intention of this projects is to provide another reference implementation to validate OWIN and the Gate implementation. [...]
January 27th, 2012 at 9:13 am
Hi Luis!
Dam, have not been keeping up with OWIN/Gate, looking through the source code now, that is some crazy delegate magic! Makes my head hurt trying to understand all the chaining of delegates. But very cool!
What is RewindableBody good for?
January 30th, 2012 at 5:46 pm
Hi, I’ve just tried Dragonfly on Mono and it works flawlessly! Fantastic job. However going through the build process at Linux is currently a pain, due to Xbuild inefficiencies and things like backslashes in paths. But I believe you really, really want to use FAKE. Nevertheless the server itself works fine.
February 1st, 2012 at 12:17 am
[...] Introducing Dragonfly – another .NET HTTP server [...]
February 12th, 2012 at 10:27 pm
@Torkel Hello again! Yeah, I love functional programming and the density closures can give you. An inline delegate and local variables can do as much for you in five lines of code as another entire class would… But it does impact your readability! And I have has some feedback about having more comments would be nice. :)
@Xyro I’m glad to hear it worked on Mono. Didn’t test it myself, but it’s only using basic Socket APIs and simple byte array juggling so that makes sense. I do kind of like FAKE in that you don’t need to install another language runtime to do the builds – though if there’s a way to support simpler cross-platform build I’d love to hear it.
February 21st, 2013 at 6:08 pm
Hi, I’ve just tried Dragonfly on Mono and it works flawlessly
February 27th, 2013 at 4:43 pm
Nice! Very clean and clear API – all ok!!