Two 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();
        }
    }
}