以编程方式调用 ASP.NET Core 请求管道

Rob*_*ner 9 middleware request-pipeline asp.net-core asp.net-core-testhost

问题

考虑到我有 HTTP 动词、路由、标头和正文有效负载,是否有一种方法可以从我自己的应用程序中以编程方式调用 ASP.NET Core 请求管道?

背景

在某些用例中,我们的 ASP.NET Core 应用程序的 WebAPI 无法访问,因为该应用程序在防火墙后面运行或无法访问。

为了为这种情况提供解决方案,我们希望我们的应用程序轮询一些其他服务以获取“工作项”,然后将其转换为我们应用程序中的 API 调用。

我考虑的方法

  • 我可能可以要求 DI 给我一个控制器实例,然后调用它的方法。这种方法的问题:
    • 不强制执行授权属性。但在我们的用例中,验证不记名令牌非常重要。所以这里的问题是:如何以编程方式调用授权中间件?
    • 我必须自己将传入的工作项路由到正确的控制器/方法。
  • 使用该Microsoft.AspNetCore.TestHost包,我可以创建一个TestClient允许我向自己发出请求的包(请参阅此处)。但这里存在一些不确定性:
    • 其预期用例TestHost是用于集成测试。在生产环境中使用它安全吗?
    • 是否有可能TestServer在常规托管的同时进行这样的运行?
    • 线程安全怎么样?TestClients我可以从单个实例创建多个TestServer实例并从不同的线程使用它们吗?

所以我确信必须有一种更干净、更直接的方法来从我自己的应用程序中以编程方式调用请求管道......

Phi*_*ipM 9

是的,这实际上相当容易。您可以在 Startup 类配置方法的末尾获取对请求管道的引用。将其保存在静态字段/单例服务/等中。

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
  // ... usual configuration code

  AClass.PipelineStaticField = app.Build();
}
Run Code Online (Sandbox Code Playgroud)

然后,在您希望注入请求的方法中,您必须构建一个 HttpContext 以传递到管道中。

var ctx = new DefaultHttpContext();

// setup a DI scope for scoped services in your controllers etc.
using var scope = _provider.CreateScope();
ctx.RequestServices = scope.ServiceProvider;

// prepare the request as needed
ctx.Request.Body = new MemoryStream(...);
ctx.Request.ContentType = "application/json";
ctx.Request.ContentLength = 1234;
ctx.Request.Method = "POST";
ctx.Request.Path = PathString.FromUriComponent("/mycontroller/action");

// you only need this if you are hosting in IIS (.UseIISIntegration())
ctx.Request.Headers["MS-ASPNETCORE-TOKEN"] = Environment.GetEnvironmentVariable("ASPNETCORE_TOKEN");

// setup a place to hold the response body
ctx.Response.Body = new MemoryStream();

// execute the request
await AClass.PipelineStaticField(ctx);

// interpret the result as needed, e.g. parse the body
ctx.Response.Body.Seek(0, SeekOrigin.Begin);
using var reader = new StreamReader(ctx.Response.Body);
string body = await reader.ReadToEndAsync();
Run Code Online (Sandbox Code Playgroud)

这样,您的请求将遍历整个管道,包括所有中间件,例如身份验证和授权。