TinyWeb: A Simple Web Server in C#

Updated on 2019-09-11

A simple home HTTP server that works across all .NET platforms

Note: This project only serves single requests at once. Try this one instead. It's better: Tiny Web Server Take 2

Tiny Web Server Take 2

Introduction

Currently, the offerings for .NET enabled web servers are platform dependent, and large, intended for scalable sites. Often, they have reams of features and lots of configuration. The simplest offering, http.sys, only works on windows.

The purpose of this web server is to expose a small, limited scalability server for something like a home network, or another scenario where the connection frequency is relatively low, and users are trusted.

It works on any .NET platform, and its component based API is simple to operate. Just drag it onto a form or a service component, wire up the events, and set the properties. Aside from the local endpoint to listen on, there is zero configuration.

Using the Code

Using the code is fairly simple:

using TinyWeb;
using System.Net;
...
var webServer = new WebServer();
webServer.ProcessRequest += new ProcessRequestEventHandler(webServer_ProcessRequest);
webServer.EndPoint = new IPEndPoint(IPAddress.Any,8080);
webServer.IsStarted = true;
...
void webServer_ProcessRequest(object sender, ProcessRequestEventArgs args)
{
    var r = args.Response;
    r.ContentType = "text/html";
    r.WriteLine("<html><h1>Hello World!</h1></html>");
}

Request has methods for getting the headers, the querystring, and the request/post stream. Response has methods for setting the response headers, and for writing the response stream.

Points of Interest

SocketUtility contains many other methods for working with sockets, especially asynchronous socket communication, including exposing awaitable methods for the primary socket operations.

History

  • 28th August, 2019 - Initial submission