Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7558303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:20:09+00:00 2026-05-30T12:20:09+00:00

I have implemented a really simple C# Http Server using HttpListener within a windows

  • 0

I have implemented a really simple C# Http Server using HttpListener within a windows service, that waits for requests and sends them to a ZMQ when recieved. It is very similar to the HTTP server here – Production ready, multi-threaded c# http server except it executes the RunServer delegate asynchronously and then once the service has started sits in a continuous loop whist the HttpListener is listening. I think I have something which works in most part, however when I stop the server if requests are outstanding then it throws an error

The I/O operation has been aborted because of either a thread exit or
an application request

in the Handle method. I’m worried I’m overlooking other things too which would be needed for a production environment.

    public XsmHttpServer(string httpAddressEndpoint)
    {                        
        _listener = new HttpListener();
        _listenerAddr = httpAddressEndpoint;
    }

    public void Start()
    {
        _listener.Start();

        Action action = RunServer;
        action.BeginInvoke(RunServerCallback, action);                        
    }


    private void RunServer()
    {
        while (_listener.IsListening)
        {
            IAsyncResult result = _listener.BeginGetContext(Handle, _listener);
            result.AsyncWaitHandle.WaitOne();
        }
    }


    private void Handle(IAsyncResult result)
    {
        HttpListener listener = (HttpListener)result.AsyncState;
        HttpListenerContext context = listener.EndGetContext(result);

        //Do work, send to ZMQ & write response back 
        _handler.Handle(context);                                         
    }

    private void RunServerCallback(IAsyncResult ar)
    {
        try
        {
            Action target = (Action)ar.AsyncState;
            target.EndInvoke(ar);
        }
        catch (Exception ex)
        {
            if (_log.IsDebugEnabled) _log.Debug("Failed to initialize Server", ex);  
        }
    }


    public void Stop()
    {
       _listener.Close();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:20:10+00:00Added an answer on May 30, 2026 at 12:20 pm

    By “stop the server”, I assume you mean you’re calling the Stop() method in your code. This is calling the listener’s Close() method, which leaves any pending requests unable to complete (http://msdn.microsoft.com/en-us/library/system.net.httplistener.close.aspx).

    You should first call the listener’s Stop() method, wait until all pending requests have been handled, and then Close() it.

    Unless your application makes it okay to chop off those pending requests, in which case you could just accept and ignore the error, but, in general, it would likely be best to let them finish to avoid situations where you’ve half-processed a request and the client resubmits it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using node, express and connect for a simple app and have basic HTTP
I am trying to adapt my simple socket server so that it can have
Background I have a Windows service that uses various third-party DLLs to perform work
I have a legacy HTTP/XML service that I need to interact with for various
I have implemented a simple file upload-download mechanism. When a user clicks a file
I have implemented a python webserver. Each http request spawns a new thread. I
I have implemented tracing based on System.Diagnostics. I am also using a System.Diagnostics.TextWriterTraceListener, and
We have implemented a popup window as a modal dialog using the IE method:
Oh hello there, fellow SO members, I have a web service that returns XML
I am looking at securing some WCF services using WIF, and have read within

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.