LiteServer
is a lightweight HTTP web server that can be used instead of Kestrel. It can be used in .net core
and .net framework
console applications in order to add endpoints and request handling pipeline to them. It is possible to add custom middlewares in the pipeline to process the Http request also.
The library targets .NET Standard 2.0 which supports the following runtimes (and newer):
- .Net Framework 4.6.1
- .NET Core 2.0
this library allows you to add a light-weight Http server and a custom pipeline and routing into your project. It has also some extensions that makes it easy to configure the LiteServer during the GenericHostBuilder configuration in the program class, in .net core projects.
Install-Package LiteServer
Install-Package LiteServer.Extensions.Hosting
After installing the library, you should:
- Initialize the
LiteServerBuilder
and give it the customized configuration. - Create the LiteServer by calling the
Build
method on theLiteServerBuilder
- Start the
LiteServer
by calling theStartAsync
and start listening to http requests.
the following is a simple implementation that shows how to configure LiteServer
in a .net framework console application.
class Program
{
static void Main(string[] args)
{
var liteServerBuilder = new LiteServerBuilder();
var pathConfigurations = new List<Action<BasePathBuilder>>()
{
builder => builder
.WithHostName("localhost")
.WithPort(5000)
};
var liteHost = liteServerBuilder.Configure(builder =>
{
builder.Use(FirstMiddleware) //here we defined the middleware pipeline
.Use(SecondMiddleware);
builder.UseEndpoints(routeBuilder => // here we defined routing
{
routeBuilder.Map("/Hello", context => context.Response.WriteAsync("Hello world"));
});
})
.UseHttpListenerServer(pathConfigurations)
.Build();
liteHost.StartAsync();
Console.ReadKey();
}
static HandlerDelegate FirstMiddleware(HandlerDelegate next)
{
return async context =>
{
await context.Response.WriteAsync("first middleware says hello! =>");
await next(context);
};
}
static HandlerDelegate SecondMiddleware(HandlerDelegate next)
{
return async context =>
{
await context.Response.WriteAsync("second middleware says hello =>");
await next(context);
};
}
}
- Create Microsoft Generic Host by calling the
CreateDefaultBuilder
onHost
class. - configure the
LiteServer
by usingConfigureLiteServerDefaults
extension method onIHostBuilder
.
the following is a simple implementation for .net core projects:
class Program
{
static void Main(string[] args)
{
try
{
var host = Host.CreateDefaultBuilder(args)
.ConfigureLiteServerDefaults(builder =>
{
builder.Use(FirstMiddleware) //here we defined the middleware pipeline
.Use(SecondMiddleware);
builder.UseEndpoints(routeBuilder => // her we defined the routing and endpoints
{
routeBuilder.Map("/Health", context => context.Response.WriteAsync("I am healthy"));
});
}, "http://localhost:5001/")
.Build();
host.Run();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
static HandlerDelegate FirstMiddleware(HandlerDelegate next)
{
return async context =>
{
await context.Response.WriteAsync("first middleware says hello! =>");
await next(context);
};
}
static HandlerDelegate SecondMiddleware(HandlerDelegate next)
{
return async context =>
{
await context.Response.WriteAsync("second middleware says hello =>");
await next(context);
};
}
}