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\logsand.\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:
staticclassProgram{staticintMain(string[]args){varlogger=LoggerFactory.Create("Owin Test");logger.Log(LogLevel.Info,"Starting fastcgi server");varserver=CreateServer();server.Start();while(true){Thread.Sleep(1000);}}privatestaticIFastCgiServerCreateServer(){returnnewIisServer(newOwinChannelFactory(LoggerFactory,applicationRegistration),LoggerFactory);}privatestaticILoggerFactory_loggerFactory;privatestaticILoggerFactoryLoggerFactory{get{if(_loggerFactory!=null)return_loggerFactory;return_loggerFactory=newGrillisoft.FastCgi.Loggers.Log4Net.LoggerFactory();}}staticvoidapplicationRegistration(IAppBuilderapp){// Register your Owin middlewares here// This example uses ADFS Bearer Auth and WebAPIvarconfiguration=newHttpConfiguration();app.UseActiveDirectoryFederationServicesBearerAuthentication(newActiveDirectoryFederationServicesBearerAuthenticationOptions{MetadataEndpoint="MyMetadataEndpoint",TokenValidationParameters=newTokenValidationParameters(){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