WebCache.Tests/Server.cs
/// <summary>
/// Callback for async server. Adds context to stack and readies server for another exchange.
/// </summary>
/// <param name="result"></param>
Callback for async server. Adds context to stack and readies server for another exchange.
Callback for async server . Adds context to stack and readies server for another exchange .
private void ListenerCallback(IAsyncResult result)
        {
            if (disposedValue)
            {
                return;
            }
            try
            {
                HttpListenerContext httpListenerContext = httpListener?.EndGetContext(result);
                if (httpListenerContext == null)
                {
                    return;
                }
                HttpListenerRequest request = httpListenerContext.Request;
                string body = null;
                using (StreamReader reader = new StreamReader(request.InputStream))
                {
                    body = reader.ReadToEnd();
                }
                _requestUriList.Add(request);
                Response testResponse;
                if (!Responses.TryGetValue(request.Url.ToString(), out testResponse))
                {
                    testResponse = Default404Response;
                }
                using (var response = httpListenerContext.Response)
                using (var outputStream = response.OutputStream)
                {
                    byte[] buffer = new byte[0];
                    if (testResponse?.ResponseData?.Length > 0)
                    {
                        buffer = testResponse.ResponseData;
                    }
                    response.ContentLength64 = buffer.Length;
                    response.StatusCode = testResponse.StatusCode;
                    outputStream.Write(buffer, 0, buffer.Length);
                    Console.WriteLine("sending response");
                }
            }
            catch (HttpListenerException ex) when (ex.ErrorCode == 995)
            {
            }
            finally
            {
                var callBack = new AsyncCallback(ListenerCallback);
                if (!disposedValue)
                {
                    httpListener?.BeginGetContext(callBack, this);
                }
            }
        }
private void ListenerCallback ( IAsyncResult result ) { if ( disposedValue ) { return ; } try { HttpListenerContext httpListenerContext = httpListener ? . EndGetContext ( result ) ; if ( httpListenerContext == null ) { return ; } HttpListenerRequest request = httpListenerContext . Request ; string body = null ; using ( StreamReader reader = new StreamReader ( request . InputStream ) ) { body = reader . ReadToEnd ( ) ; } _requestUriList . Add ( request ) ; Response testResponse ; if ( ! Responses . TryGetValue ( request . Url . ToString ( ) , out testResponse ) ) { testResponse = Default404Response ; } using ( var response = httpListenerContext . Response ) using ( var outputStream = response . OutputStream ) { byte [ ] buffer = new byte [ 0 ] ; if ( testResponse ? . ResponseData ? . Length > 0 ) { buffer = testResponse . ResponseData ; } response . ContentLength64 = buffer . Length ; response . StatusCode = testResponse . StatusCode ; outputStream . Write ( buffer , 0 , buffer . Length ) ; Console . WriteLine ( " " ) ; } } catch ( HttpListenerException ex ) when ( ex . ErrorCode == 995 ) { } finally { var callBack = new AsyncCallback ( ListenerCallback ) ; if ( ! disposedValue ) { httpListener ? . BeginGetContext ( callBack , this ) ; } } }
Callback for async server.
Callback for async server .
// Call EndGetContext to complete the asynchronous operation. // System.Net.HttpListenerException: The I/ O operation has been aborted because of either a thread exit or an application request // This appears to happen if a socket is closed, it seems safe to carry on and begin listening again.
result
IAsyncResult
None