I am trying to build a test integration suite with Microsoft's TestServer
class to test two dotnet web REST API services based on current dotnet 7 SDK in a single XUnit test.
For the sake of better understanding the issue let's call the services appService and idpService. The appService needs to send auth web HTTP requests to idpService hence in my XUnit test I have to have both HttpClients for both servers available and the appService's config needs to point to config of idpService which uses an IdentityServer4 impl. to facilitate OAuth authentication services.
Now, I have not yet reached the point of testing auth logic, but have already hit an issue where an HTTP route can not be matched exception: Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The appService's HttpClient instance web HTTP request matched multiple endpoints.
This happens as I have two very simple endpoints ie. handlers for GET /status
in both web API services used for a sanity health check; their purpose is irrelevant to this issue.
PROBLEM: Now, from what I understand, it seems that even tho I use appHtppClient
(ApiClient
in screenshot below) instance to send the web HTTP GET request to /status
(instantiated from appServiceServer.CreateClient()
) there is some kind of sharing of resources somewhere involved in between the running TestServer instances. It seems the servers share the address space. Now very funny thing is the same issue DOES NOT take place if I swap the HttpClient to idpService's one (note I tested changing the route to eg. status2 on idpService just for sanity, and then when switched code back again to use appClient to issue a web HTTP request it matches status2(!) and fails as expected due to IoC/DI issues; unrelated to this topic - screenshot below).
I tried changing the BaseAddress
on servers and setting up in builder code different IPs like: .ConfigureKestrel(opts => opts.ListenAnyIP(6000))
and then other one: .ConfigureKestrel(opts => opts.ListenAnyIP(6001))
.
Any ideas to make this work? Hints? Background on why is this?
I am wondering if anyone has hit this issue. I will probably create a repro repository and maybe add later.