-
Notifications
You must be signed in to change notification settings - Fork 0
/
07_CustomiseUpstreamRequest.cs
54 lines (48 loc) · 1.76 KB
/
07_CustomiseUpstreamRequest.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
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace ProxyKit.Recipes
{
public class CustomiseUpstreamRequest : Recipe<CustomiseUpstreamRequest.Startup>
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddProxy();
}
public const string XCorrelationId = "X-Correlation-ID";
public void Configure(IApplicationBuilder app)
{
// Inline
app.RunProxy(context =>
{
var forwardContext = context.ForwardTo("http://localhost:5001");
if (forwardContext.UpstreamRequest.Headers.Contains("X-Correlation-ID"))
{
forwardContext.UpstreamRequest.Headers.Add("X-Correlation-ID", Guid.NewGuid().ToString());
}
return forwardContext.Send();
});
// Extension Method
app.RunProxy(context => context
.ForwardTo("http://localhost:5001")
.AddXForwardedHeaders()
.ApplyCorrelationId()
.Send());
}
}
}
public static class CorrelationIdExtensions
{
public const string XCorrelationId = "X-Correlation-ID";
public static ForwardContext ApplyCorrelationId(this ForwardContext forwardContext)
{
if (forwardContext.UpstreamRequest.Headers.Contains(XCorrelationId))
{
forwardContext.UpstreamRequest.Headers.Add(XCorrelationId, Guid.NewGuid().ToString());
}
return forwardContext;
}
}
}