Hello,
I'm trying to develop a very small web server on a NetDuino plus 2, with v4.3.0 beta 1. The server just displays a single page when hit with any request. The first request works fine. The second one also, but then I get a System.NullReferenceException in System.Http.dll. This exception seems to be triggered completely out of my code, because I cannot catch it anywhere, not in any thread, and not in Main, nor does it report any call stack involving my code.
Any idea ?
(code follows; the static WebServer.Run() method is called from Main -- not reproduced here --- after making sure the board has an IP address.)
Thx,
Pyt.
public static class WebServer { #region Properties and fields //////private Socket _socket = null; private static readonly NetworkInterface _netIf = NetworkInterface.GetAllNetworkInterfaces()[0]; private const string _c_httpPrefix = "http"; private static Queue _contextQueue = new Queue(); #endregion #region Methods /// <summary> /// The web server's main routine. /// </summary> public static void Run() { HttpListener listener = new HttpListener(_c_httpPrefix); while (true) { try { if (!listener.IsListening) listener.Start(); // Whenever a request comes in, we queue the context, start a // new thread to handle the request, and go back to listening for // more requests. Several threads may thus be started in parallel // which will each be handling an incoming request. HttpListenerContext context = listener.GetContext(); // blocks until incoming request lock (_contextQueue) _contextQueue.Enqueue(context); Thread th = new Thread(new ThreadStart(_requestHandlerProc)); th.Start(); } catch (InvalidOperationException) { listener.Stop(); Thread.Sleep(200); } catch (ObjectDisposedException) { listener.Start(); } catch { Thread.Sleep(200); } } } private static void _requestHandlerProc() { HttpListenerContext context = null; try { lock (_contextQueue) context = (HttpListenerContext)_contextQueue.Dequeue(); if (context != null) { switch (context.Request.HttpMethod.ToUpper()) { case "GET": _processClientGetRequest(context); break; //////case "POST": ProcessClientPostRequest(context); break; } } } catch (Exception ex) { Utils.DebugPrintException(ex, "_requestHandlerProc"); } finally { if (context != null) context.Close(); } } private static void _processClientGetRequest(HttpListenerContext context) { Debug.Print("Request URL: " + context.Request.RawUrl); Debug.Print("Request Headers:"); Debug.Print(context.Request.Headers.ToString()); //Compose a response StringBuilder sbResp = new StringBuilder("<html><header><title>Energlass gateway</title></header><body><b>"); sbResp.Append(_netIf.IsDhcpEnabled ? "Dynamic IP = " : "Static IP = "); sbResp.Append("</b>"); sbResp.Append(_netIf.IPAddress); sbResp.Append("<p/>" + DateTime.Now.ToString()); sbResp.Append("</body></html>"); byte[] msgBody = Encoding.UTF8.GetBytes(sbResp.ToString()); context.Response.ContentEncoding = Encoding.UTF8; context.Response.ContentType = "text/html"; context.Response.ContentLength64 = msgBody.Length; context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.OutputStream.Write(msgBody, 0, msgBody.Length); } #endregion }