Remy
Remy

Reputation: 5173

How to set the TimeoutManager on HttpListener

Starting from .NET framework 4.5 the HttpListenertimeoutManager Class was introduced, which acts as a manager for HttpListeners:

The timeout manager to use for an HttpListener object.

Which allows you to set different timeouts for the HttpListener using TimeSpan like so:

private HttpListenerTimeoutManager manager;

manager.IdleConnection = TimeSpan.FromMinutes(2);
manager.HeaderWait = timespan.FromMinutes(2);
manager.DrainEntityBody= timespan.FromMinutes(2);

this seems like a neat thing to have to save a certain preset in, without having to set all the timeouts individually for any listener that may be running.

Now i want to set the TimeoutManager on my HttpListener listener to my newly created manager. However there seems to be no way to do that.

listener.TimeoutManager = manager; cannot be done as the property listener.TimeoutManager is read only (as stated in the docs).

But there is no alternative listener.TimeoutManager.Set() method to set it either, or any other setter method.

Creating a new HttpListener with the manager in its constructor listener = new HttpListener(manager) does not work either, as HttpListener has no constructor that takes 1 argument.

Meaning the only way i can edit the HeaderWait using the manager on my listener is to do the following:

listener.TimeoutManager.HeaderWait = manager.HeaderWait;

which really just comes down to doing

listener.TimeoutManager.Headerwait = Timespan.FromMinutes(2);

Which then seems the better option as it saves me from creating an additional variable, and it still forces me to set every timeout individually.

Am I misunderstanding the usecase of HttpListenerTimeoutManager, or overseeing the way of setting it here?

Using Unity with the .NET 4.x (which brings support up to .NET 4.7 class libraries) scripting runtime version.

Upvotes: 3

Views: 3736

Answers (1)

Remy
Remy

Reputation: 5173

Converting Paul and Kevin Gosse their comments into an answer for future reference:

as Kevin Gosse pointed out I was indeed wrong in thinking that a HttpListenerTimeoutManager can be used to manage the timeouts across multiple HttpListeners. Instead it is just there to

group all the types of timeouts in one place, to make the HttpListener simpler to use

Which makes sense looking at the implementation of the HttpListenerTimeoutManager Class.

The correct way to use the HttpListenerTimeoutManager is by setting it to the property of HttpListener.TimeoutManager like so:

HttpListener listener;
HttpListenerTimeoutManager manager;

public Foo()
{
    manager = listener.TimeoutManager;
    manager.IdleConnection = TimeSpan.FromMinutes(5);
    manager.HeaderWait = TimeSpan.FromMinutes(5);
}

Upvotes: 3

Related Questions