Skip to content

Commit 569ab62

Browse files
committed
Add the injector middleware
1 parent 2861ccd commit 569ab62

File tree

4 files changed

+125
-2
lines changed

4 files changed

+125
-2
lines changed

Diff for: MiddlewareInjector/MiddlewareInjectorExtensions.cs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
7+
namespace MiddlewareInjector
8+
{
9+
public static class MiddlewareInjectorExtensions
10+
{
11+
public static IApplicationBuilder UseMiddlewareInjector(this IApplicationBuilder builder, MiddlewareInjectorOptions options)
12+
{
13+
return builder.UseMiddleware<MiddlewareInjectorMiddleware>(builder.New(), options);
14+
}
15+
}
16+
}

Diff for: MiddlewareInjector/MiddlewareInjectorMiddleware.cs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.Http;
7+
8+
namespace MiddlewareInjector
9+
{
10+
public class MiddlewareInjectorMiddleware
11+
{
12+
private readonly RequestDelegate _next;
13+
private readonly IApplicationBuilder _builder;
14+
private readonly MiddlewareInjectorOptions _options;
15+
private RequestDelegate _subPipeline;
16+
17+
public MiddlewareInjectorMiddleware(RequestDelegate next, IApplicationBuilder builder, MiddlewareInjectorOptions options)
18+
{
19+
_next = next ?? throw new ArgumentNullException(nameof(next));
20+
_builder = builder ?? throw new ArgumentNullException(nameof(builder));
21+
_options = options ?? throw new ArgumentNullException(nameof(options));
22+
}
23+
24+
public Task Invoke(HttpContext httpContext)
25+
{
26+
var injector = _options.GetInjector();
27+
if (injector != null)
28+
{
29+
var builder = _builder.New();
30+
injector(builder);
31+
builder.Run(_next);
32+
_subPipeline = builder.Build();
33+
}
34+
35+
if (_subPipeline != null)
36+
{
37+
return _subPipeline(httpContext);
38+
}
39+
40+
return _next(httpContext);
41+
}
42+
}
43+
}

Diff for: MiddlewareInjector/MiddlewareInjectorOptions.cs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Builder;
7+
8+
namespace MiddlewareInjector
9+
{
10+
public class MiddlewareInjectorOptions
11+
{
12+
private Action<IApplicationBuilder> _injector;
13+
14+
public void InjectMiddleware(Action<IApplicationBuilder> builder)
15+
{
16+
Interlocked.Exchange(ref _injector, builder);
17+
}
18+
19+
internal Action<IApplicationBuilder> GetInjector()
20+
{
21+
return Interlocked.Exchange(ref _injector, null);
22+
}
23+
}
24+
}

Diff for: MiddlewareInjector/Startup.cs

+42-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,49 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
2525
app.UseDeveloperExceptionPage();
2626
}
2727

28-
app.Run(async (context) =>
28+
var injectorOptions = new MiddlewareInjectorOptions();
29+
30+
app.UseMiddlewareInjector(injectorOptions);
31+
32+
app.Run(async context =>
33+
{
34+
context.Response.ContentType = "text/html";
35+
await context.Response.WriteAsync("<html><body>");
36+
if (context.Request.Path.Equals("/clear"))
37+
{
38+
injectorOptions.InjectMiddleware(_ => { });
39+
await context.Response.WriteAsync("Cleared middleware<br>");
40+
}
41+
else if (context.Request.Path.Equals("/inject"))
42+
{
43+
injectorOptions.InjectMiddleware(InjectContent);
44+
45+
await context.Response.WriteAsync("Injected middleware<br>");
46+
}
47+
else
48+
{
49+
await context.Response.WriteAsync("Hello World!<br>");
50+
}
51+
await context.Response.WriteAsync("<a href=\"/inject\">Inject</a><br>");
52+
await context.Response.WriteAsync("<a href=\"/testpath\">Test Path</a><br>");
53+
await context.Response.WriteAsync("<a href=\"/clear\">Clear</a><br>");
54+
await context.Response.WriteAsync("<a href=\"/\">Home</a><br>");
55+
await context.Response.WriteAsync("</body></html>");
56+
});
57+
}
58+
59+
public void InjectContent(IApplicationBuilder builder)
60+
{
61+
builder.Map("/testpath", subBuilder =>
2962
{
30-
await context.Response.WriteAsync("Hello World!");
63+
subBuilder.Run(async context =>
64+
{
65+
context.Response.ContentType = "text/html";
66+
await context.Response.WriteAsync("<html><body>");
67+
await context.Response.WriteAsync("Injected content<br>");
68+
await context.Response.WriteAsync("<a href=\"/\">Home</a><br>");
69+
await context.Response.WriteAsync("</body></html>");
70+
});
3171
});
3272
}
3373
}

0 commit comments

Comments
 (0)