-
Notifications
You must be signed in to change notification settings - Fork 21
/
Program.cs
30 lines (23 loc) · 827 Bytes
/
Program.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
using System.Net;
using AspNetCoreFromScratch;
using Microsoft.Extensions.DependencyInjection;
var serviceProvider = new ServiceCollection()
.AddSingleton<RouteRegistry>()
.AddControllers()
.AddMiddleware<CustomMiddleware>()
.AddMiddleware<RoutingMiddleware>()
.BuildServiceProvider();
// Get the list of middleware from the DI container
var middlewares = serviceProvider.GetServices<IMiddleware>().ToList();
// Create a middleware container
var middlewareContainer = new MiddlewarePipeline(middlewares);
var httpListener = new HttpListener();
httpListener.Prefixes.Add("http://localhost:5001/");
httpListener.Start();
Console.WriteLine("Listening...");
while (true)
{
var context = httpListener.GetContext();
await middlewareContainer.InvokeAsync(context);
context.Response.Close();
}