Thursday, November 15, 2007

.Net.HttpListener.ShutDown

Once started, onHttpRequest handler is called for processing incoming connections. The problem is to shut down the service because using both Close, Abort and Stop causes any access to the listener besides IsListening to fall with ObjectDisposedException. In addition, it is desirable to have all connection processing finished on the service shut down.

The idea is to solve to problem by
  1. preceding the listener methods invocation by IsListening check and
  2. enforcing the sequence by rwLock
so that connections are processed acquired reader lock and controller stops the service with the writer lock. This way, we ensure that no connections are in the middle of process on service shut down.

The problem is, however, that the stopping thread will be blocking while shuts down. This prevents scenario: send "terminate" signals to all running processes and wait for them to finish. The only alternatives I see to enable the concurrent shut down: 1) go back to single thread, which accepts two messages: 'stop' and 'new connection' to be processed in a new thread; or 2) create a terminator thread for every server.

No comments: