John Lam 1997 — 2026

Hosting ASP.NET in a WinForms app

This entry doesn’t have anything to do with aspect-oriented programming. I’m on my way back home after teaching my ASP.NET class to the nice folks at UNUM Provident in Portland, Maine. After missing my connecting flight at LaGuardia, I’m sitting in the United Lounge writing this piece on my experiences with hosting ASP.NET inside of a WinForms application.

Before I begin, let me explain why I’m interested in hosting ASP.NET inside of a WinForms app. One of the things that I like to do (when I have the energy) is to go out and take pictures of the area around where I happen to be teaching a class. I’ve got a fair collection of digital photographs from lots of different places, and I’d like to publish them on this web site. However, what has always frustrated me was the difficulty in creating what amounts to a photo essay using traditional web logging tools such as the one that I’m using to create this entry.

I’ve been itching to write a WinForms app, and I’ve been itching to learn the syntax of VB.NET, so I decided to write an app that will let me create photo essays. The concept is real simple: let a user select a bunch of digital photographs, let them annotate those photographs with text, and let them publish the generated photo essay to a web server as a collection of static HTML pages and images. There are several other requirements that I have for my application:

  • It will automatically resize my pictures and generate thumbnails, as well as several other differently sized versions of the same picture.
  • It will let me publish my photo essay using arbitrary templates written using ASP.NET.
  • It will save the photo essay in a MSDE database.

Hosting ASP.NET turns out to be ridiculously simple. At first, I first tried to do it the hard way: I derived my own class from System.Web.HttpWorkerRequest. This led to all sorts of frustration. Thankfully, while I was teaching my class up in Portland, Fritz Onion and I hung out one evening after the MaineBytes user group meeting that Fritz spoke at. Fritz pointed that I really should be using System.Web.Hosting.SimpleWorkerRequest in my hosting scenario. He also sent along some sample code that illustrated this. Wow. That changed everything. In 5 minutes I had ASP.NET hosted inside of my application.

Here’s the sample code that Fritz sent along:

`using System; using System.IO; using System.Web; using System.Web.Hosting;

public class MyExeHost : MarshalByRefObject { public void ProcessRequest(String page) { HttpWorkerRequest hwr = new SimpleWorkerRequest(page, null, Console.Out); HttpRuntime.ProcessRequest(hwr); } }

public class App { public static void Main(string[] args) { MyExeHost host = (MyExeHost) ApplicationHost.CreateApplicationHost(typeof(MyExeHost), “/”, Directory.GetCurrentDirectory()); foreach (String page in args) { host.ProcessRequest(page); } } }`

While Fritz’s code got me most of the way to my solution, one other thing that I needed to do was to have my ASP.NET host add some additional objects to the HttpContext. One of the objects that I wanted to add was a DataSet object that would contain the list of images and their associated text. This would let the ASP.NET page databind against that DataSet to generate the final list / set of images.

To make this work, I need to intercept the creation of the HttpContext object and add my DataSet object to the HttpContext object. This required me to derive a custom handler from the System.Web.SimpleWorkerRequest class. The key method that I need to override was the SetEndOfSendNotification method. In this method, the extraData parameter will contain the newly created HttpContext object. Here’s the code that makes this possible:

`public class CustomWorkerRequest : SimpleWorkerRequest { public CustomWorkerRequest( string page, string query, TextWriter writer ) : base( page, query, writer ) {}

public override void SetEndOfSendNotification( EndOfSendNotification callback, object extraData ) { base.SetEndOfSendNotification( callback, extraData );

HttpContext context = extraData as HttpContext;
if( context != null )
{
  // TODO: add a reference to a DataSet object here that would contain
  // the data to be injected into the web page. The web page will databind
  // against this DataSet to generate the photoesssay HTML
}

} }`

Hopefully this helps anyone else walking down this path. Feel free to send me some email if you have comments / questions / suggestions.