-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomServer.cs
61 lines (51 loc) · 1.86 KB
/
CustomServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Hosting.Server;
namespace api_corelation.Models
{
public class CustomServer : IServer
{
private readonly HttpListener _listener;
public IFeatureCollection Features { get; private set; }
public CustomServer(int port)
{
_listener = new HttpListener();
_listener.Prefixes.Add($"http://*:{port}/");
}
public Task StartAsync<TContext>(IHttpApplication<TContext> application, CancellationToken cancellationToken)
{
_listener.Start();
//Console.WriteLine($"Custom server listening on http://*:5000/");
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
_listener.Stop();
//Console.WriteLine("Custom server stopped.");
return Task.CompletedTask;
}
public Task HandleRequestAsync(HttpListenerContext context)
{
var request = context.Request;
var response = context.Response;
string[] headers = request.Headers.AllKeys;
//foreach (string header in headers)
//{
// Console.WriteLine($"{header}: {request.Headers[header]}");
//}
string responseString = $"Hello World! Request received at {DateTime.Now}";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
response.OutputStream.Write(buffer, 0, buffer.Length);
response.OutputStream.Close();
return Task.CompletedTask;
}
public void Dispose()
{
throw new NotImplementedException();
}
}
}