title: HttpListener Problem
body: <p>I have to establish an <code>HttpListener</code> that will wait for requests made by our client's server. I have to receive that request on port 8088 and extract the query string. That is the easy part.</p>
  
  <p>I'm running the <code>HttpListener</code> in a windows service. I cannot get it to fire properly. I build the setup project install the service on our server and it never starts. I suspect there's an error with my code. </p>
  
  <p>HttpListenerClass:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.IO;
  using System.Net;
  using System.Threading;
  
  namespace lalalolo
  {
      class HttpListenerClass
      {
          bool keepAlive = true;
  
          public void AddToFile(string contents)
          {
              var fs = new FileStream(@"C:\HttpListenerserv.txt", FileMode.OpenOrCreate, FileAccess.Write);
              var sw = new StreamWriter(fs);
              sw.BaseStream.Seek(0, SeekOrigin.End);
              sw.WriteLine(contents);
              sw.Flush();
              sw.Close();
          }
  
          private HttpListener listener;
  
          public HttpListenerClass()
          {
              ThreadPool.SetMaxThreads(50, 100);
              ThreadPool.SetMinThreads(50, 50);
              listener = new HttpListener();
              listener.Prefixes.Add("http://*:8088/");
          }
  
          public void Start()
          {
  
              listener.Start();
              if(keepalive == true){
              {
                  try
                  {
                      HttpListenerContext ctx = listener.GetContext();
                      ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessRequest), ctx);
                  }
                  catch(Exception ex)
                  {
                      AddToFile(ex.Message);
                  }
                }
              }
          }
  
          public void Stop()
          {
              listener.Stop();
              keepalive == false;
          }
  
          public void ProcessRequest(object listenerContext)
          {
              try
              {
                  var context = (HttpListenerContext)listenerContext;
                  string QS = context.Request.QueryString["ID"];
                  AddToFile(QS);
              }
  
              catch(Exception ex)
              {
                  AddToFile(ex.Message);
              }
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Service1.cs:</p>
  
  <pre><code>using System.ComponentModel;
  using System.Data;
  using System.Diagnostics;
  using System.IO;
  using System.Linq;
  using System.Net;
  using System.ServiceProcess;
  using System.Text;
  using System.Threading;
  
  namespace lalalolo
  {
      public partial class HttpListenerTest1 : ServiceBase
      {
          HttpListenerClass HTTP = new HttpListenerClass();
  
          public void AddToFile(string contents)
          {
              var fs = new FileStream(@"C:\HttpListenerserv.txt", FileMode.OpenOrCreate, FileAccess.Write);
              var sw = new StreamWriter(fs);
              sw.BaseStream.Seek(0, SeekOrigin.End);
              sw.WriteLine(contents);
              sw.Flush();
              sw.Close();
          }
  
          public HttpListenerTest1()
          {
              InitializeComponent();
          }
  
          protected override void OnStart(string[] args)
          {
              HTTP.Start();
          }
  
          protected override void OnStop()
          {
              HTTP.Stop();   
          }
      }
  }
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>Thank you guys!</p>