Thread: Error stop Http Listener

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    Error stop Http Listener

    Hello everyone,


    Here is my simple application code for a Windows Service application. The question is, when stop the service, there is always,

    // [System.InvalidOperationException] = {"Please call the Start() method before calling this method."}

    Does anyone have any ideas why there is such issue and how to fix?

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.Text;
    using System.Net;
    using System.Threading;
    
    namespace TestServiceStop1
    {
        public partial class Service1 : ServiceBase
        {
            private Thread _tHttpThread;
            private TestHttpServer _server;
    
            public Service1()
            {
                InitializeComponent();
            }
    
            protected override void OnStart(string[] args)
            {
                _server = new TestHttpServer(this);
    
                _tHttpThread = new Thread(_server.StartListen);
    
                _tHttpThread.Start();
            }
    
            protected override void OnStop()
            {
                // stop HTTP server
                _server.Stop(false);
            }
        }
    
        public class TestHttpServer
        {
            // listening HTTP port
            private int _Port = 0;
    
            // internal wrapped HTTP listener
            private HttpListener _Server = new HttpListener();
    
            private Service1 _manager;
    
            public TestHttpServer (Service1 manager)
            {
                _manager = manager;
            }
    
            public int ListenPort
            {
                get
                {
                    return _Port;
                }
                set
                {
                    _Port = value;
                }
            }
    
            public void StartListen()
            {
                try
                {
                    IAsyncResult result;
                    _Server.Prefixes.Add(String.Format("http://+:{0}/", 9099));
                    _Server.Start();
                    while (true)
                    {
                        result = _Server.BeginGetContext(new AsyncCallback(this.HttpCallback), _Server);
                        result.AsyncWaitHandle.WaitOne();
                    }
                }
                // any exceptions are not expected
                // catch InvalidOperationException during service stop
                // [System.InvalidOperationException] = {"Please call the Start() method before calling this method."}
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    
            public void Stop(bool isTerminate)
            {
                _Server.Stop();
            }
    
            // callback function when there is HTTP request received
            private void HttpCallback(IAsyncResult result)
            {
                HttpListenerContext context = _Server.EndGetContext(result);
                HandleRequest(context);
            }
    
            // find matched URL HTTP request handler and invoke related handler
            private void HandleRequest(HttpListenerContext context)
            {
                string matchUrl = context.Request.Url.AbsolutePath.Trim().ToLower();
    
                context.Response.StatusCode = 200;
                context.Response.StatusDescription = "OK";
                context.Response.Close();
            }
        } // end of Http Listener class
    }

    thanks in advance,
    George

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Before calling Stop on the Server, you need to terminate your loop, or it will use a Server object that is already stopped and throw said exception.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HTTP Methods POST AND GET "DATA"
    By bhupesh.kec in forum C Programming
    Replies: 4
    Last Post: 06-05-2008, 12:35 PM
  2. my HTTP request handler code correct?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-25-2008, 04:01 AM
  3. C# HTTP request/response
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-02-2008, 06:00 AM
  4. Small HTTP proxy
    By zacs7 in forum Networking/Device Communication
    Replies: 8
    Last Post: 09-11-2007, 03:53 AM
  5. Writing all HTTP requests from a website to a log file
    By goomyman in forum C# Programming
    Replies: 1
    Last Post: 07-29-2005, 09:18 AM