1

This question had a similar situation but the guy ended up using HttpClient as a hack: Programmatically invoking the ASP.NET Core request pipeline

Is there a way to trigger the http pipeline (simulate a full request) by providing the payload, headers, verb and url route programmatically WITHOUT using the HttpClient?

I also saw you could do something similar with the TestServer for integration testing but this feels like a hack as well for what I want

Simulate POST request in a unit test using ASP.NET Core

Cody Bouche
  • 945
  • 5
  • 10
  • Could you explain why you consider the `WebApplicationFactory` as a hack? – HMZ Jul 08 '20 at 18:34
  • Could I use the same `IWebHostBuilder` reference used in my main startup and register a singleton to the `TestServer`? Im assuming `TestServer` needs to be a new reference every time since it calls Build() internally. – Cody Bouche Jul 08 '20 at 18:48
  • Also I want to use this in production to push an arbitrary number of requests through the request pipeline and creating a new WebHost instance per request seems like a lot of overhead... – Cody Bouche Jul 08 '20 at 18:56
  • you want to use HTTP payload, HTTP headers, HTTP verb, HTTP url route without HTTP? nice. – Yehor Androsov Jul 08 '20 at 19:49
  • obviously the dispatcher parses all these things and activates the controller method in some way already... im asking how to trigger that programmatically...not that weird of a thing to ask... @YegorAndrosov – Cody Bouche Jul 08 '20 at 20:09
  • but how do you intend to trigger it? on the outer side there is only http port listener, and inside you are able to call services or whatever your request pipeline has through dependency injection. i can't really imagine the use case for your question. – Yehor Androsov Jul 08 '20 at 20:31
  • it is highly possible that this functionality marked as internal in asp.net core lib, then it will become even more dirty if used through reflection – Yehor Androsov Jul 08 '20 at 20:32
  • @CodyBouche i see your point, but according to the team the `WebApplicationFactory` is the way to go for invoking the pipeline and the `TestServer` is an infrastructure concept and lacks the setup that `WebApplicationFactory` does and tbh i cant see how invoking it the way you want would't be hacky. you can try creating your own server implementation and registering it as Singleton. – HMZ Jul 08 '20 at 21:21
  • @YegorAndrosov thats exactly my question.. how do i trigger it? I was looking for a way to get a reference the the host and pushing a request through (i want to be able to do this from a service or something). – Cody Bouche Jul 08 '20 at 22:37
  • @HMZ `TestServer` still uses `HttpClient` internally to make requests. Im basically looking for a way to get a reference to the running host (this should already be a single instance?) and call the pipeline. – Cody Bouche Jul 08 '20 at 22:37
  • @CodyBouche it does but its part of the `WebApplicationFactory` and can't be used in `Net Core 3.1` see the [Source](https://github.com/dotnet/aspnetcore/blob/f7f80fdbaaa0283e8e2e89ceed7abf7fe413b17c/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs) and [docs](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.testhost.testserver?view=aspnetcore-3.0&viewFallbackFrom=aspnetcore-3.1) I may be wrong here because i haven't tried it but you could try creating a static property in the `Program` class and getting the instance of `IWebHostBuilder` from the `CreateHostBuilder()` method – HMZ Jul 08 '20 at 22:53
  • @CodyBouche if this worked then override the default behaviour of `WebApplicationFactory` by inheriting from it and doing your own logic using the instance see [here](https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-3.1#customize-webapplicationfactory) – HMZ Jul 08 '20 at 23:00
  • Does this answer your question? [Programmatically invoking the ASP.NET Core request pipeline](https://stackoverflow.com/questions/50407760/programmatically-invoking-the-asp-net-core-request-pipeline) – PhillipM Dec 04 '20 at 08:49

0 Answers0