Skip to content

Commit ccf312a

Browse files
author
Michael Hallett
committed
Merge pull request restsharp#660 from issafram/SimpleServerFix
SimpleServer Cleanup With Added Try Catches
2 parents a2daa1b + 95894a3 commit ccf312a

File tree

1 file changed

+57
-34
lines changed

1 file changed

+57
-34
lines changed
Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,81 @@
1-
using System;
2-
using System.Net;
3-
using System.Threading;
4-
5-
namespace RestSharp.IntegrationTests.Helpers
1+
namespace RestSharp.IntegrationTests.Helpers
62
{
3+
using System;
4+
using System.Net;
5+
using System.Security;
6+
using System.Threading;
7+
78
public class SimpleServer : IDisposable
89
{
9-
private readonly HttpListener _listener;
10-
private readonly Action<HttpListenerContext> _handler;
11-
private Thread _processor;
10+
private readonly HttpListener listener;
11+
private readonly Action<HttpListenerContext> handler;
12+
private Thread thread;
13+
14+
private SimpleServer(HttpListener listener, Action<HttpListenerContext> handler)
15+
{
16+
this.listener = listener;
17+
this.handler = handler;
18+
}
1219

13-
public static SimpleServer Create(string url, Action<HttpListenerContext> handler,
20+
public static SimpleServer Create(
21+
string url,
22+
Action<HttpListenerContext> handler,
1423
AuthenticationSchemes authenticationSchemes = AuthenticationSchemes.Anonymous)
1524
{
16-
var listener = new HttpListener
17-
{
18-
Prefixes = {url},
19-
AuthenticationSchemes = authenticationSchemes
20-
};
25+
var listener = new HttpListener { Prefixes = { url }, AuthenticationSchemes = authenticationSchemes };
2126
var server = new SimpleServer(listener, handler);
2227

2328
server.Start();
24-
2529
return server;
2630
}
2731

28-
private SimpleServer(HttpListener listener, Action<HttpListenerContext> handler)
29-
{
30-
_listener = listener;
31-
_handler = handler;
32-
}
33-
3432
public void Start()
3533
{
36-
if (!_listener.IsListening)
34+
if (this.listener.IsListening)
3735
{
38-
_listener.Start();
36+
return;
37+
}
3938

40-
_processor = new Thread(() =>
41-
{
42-
var context = _listener.GetContext();
43-
_handler(context);
44-
context.Response.Close();
45-
}) {Name = "WebServer"};
39+
this.listener.Start();
4640

47-
_processor.Start();
48-
}
41+
this.thread = new Thread(() =>
42+
{
43+
var context = this.listener.GetContext();
44+
this.handler(context);
45+
context.Response.Close();
46+
}) { Name = "WebServer" };
47+
48+
this.thread.Start();
4949
}
5050

5151
public void Dispose()
5252
{
53-
_processor.Abort();
54-
_listener.Stop();
55-
_listener.Close();
53+
try
54+
{
55+
this.thread.Abort();
56+
}
57+
catch (ThreadStateException threadStateException)
58+
{
59+
Console.WriteLine("Issue aborting thread - {0}.", threadStateException.Message);
60+
}
61+
catch (SecurityException securityException)
62+
{
63+
Console.WriteLine("Issue aborting thread - {0}.", securityException.Message);
64+
}
65+
66+
if (this.listener.IsListening)
67+
{
68+
try
69+
{
70+
this.listener.Stop();
71+
}
72+
catch (ObjectDisposedException objectDisposedException)
73+
{
74+
Console.WriteLine("Issue stopping listener - {0}", objectDisposedException.Message);
75+
}
76+
}
77+
78+
this.listener.Close();
5679
}
5780
}
5881
}

0 commit comments

Comments
 (0)