- Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathProgram.cs
44 lines (36 loc) · 1.29 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
varbuilder=WebApplication.CreateBuilder();
builder.Services.AddSingleton<Content>();
varapp=builder.Build();
//Pay attention to the order of the parameter passed.
//If your parameter is distinct, the order does not matter, As you can see here we put Greeting
//at the end of the parameter passing although in the constructor Greeting was the second on the parameter list.
//However if you pass multiple parameters of the same type, the order matters.
app.UseMiddleware<TerminalMiddleware>("Cairo","Dody",newGreeting());
app.Run();
publicclassContent
{
publicstringSayHello()=>"Hello world";
}
publicclassGreeting
{
publicstringGreet()=>"Good morning";
}
publicclassTerminalMiddleware
{
readonlyContent_content;
readonlystring_city;
readonlyGreeting_greet;
readonlystring_name;
publicTerminalMiddleware(RequestDelegatenext,Greetinggreet,stringcity,Contentcnt,stringname)
{
//We are not using the parameter next in this middleware since this middleware is terminal
_content=cnt;
_city=city;
_greet=greet;
_name=name;
}
publicasyncTaskInvoke(HttpContextcontext)
{
awaitcontext.Response.WriteAsync($"{_greet.Greet()}{_name}, {_content.SayHello()} from {_city}");
}
}