Tutorial 1 not working with "Lite" branch - Uri data being blown away

Sep 4, 2009 at 10:12 AM

Neat project!  I like the simplicity / "minimalist" approach.

I downloaded the source and pulled the HttpServer directory under the "Lite" branch into my project.  I'm trying to recreate the first sample application, but ran into a small hiccup.  In my onRequest handler, args.Request.Uri simply returns "http://localhost/".  The Request.UriPath property properly shows the path portion that was requested, but the .Uri property seems to just be an EmptyUri.  Note I am requesting the page through "".

I'm the first to admit that I don't have a good handle on what's going on under the hood here, but I did a bit of preliminary debugging / code stepping.  At first I see Uri properties being set correctly, but somewhere along the way inside the engine, HttpListener.ProcessRequest doesn't find any handlers and so calls FireRequestCallback(context, request, response, _notFoundHandler) which, in turn, calls request.Clear() and blows away all the lovely request data that was parsed.  Unfortunately, all this happens before the OnRequest event is fired, so by the time it gets to my code, I have no more Uri info!

I hope this makes sense to someone..

Is this a bug, or should the tutorial sample include a line of code to "register" the handler so that the Uri data doesn't get blown away?

Appreciate any help you can offer!


Sep 4, 2009 at 10:34 AM
Edited Sep 4, 2009 at 10:35 AM

Alright I think I've got it figured out.  Instead of using the OnRequest event, use AddHandler with a delegate.

i.e. Instead of:

webserver.RequestReceived += OnRequest;

You need something this:

webserver.AddHandler(null, null, null, false, new HttpRequestCallback(reqCallback));
private static void reqCallback(IHttpClientContext context, IHttpRequest request, IHttpResponse response) {