HTTPListener exception

Hi
I made simple test application with http listener, based on Ethernet and Http server tutorials.

I tested it using Postman Runner with 1000 iterations test (with delay 3 sec)
It works with only few replies delayed - response time is less then 200 ms, but had few responses with reply time more then 15 sec!

But - every few attempts I got exceptions:

112
116
RawUrl: /api/on
Command: on
#### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (5) ####
#### Message:
#### System.Net.OutputNetworkStreamWrapper::Flush [IP: 0014] ####
#### System.Net.HttpListenerResponse::System.IDisposable.Dispose [IP: 0018] ####
#### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (5) ####
#### Message:
#### System.Net.OutputNetworkStreamWrapper::Flush [IP: 0014] ####
#### System.Net.HttpListenerResponse::System.IDisposable.Dispose [IP: 0018] ####
#### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (5) ####
#### Message:
#### System.Net.OutputNetworkStreamWrapper::Flush [IP: 0014] ####
#### System.Net.HttpListenerResponse::System.IDisposable.Dispose [IP: 0018] ####
#### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (5) ####
#### Message:
#### System.Net.OutputNetworkStreamWrapper::Flush [IP: 0014] ####
#### System.Net.HttpListenerResponse::System.IDisposable.Dispose [IP: 0018] ####
112
116
RawUrl: /api/off
Command: off
#### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (5) ####
#### Message:
#### System.Net.OutputNetworkStreamWrapper::Flush [IP: 0014] ####
#### System.Net.HttpListenerResponse::System.IDisposable.Dispose [IP: 0018] ####
112
116
RawUrl: /api/on
Command: on
112
116
RawUrl: /api/off
Command: off
112
116
RawUrl: /api/on
Command: on
112
116
RawUrl: /api/off
Command: off
112
116
RawUrl: /api/on
Command: on
112
116
RawUrl: /api/off
Command: off

Exception doesn’t brake program excecution - I can’t catch it either within my try.

Can you tell me what could be wrong. For me it looks like smth is going wrong within HttpListener class.

Here is my code:

using GHIElectronics.TinyCLR.Networking;
using GHIElectronics.TinyCLR.Devices.Network;
using GHIElectronics.TinyCLR.Devices.Gpio;
using GHIElectronics.TinyCLR.Pins;
using System.Threading;
using System.Net;
using System;

namespace Httptest
{

public static class Http
{
    
    public static void Start()
    {
        //Create a listener.
        HttpListener listener = new HttpListener("http", 80);

        listener.Start();
        var clientRequestCount = 0;

        while (true)
        {
            try
            {

                //Note: The GetContext method blocks while waiting for a request.
                HttpListenerContext context = listener.GetContext();

                //Obtain a response object.
                HttpListenerResponse response = context.Response;
                
                //Construct a response.                
                var responseString = string.Format("<HTML><BODY> I am TinyCLR OS Server. " +
                    "Client request count: {0}</BODY></HTML>", ++clientRequestCount);

                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);

                //Get a response stream and write the response to it.
                response.ContentLength64 = buffer.Length;
                var output = response.OutputStream;

                System.Diagnostics.Debug.WriteLine("112");

                output.Write(buffer, 0, buffer.Length);

                System.Diagnostics.Debug.WriteLine("116");

                //You must close the output stream.
                output.Close();
                
                //geting command from request
                HttpListenerRequest request = context.Request;

                if (request.RawUrl.Length > 0)
                {
                    System.Diagnostics.Debug.WriteLine("RawUrl: "+request.RawUrl);
                    string[] strParams = request.RawUrl.Substring(1).Split('/');

                    //var method = request.HttpMethod;

                    if (strParams[0].Length > 0 && strParams[0] == "api")
                        if (strParams.Length >= 2)
                        {
                            switch (strParams[1])
                            {
                                case "beep":

                                    break;

                                case "on":
                                    System.Diagnostics.Debug.WriteLine("Command: "+strParams[1]);

                                    break;

                                case "off":
                                    System.Diagnostics.Debug.WriteLine("Command: " + strParams[1]);
                                    break;

                            }

                        }

                }
                

            }
            catch (OutOfMemoryException ex1)
            {
                throw new OutOfMemoryException("Exc at 01: " + ex1.Message);
            }
            catch (Exception ex)
            {
            }

            
        }

        listener.Stop();
    }

    
}

}

Hi, did you solve this? I had the exact same issue. It turned out to be because I closed the output stream. When I instead closed the response, the exception whent away and the server performs much better.