C# fastcgi protocol implementation plus some usage examples. A good example on how to self-host your web application without the need of IIS or Mono.
The purpose of this implementation is to have a more reliable solution than the one offered with Mono and also a cleaner and reusable implementation of the protocol to host not only ASP.NET applications but also custom low level (and fast) applications, the ones that usually are implemented with an HttpListener. With this implementation is possible for example to host an ASP.NET application (also MVC) with and Nginx web-server on both Windows and Linux.
The easiest way to install FastCgi core library is via NuGet.
In Visual Studio's Package Manager Console, enter the following command:
Install-Package Grillisoft.FastCgi
To add logging using log4net:
Install-Package Grillisoft.FastCgi.Loggers.Log4Net
To host an aspnet website:
Install-Package Grillisoft.FastCgi.AspNet
You can run the first example/test following this procedure:
- Build the solution (Debug | Mixed Platforms)
- For Windows,
a. Create the folders.\Examples\windows-nginx-1.6.2\logs
and.\Examples\windows-nginx-1.6.2\temp
b. Run.\Examples\windows-nginx-1.6.2\nginx.exe
.
For Linux, run nginx with the configuration supplied with the Windows example. - Run
.\FastCgi.Server\bin\Debug\Grillisoft.FastCgi.Server.exe
. - In your browser, go to http://localhost:8082/info.aspx or http://localhost:8082/test.aspx.
sharpfastcgi now has an Owin-compatible ChannelFactory that allows you to run any Owin-compatible middleware as a FastCGI application. It has been tested with Microsoft WebAPI 5.2.3 and with the various Microsoft security middlewares for authentication to ADFS, Facebook, Twitter, etc.
A minimal example for IIS would look like this:
static class Program
{
static int Main(string[] args)
{
var logger = LoggerFactory.Create("Owin Test");
logger.Log(LogLevel.Info, "Starting fastcgi server");
var server = CreateServer();
server.Start();
while (true)
{
Thread.Sleep(1000);
}
}
private static IFastCgiServer CreateServer()
{
return new IisServer(new OwinChannelFactory(LoggerFactory, applicationRegistration), LoggerFactory);
}
private static ILoggerFactory _loggerFactory;
private static ILoggerFactory LoggerFactory
{
get
{
if (_loggerFactory != null)
return _loggerFactory;
return _loggerFactory = new Grillisoft.FastCgi.Loggers.Log4Net.LoggerFactory();
}
}
static void applicationRegistration(IAppBuilder app)
{
// Register your Owin middlewares here
// This example uses ADFS Bearer Auth and WebAPI
var configuration = new HttpConfiguration();
app.UseActiveDirectoryFederationServicesBearerAuthentication(
new ActiveDirectoryFederationServicesBearerAuthenticationOptions
{
MetadataEndpoint ="MyMetadataEndpoint",
TokenValidationParameters = new TokenValidationParameters()
{
ValidAudience = "MyAudience"
}
});
app.UseWebApi(configuration);
}
}
You can also read this article I wrote about this library so you can have a deeper understanding on how it works: http://www.codeproject.com/Articles/388040/FastCGI-NET-and-ASP-NET-self-hosting