为什么的HttpListener开始处置方法上的东西异常?异常、东西、方法、HttpListener

2023-09-07 09:07:35 作者:嬲

有些事情我真的不同的HttpListener明白了。

在code以下不言自明的,并以某种方式暴露了问题。

我只要实现两个不同的HttpListener具有相同的单preFIX为每一个。 然后,我开始了第一个监听,当然我得到一个HttpListenerException当我尝试启动第二个(同preFIX)..到目前为止好。

好了我的错(或者,如果我们正在经历一个配置工具的最终用户的故障)。没有丝毫的慌乱,我只是清除了第二个侦听器的preFIX并指定一个新的...或停止第一个监听并尝试重新启动第二个,或任何...

但无法做到这一切,因为只要我试图访问第二个监听器prefixes或其他任何东西,我得到一个的ObjectDisposedException(无法访问一个释放的对象对象名称:System.Net。的HttpListener)。

我的问题是,为什么? 我看不到的HttpListener任何文件指定一个HttpListenerException的HttpListener的一些内在的东西的对象是某种处置和对象是从......这一点只是无用

因此​​,这意味着,如果我开始一个的HttpListener,并得到一个HttpListenerException我必须重新创建在任何情况下,一个全新的HttpListener对象?似乎有点怪异,我(但有可能是另一种方式,或一个很好的理由)。

在此先感谢您的回答!

  VAR listener1 =新的HttpListener();
。listener1 prefixes.Add(HTTP://本地主机:8080 / MyHandler的/);
listener1.Start();

VAR LISTENER2 =新的HttpListener();
。LISTENER2 prefixes.Add(HTTP://本地主机:8080 / MyHandler的/);

尝试
{
   listener2.Start();
}
赶上(HttpListenerException前)
{
  。LISTENER2 prefixes.Clear(); // BAM!的ObjectDisposedException
}
 

解决方案

的异常意味着东西出了可怕的错误。而不是让不知情的消费者继续使用可能损坏和不稳定的对象,它代替处置本身。只要创建新的监听器,并高兴的是,你不必担心使用可能没用的对象。

There is something that I really don't understand with the HttpListener.

异常处理

The code below speaks for itself and expose the "issue" in one way.

I simply instantiate two different HttpListener with the same single prefix for each one. I then start the first listener, and of course I get an HttpListenerException when I try to start the second one (same prefix) .. so far so good.

Ok my fault (or the end user fault if we are going through a configuration tool). No panic, I will just clear the prefix of the second listener and specify a new one ... or stop the first listener and try to restart the second one, or whatever ...

But can't do all of this because as soon as I am trying to access the second listener Prefixes or anything else, I get an ObjectDisposedException (Cannot access a disposed object. Object name: 'System.Net.HttpListener').

My question is WHY ? I do not see anything in HttpListener documentation specifying that on a HttpListenerException some inner stuff of HttpListener object is somehow disposed and the object is just useless from that point on ...

So this means that if I am starting an HttpListener and get an HttpListenerException I have to recreate a whole new HttpListener object in any case ? Seems a little bit weird for me (but there may be another way or a very good reason).

Thanks in advance for your answers !!

var listener1 = new HttpListener();
listener1.Prefixes.Add("http://localhost:8080/MyHandler/");
listener1.Start();

var listener2 = new HttpListener();
listener2.Prefixes.Add("http://localhost:8080/MyHandler/");

try
{
   listener2.Start();
}
catch (HttpListenerException ex)
{
  listener2.Prefixes.Clear(); // BAM ! ObjectDisposedException
}

解决方案

An exception means something has gone horribly wrong. Rather than allow unsuspecting consumers to continue to use a possibly corrupt and unstable object, it instead disposes of itself. Just create the new listener, and be happy that you don't have to worry about using a possibly useless object.