Skip to content

Instantly share code, notes, and snippets.

@jchadwick
Created May 16, 2012 19:24
Show Gist options
  • Save jchadwick/2713242 to your computer and use it in GitHub Desktop.
Save jchadwick/2713242 to your computer and use it in GitHub Desktop.
In-Memory ASP.NET Website
using System;
using System.Web;
using System.Web.Hosting;
using System.IO;
public class InMemoryAspNet
{
public string PhysicalDirectory { get; set; }
public InMemoryAspNet()
{
PhysicalDirectory = Environment.CurrentDirectory;
}
public string ProcessRequest(string path, string query = null)
{
using (var stream = new StringWriter())
{
ProcessRequest(stream, path, query);
stream.Flush();
return stream.GetStringBuilder().ToString();
}
}
public void ProcessRequest(TextWriter stream, string path, string query = null)
{
var host = (AspNetHost)ApplicationHost.CreateApplicationHost(typeof(AspNetHost), "/", PhysicalDirectory);
host.ProcessRequest(stream, path, query);
}
class AspNetHost : MarshalByRefObject
{
public void ProcessRequest(TextWriter stream, string path, string query = null)
{
var request = new SimpleWorkerRequest(path, query, stream);
HttpRuntime.ProcessRequest(request);
}
public override object InitializeLifetimeService()
{
// This tells the CLR not to surreptitiously
// destroy this object -- it's a singleton
// and will live for the life of the appdomain
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment