Fos (fastcgi owin服务器)
a .NET 4/MONO兼容Fastcgi Owin服务器编写了C#。这意味着它可以处理Nginx或IIS等Web服务器的FASTCGI请求,将其传递给OWIN管道并回复Web服务器。这取决于我的GitHub存储库之一FastCginet库。
用法
该软件是用于自托管的库。这意味着您应该在OWIN Web应用程序中添加对其的参考,并以此为单位:
Fos;
using Fos .Owin;
using Owin;
…
…
private static Fos SelfHost Fos Server;
public static void Main(string[] args)
{
using ( Fos Server = new Fos SelfHost(applicationRegistration))
{
// Bind on 127.0.0.1, port 9000
Fos Server.Bind(System.Net.IPAddress.Loopback, 9000);
// If you\’re on *nix and like unix sockets
Fos Server.Bind(\”/tmp/fcgisocket.sock\”);
// Start the server.
Fos Server.Start(false);
}
}
static void applicationRegistration(IAppBuilder builder)
{
// To log statistics get access to the pretty statistics page, you need to create a shunt, like this:
var statisticsPipeline = builder.New();
// Here you would add statistics authentication middleware. One example would be only allowing connections from localhost or from admins
// statisticsPipeline.Use<MyStatisticsPageAuthenticationMiddleware>();
statisticsPipeline.UseStatisticsLogging( Fos Server, new TimeSpan(0, 30, 0));
// Will shunt to \”statisticsPipeline\” if request is to \”/_stats\”
var statisticsMapping = new Dictionary<string, IAppBuilder>() { { \”/_stats\”, statisticsPipeline } };
builder.Use<ShuntMiddleware>(statisticsMapping);
// This is how you register your application\’s middleware. This is typically one of your Owin compatible Web frameworks
builder.Use(typeof(MyApplicationType));
}\”>
using Fos ; using Fos . Owin ; using Owin ; .. . .. . private static Fos SelfHost Fos Server ; public static void Main ( string [ ] args ) { using ( Fos Server = new Fos SelfHost ( applicationRegistration ) ) { // Bind on 127.0.0.1, port 9000 Fos Server . Bind ( System . Net . IPAddress . Loopback , 9000 ) ; // If you\'re on *nix and like unix sockets Fos Server . Bind ( \"/tmp/fcgisocket.sock\" ) ; // Start the server. Fos Server . Start ( false ) ; } } static void applicationRegistration ( IAppBuilder builder ) { // To log statistics get access to the pretty statistics page, you need to create a shunt, like this: var statisticsPipeline = builder . New ( ) ; // Here you would add statistics authentication middleware. One example would be only allowing connections from localhost or from admins // statisticsPipeline.Use<MyStatisticsPageAuthenticationMiddleware>(); statisticsPipeline . UseStatisticsLogging ( Fos Server , new TimeSpan ( 0 , 30 , 0 ) ) ; // Will shunt to \"statisticsPipeline\" if request is to \"/_stats\" var statisticsMapping = new Dictionary < string , IAppBuilder > ( ) { { \"/_stats\" , statisticsPipeline } } ; builder . Use < ShuntMiddleware > ( statisticsMapping ) ; // This is how you register your application\'s middleware. This is typically one of your Owin compatible Web frameworks builder . Use ( typeof ( MyApplicationType ) ) ; }
有关UNIX插座的注意:Nuget的Fos具有一个错误,它将在用Mono绑定到Unix插座时,将其抛出NullReferenceException 。这是在主机中修复的。另外, Fos不会在套接字文件中为您设置权限,而是在停止时会删除套接字文件,因此请确保所有权限均已设置。
建筑学
Fos由一个主要循环组成,它在该循环中处理了已建立的连接的新连接和传入数据。此循环仅在不阻止的情况下才能运行同步套接字操作。从本质上讲,这是一个异步循环。循环很好,花花公子。实际上,鉴于在处理过多的连接时,希望在处理太多连接时保存系统资源的服务器应用程序使用此循环模型,因为在这种情况下,单连接模型可以快速食用内存并消耗CPU。但是,异步循环并不是世界上饥饿的解决方案,因为它为用户增加了负担:用户现在应该注意不要在任何耗时的操作中阻止它。为了用一个示例说明这一点,假设您有一个由网站用户触发的操作,该操作将数千行插入数据库的表中。如果您不偏移此单位工作单位(数据库插入数千行)到另一个线程,则一个用户可以阻止Fos的主循环,直到您的操作完成为止,而其他访问者将无法完成该工作时访问您的网站。如果您正在编写C#5,请记住使用async并await ,或者使用您的语言正确地将工作正确地抵消到另一个线程中,如果该工作可能需要一些时间才能完成。
安装
您可以通过Nuget安装Fos ,也可以手动构建它以进行最新的错误修复和功能。
构建指令:克隆此存储库,然后在Monodevelop(实际上是我使用的开发IDE)或Visual Studio中打开此解决方案,然后构建。您会在bin文件夹中找到二进制Fos .dll。是的,这很简单。
错误处理和记录
您可以定义一个记录仪,该记录仪在处理FastCGI服务器和其他操作的连接时内部使用。您只需要实现Fos .logging.iserverlogger界面,然后使用SetLogger方法将您的实例注册到您的Fos Self -Bhost实例。请注意:
- 您对Iserverlogger的实施不得抛出任何例外。您的实现将与服务器本身并肩作用,如果引发异常,则将崩溃服务器。如果不确定,请确保添加一个
try .. catch实现的每种方法。 - 如果设置了实现IDIsposable的记录器,则将服务器处置时将其处置。
- 如果应用程序抛出异常, Fos将其显示给访问者。如果您不希望显示异常,请添加中间件,该中间件将在管道中首先处理异常。
您还可以设置一个自定义的内部记录仪,该记录仪带有Fos 。此记录器记录访问统计信息,应用程序抛出的例外,并提供一个页面,将它们列出在一个不错的/简单的页面中。当然,如果此页面不适合您,您可以自由地实现自己的统计记录仪。此自定义内部记录仪仅用于实际目的。如果要设置它,请查看主要示例。显示错误和统计信息的页面需要注意。也许你可以帮忙?
当前状态和警告
当前,该服务器似乎与NancyFX和Simple.Web兼容(我不确定它是否与其他框架兼容,Owin为实施者留下了一些不良的空间)。要使用这些,您只需要调用builder.usenancy()或builder.useimpleweb()(使用了适当的命名空间,例如nancy.owin或simple.web.owinsupport)。也就是说,请注意:
- 我只使用了Nginx的Fos ,它似乎可以完美地运行并处理许多请求而不会破坏汗水。尽管非常简单,但我对其进行了测试的应用程序进行了很多数据库工作,并且许多异步/等待套接字操作,而Fos甚至没有以任何方式失败。另外,如果您不使用不安全的代码冒险(并且不要使用自定义记录仪), Fos实际上可以保证不停止工作,因此请尝试一下!
- API不能保证一段时间保持稳定,很可能不会。但是,没有预期的重大变化,并且由于没有太大的破裂空间,因此API更改可能只需5分钟即可使您的代码适应。
FastCGI参数
托多
到应用程序构建者
如果您打算构建OWIN兼容应用程序并使用此服务器运行,请注意以下内容:
- 到目前为止,当且仅当关闭请求的FastCGI插座时,键“ Owin.callcancelled”发现的OWIN concellationToken发出了信号
非标准扩展:
- Fos的IAPPBUILDER实现将tocellationToken添加到属性字典中,该字典在服务器停止和/或处置时发出了信号。您可以通过密钥“ host.onappdisposing”到达这个令牌
目标
该项目的目标是为我们提供一种为我们的Web应用程序提供服务的方法:启用了您选择的强大FASTCGI网络服务器(NGINX,IIS,APACHE,LIGHTTPD等),C#或任何其他编译CIL和任何MONO兼容操作系统的语言或任何其他语言。这也是刺激OWIN和.NET OSS生态系统的尝试,因为Mono上的ASP.NET可能不会在将来受到过多的关注,并且由于它看起来像ASP.NET的更好,更开放的替代方案。
