I am trying to host my api project using WebApplicationFactory. I have overriden ConfigureWebHost and added my desired url in UseUrls() method. I am setting the app to host on http://localhost:5000, but it is inaccessible from browser. The only way to call endpoints in the host is via httpClient created by CreateClient() method in WebApplicationFactory instance.
public class ApiHubFactory<TEntryPoint> : WebApplicationFactory<TEntryPoint> where TEntryPoint : class
{
public string HostUrl { get; set; } = "https://localhost:5001";
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseUrls("http://localhost:5000", HostUrl);
//Adding fake authentication scheme to bypass JWT authentication
builder.ConfigureServices(services =>
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = TestAuthHandler.AuthenticationScheme;
options.DefaultScheme = TestAuthHandler.AuthenticationScheme;
options.DefaultChallengeScheme = TestAuthHandler.AuthenticationScheme;
})
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>(TestAuthHandler.AuthenticationScheme, options => { });
});
}
}
I have tried to find out on what url the host is starting looking in httpclient. Despite I have set it to be http://localhost:5000, it is 127.0.0.1:80, which is also inaccessible from browser.
Also I have tried using settings, the answer is from How to configure url on Host created by WebApplicationFactory?
public class CustomWebApplicationFactory<TStartup>
: WebApplicationFactory<TStartup> where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
// Notice there is no `--` prefix in "urls"
builder.UseSetting("urls", "http://localhost:1234");
// other stuff as usual
}
}