George George

George George

  • NA
  • 778
  • 0

Error stop Http Listener

Jun 3 2008 3:59 AM

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
}
[/Code]


thanks in advance,
George