[BUG] Dynamic API endpoint creation with dynamic url is slow
Describe the bug I use Refit with different host URLs, which come dynamically from the database. To accomplish this, I have created a factory service, which creates the Refit Endpoints dynamically based on the URL of the database. The problem which I encountered, is that the first API call with this newly created endpoint object is really slow (more than 40 seconds). If I debug the same code with a literal string URL instead of an URL from the database, I don't encounter this slowness.
//Slow
Project project = await projectService.GetSelectedProject();
var endpointWithDynamicUrl = Refit.RestService.For<TApiEndpoint>(project.Url, new Refit.RefitSettings()
{
AuthorizationHeaderValueGetter = this.apiAuthService.GetToken
});
//Fast
var endpointWithLiteralUrl = Refit.RestService.For<TApiEndpoint>("https://example.com/", new Refit.RefitSettings()
{
AuthorizationHeaderValueGetter = this.apiAuthService.GetToken
});
Steps To Reproduce
Expected behavior
That dynamically created API endpoint objects with a dynamic URL source doesn't cause a slow endpoint call.
Screenshots
Environment
- OS: Windows
- Device: Lenovo thinkpad p1
- Version:
- Working Version:
Additional context
If the URL in the database is a local URL, the first call is much faster.
Any suggestions for improvement? It doesn't look like a bug!
I finally found the problem. The underlying HttpClient wasn't able to resolve my IPv6 address (which was wrongly configured in my DNS). It takes more than half a minute for the HttpClient to figure out IPv6 won't work and to switch to IPv4. To conclude the problem wasn't in Refit.
I just hit this same issue, also thinking that Refit might be the culprit. For me it did not take half a minute, just a few seconds (but very noticeable at application startup).
What I ended up doing is disabling IPv6 by adding the following runtime configuration option in my application csproj:
<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Net.DisableIPv6" Value="true" />
</ItemGroup>
After disabling IPv6, startup time went from ~2.5 seconds to ~0.2 seconds.
See also Forcing HttpClient to use IPv4 or IPv6 addresses by @meziantou and .NET 6 Networking Improvements (An option to globally disable IPv6).
Addendum:
Turns out the server was listening on http://127.0.0.1:5000 and the client was connecting to http://localhost:5000 so by changing the server configuration to listen on localhost the slowness issue disappeared without having to disable IPv6.
Same conclusion as Luuk: no problem in Refit!
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.