In an ASP.NET Core 2.0 project, I'm trying to initiate a new request to the server itself. Since Kestrel is a managed web server, I assume this should be easy to do, but I couldn't find the answer.
This is particularly useful when you need the results from other actions to produce the result (possibly multiple actions), but you want to run the whole pipeline for each. (eg. logging, authorization, or routing)
No, I don't think this is an XY problem, I'll edit to clarify, and provide usecases at the end.
What I want to achieve is to avoid the network layer, otherwise HttpClient
was the way to go, and it was easy. But if I interact with the network, overheads aside, I'd also need to know the hosting URL and Port of the server, which makes it ugly.
I'll elaborate by an example: I have used patterns like this for in-process hosting of the web server (mainly for unit-tests)
var server = TestServer.Create(appBuilder =>
{
var startup = new TestStartup();
startup.Configuration(appBuilder);
});
var client = server.HttpClient;
// Use the client to send requests
This test server allows an in-memory stream to be processed without any binding on the network. I want to achieve similar results using Kestrel. Or, if possible, construct a Request
object and pass a reference for it to be processed by the web host and pipeline.
I can think of a few use cases for this:
- Batching multiple requests to the server (via request body) and avoid transport latency
- Late-running the routing after some action-specific logic, where the best and easiest key to explain what to expect from the server is the URL itself
- The effect of server-side redirects (
Server.Transfer
in old days)
I am aware of the security risks and the smell, but in some cases this can be a good solution. My case, specifically, is batching plus some calculations on the results for small monitoring stubs that are limited in access and only perform read-only operations, so no risks.