EmbedIO is a small, cross-platform server for .Net Framework and .Net Core that can be added to projects that need to handle HTTP requests without the need for a full web server.
EmbedIO is written entirely in C#, and its development is focused on achieving high efficiency with low memory consumption. All operations are available in an asynchronous version.
It is distributed under the MIT license, and all code and documentation is available at https://github.com/unosquare/embedio. EmbedIO is compatible with .NET for Windows, Mono, and Xamarin.
Installing EmbedIO
The EmbedIO library is distributed as a NuGet package, so adding it to our project is as simple as doing:
Install-Package EmbedIO
Example of Using EmbedIO
Let’s create a small example to illustrate the use of EmbedIO. To do this, we create a simple console application and replace the code with the following.
internal static class Program
{
[STAThread]
private static void Main()
{
var url = "http://localhost:9696/";
using (WebServer server = CreateWebServer(url))
{
server.RunAsync();
}
Console.ReadKey(true);
}
// Create and configure our web server.
private static WebServer CreateWebServer(string url)
{
WebServer server = new WebServer(o => o
.WithUrlPrefix(url)
.WithMode(HttpListenerMode.EmbedIO))
.WithLocalSessionManager()
.WithAction("/test", HttpVerbs.Any, ctx =>
{
Console.WriteLine("Request received");
return ctx.SendDataAsync(new { mensaje = "Hola mundo"});
}
);
// Listen for state changes.
server.StateChanged += (s, e) => $"WebServer New State - {e.NewState}".Info();
return server;
}
}
That’s how easy it is to create an Endpoint for the URL http://localhost:9696/test, which returns a Json file with “Hello world”, and logs to the console that it has received a request.
It’s also very easy to create controllers and associate them to create an API. For example,
internal class Program
{
static async Task Main(string[] args)
{
var url = "http://localhost:9696/";
var server = CreateWebServer(url);
await server.RunAsync();
}
private static WebServer CreateWebServer(string url)
{
WebServer server = new WebServer(o => o
.WithUrlPrefix(url)
.WithMode(HttpListenerMode.EmbedIO))
.WithLocalSessionManager()
.WithWebApi("/api", m => m.WithController<HelloController>());
// Listen for state changes.
server.StateChanged += (s, e) => $"WebServer New State - {e.NewState}".Info();
return server;
}
}
public class HelloController : WebApiController
{
[Route(HttpVerbs.Get, "/hello")]
public string GetGreeting() => "Hola, mundo!";
}
In a real example, of course, the server would perform the necessary actions, and the data returned would be, for example, loaded from a data source.
Of course, this is just a small example of what EmbedIO can do. It can also serve static pages, manage user sessions, use Websockets. Furthermore, it includes support for CORS.
Additionally, it has an interesting module for easily configuring a complete REST API. You can literally generate a REST API in minutes.
Finally, it is extensible. Its modular design allows you to add your own modules, for example, to configure middleware.
In summary, a very interesting library that, while it may not fit all projects, can be useful in those Apps that require handling HTTP requests in a simple and efficient way.

