Skip to content

Commit a03504a

Browse files
author
Anton Wieslander
committed
middleware
0 parents  commit a03504a

13 files changed

+465
-0
lines changed

.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
# User specific
3+
**/.idea/**/workspace.xml
4+
**/.idea/**/tasks.xml
5+
**/.idea/shelf/*
6+
**/.idea/dictionaries
7+
**/.idea/httpRequests/
8+
9+
# Sensitive or high-churn files
10+
**/.idea/**/dataSources/
11+
**/.idea/**/dataSources.ids
12+
**/.idea/**/dataSources.xml
13+
**/.idea/**/dataSources.local.xml
14+
**/.idea/**/sqlDataSources.xml
15+
**/.idea/**/dynamic.xml
16+
17+
# Rider
18+
# Rider auto-generates .iml files, and contentModel.xml
19+
**/.idea/**/*.iml
20+
**/.idea/**/contentModel.xml
21+
**/.idea/**/modules.xml
22+
23+
# System Files
24+
.vs/
25+
**/obj
26+
**/bin
27+
**/Properties
28+
*.csproj.user
29+
**/node_modules

Middleware/Middleware/Middleware.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29905.134
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Middleware", "Middleware\Middleware.csproj", "{8B336568-76B8-474D-9301-7801A1C643A9}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{8B336568-76B8-474D-9301-7801A1C643A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{8B336568-76B8-474D-9301-7801A1C643A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{8B336568-76B8-474D-9301-7801A1C643A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{8B336568-76B8-474D-9301-7801A1C643A9}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {769557E6-1F30-4DC3-AC6C-AD4A9B55E5D9}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net.Http;
5+
using System.Text;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace Middleware
10+
{
11+
public class HttpClientMiddleware : DelegatingHandler
12+
{
13+
protected override async Task<HttpResponseMessage> SendAsync(
14+
HttpRequestMessage request,
15+
CancellationToken cancellationToken)
16+
{
17+
// do before
18+
var response = await base.SendAsync(request, cancellationToken);
19+
// do after
20+
return response;
21+
}
22+
}
23+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
</Project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace Middleware
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder =>
22+
{
23+
webBuilder.UseStartup<Startup>();
24+
});
25+
}
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:57891",
7+
"sslPort": 44349
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"Middleware": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
22+
"environmentVariables": {
23+
"ASPNETCORE_ENVIRONMENT": "Development"
24+
}
25+
}
26+
}
27+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Hosting;
10+
11+
namespace Middleware
12+
{
13+
public class Startup
14+
{
15+
public void ConfigureServices(IServiceCollection services)
16+
{
17+
services.AddTransient<HttpClientMiddleware>();
18+
19+
services.AddHttpClient("name")
20+
.AddHttpMessageHandler<HttpClientMiddleware>() // 1st
21+
.AddHttpMessageHandler<HttpClientMiddleware>(); // 2nd
22+
}
23+
24+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
25+
{
26+
if (env.IsDevelopment())
27+
{
28+
app.UseDeveloperExceptionPage();
29+
}
30+
31+
app.UseRouting();
32+
33+
app.UseMiddleware<CustomMiddleware>();
34+
35+
app.Use((request) =>
36+
{
37+
//do something before request
38+
//no access to httpcontext
39+
return request;
40+
});
41+
42+
app.Use(async (httpContext, function) =>
43+
{
44+
// do something before
45+
await function();
46+
// do something after
47+
});
48+
49+
app.UseEndpoints(endpoints =>
50+
{
51+
endpoints.MapGet("/", async context =>
52+
{
53+
await context.Response.WriteAsync("Hello World!");
54+
});
55+
});
56+
}
57+
}
58+
59+
public class CustomMiddleware
60+
{
61+
private readonly RequestDelegate _next;
62+
63+
public CustomMiddleware(RequestDelegate next)
64+
{
65+
_next = next;
66+
}
67+
68+
public async Task InvokeAsync(HttpContext context)
69+
{
70+
// do before
71+
await _next(context);
72+
// do after
73+
}
74+
}
75+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"AllowedHosts": "*"
10+
}

Middleware/core_concept.linq

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
Try(LambdaFirst);
6+
7+
Wrap(LambdaSeccond);
8+
}
9+
10+
public void First()
11+
{
12+
"executing first function".Dump();
13+
}
14+
15+
public void Seccond()
16+
{
17+
"executing seccond function".Dump();
18+
}
19+
20+
public void LambdaFirst(){
21+
Wrap(First);
22+
}
23+
public void LambdaSeccond()
24+
{
25+
Try(Seccond);
26+
}
27+
28+
public void Wrap(Action function)
29+
{
30+
"start".Dump();
31+
function();
32+
"ends".Dump();
33+
}
34+
35+
public void Try(Action function)
36+
{
37+
try
38+
{
39+
"trying".Dump();
40+
function();
41+
}
42+
catch (Exception)
43+
{
44+
45+
}
46+
}

0 commit comments

Comments
 (0)