From 728ba69fc6798091a5cd552d5b221ec188d6061d Mon Sep 17 00:00:00 2001 From: Luigi Grilli Date: Mon, 17 Nov 2014 22:56:36 +0000 Subject: [PATCH] Added KeepConnection option Added KeepConnection option to fastcgi asp.net server command. This option can be found and set to true/false in the app.config file --- FastCgi.AspNet/FastCgiAspNetServer.cs | 19 ++++++++++++------- FastCgi.Server/Program.cs | 14 +++++++++++++- FastCgi.Server/app.config | 1 + 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/FastCgi.AspNet/FastCgiAspNetServer.cs b/FastCgi.AspNet/FastCgiAspNetServer.cs index a0fb8e4..c2efbc9 100644 --- a/FastCgi.AspNet/FastCgiAspNetServer.cs +++ b/FastCgi.AspNet/FastCgiAspNetServer.cs @@ -19,11 +19,12 @@ public class FastCgiAspNetServer : MarshalByRefObject /// Tcp/ip port where the server will be listening for fastcgi requests /// Virtual path of your ASP.NET application /// Physical path of your ASP.NET application + /// When enabling this option, it will be responsability of the web server to close the connection. Default is false. /// - public static FastCgiAspNetServer CreateApplicationHost(int port, string virtualPath, string physicalPath) + public static FastCgiAspNetServer CreateApplicationHost(int port, string virtualPath, string physicalPath, bool keepConnection) { var ret = (FastCgiAspNetServer)ApplicationHost.CreateApplicationHost(typeof(FastCgiAspNetServer), virtualPath, physicalPath); - ret.Initialize(port, virtualPath, physicalPath); + ret.Initialize(port, virtualPath, physicalPath, keepConnection); return ret; } @@ -32,9 +33,9 @@ public override object InitializeLifetimeService() return null; //never expire lease } - internal void Initialize(int port, string virtualPath, string physicalPath) + internal void Initialize(int port, string virtualPath, string physicalPath, bool keepConnection) { - _server = new ServerInternal(port, virtualPath, physicalPath); + _server = new ServerInternal(port, virtualPath, physicalPath, keepConnection); } /// @@ -56,17 +57,20 @@ public void Stop() internal class ServerInternal : TcpServer { - internal ServerInternal(int port, string virtualPath, string physicalPath) + internal ServerInternal(int port, string virtualPath, string physicalPath, bool keepConnection) : base(port) { this.VirtualPath = virtualPath; this.PhysicalPath = physicalPath; + this.KeepConnection = keepConnection; } public string VirtualPath { get; private set; } public string PhysicalPath { get; private set; } + public bool KeepConnection { get; private set; } + protected override IChannel CreateChannel(TcpLayer tcpLayer) { return new CustomAspNetChannelStack(tcpLayer, this); @@ -77,7 +81,7 @@ private class CustomAspNetChannelStack : IChannel private readonly ServerInternal _server; private readonly TcpLayer _tcpLayer; - public CustomAspNetChannelStack(TcpLayer tcpLayer, ServerInternal server) + public CustomAspNetChannelStack(TcpLayer tcpLayer, ServerInternal server) { _server = server; _tcpLayer = tcpLayer; @@ -94,7 +98,8 @@ private CustomAspNetChannel CreateUpperLayer(TcpLayer tcpLayer) private void RequestEnded(object sender, EventArgs e) { - _tcpLayer.Close(); + if(!_server.KeepConnection) + _tcpLayer.Close(); } public void Run() diff --git a/FastCgi.Server/Program.cs b/FastCgi.Server/Program.cs index 112ac32..f751d30 100644 --- a/FastCgi.Server/Program.cs +++ b/FastCgi.Server/Program.cs @@ -40,9 +40,21 @@ private static int Port } } + private static bool KeepConnection + { + get + { + bool ret; + if (Boolean.TryParse(ConfigurationManager.AppSettings["KeepConnection"], out ret)) + return ret; + + return false; + } + } + static void Main(string[] args) { - var server = FastCgiAspNetServer.CreateApplicationHost(Port, VirtualPath, PhysicalPath); + var server = FastCgiAspNetServer.CreateApplicationHost(Port, VirtualPath, PhysicalPath, KeepConnection); server.Start(); Console.WriteLine ("Listening on port {0}", Port); diff --git a/FastCgi.Server/app.config b/FastCgi.Server/app.config index 277c69e..bf53c8a 100644 --- a/FastCgi.Server/app.config +++ b/FastCgi.Server/app.config @@ -4,6 +4,7 @@ +