2011-03-30 27 views
1

我必须建立一个HttpListener,它将等待客户端服务器发出的请求。我必须在端口8088上接收该请求并提取查询字符串。这是很容易的部分。HttpListener问题

我在windows服务中运行HttpListener。我无法让它正确启动。我构建安装项目在我们的服务器上安装服务,并且它永远不会启动。我怀疑我的代码存在错误。

HttpListenerClass:

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); 
      } 
     } 
    } 
} 

Service1.cs:

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(); 
     } 
    } 
} 

我在做什么错?

谢谢你们!

+0

查看事件日志并发布您在此处看到的错误。 – Aliostad 2011-03-30 11:59:03

+0

当你说“永不开始”,你能更具体吗?你打算怎么开始它?它是否因错误而失败?该错误是否在控制台或事件查看器中可见?也许把所有的东西都包装在try/catch中,并在任何地方写出任何异常来看看是否能够解决这个问题? – David 2011-03-30 12:01:12

+0

当两个线程同时进入AddToFile()方法时,会发生什么情况会非常令人兴奋。 ;) – 2011-03-30 12:44:04

回答

2

while(true)循环中排队工作项目?你是认真的吗?!

  1. 您的OnStart方法永远不会因为while循环而返回。但是,从OnStart方法返回对于服务经理知道您的服务是否正确启动至关重要。
  2. 由于无限循环,您的服务可能只会因OutOfMemoryException或类似事件而死亡。

建议:
尝试采用this sample。它在IronPython中,但也使用.NET框架。提示:该实现中的while(true)应该更改为能够在服务停止时打破while循环。此外,您必须以异步方式在Start方法中调用serveforever
这应该让你去。

+0

嗯...是的,怎么了? – 2011-03-30 12:01:14

+0

丹尼尔,我从你看到的互联网上的回答问题中借用了那部分代码。你会说什么,我必须改变代码?非常感谢你们的反馈! – Dragan 2011-03-30 12:03:33

+5

@Daniel Hilgarth - 相当苛刻。他正在努力学习。他试图复制代码,它不起作用。然后他来到这里,询问是否修理它。我不认为这是一个论坛来判断人们的工作价值...... – skaz 2011-03-30 12:09:28