Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

TestServer w/ WebHostBuilder doesn't read appsettings.json on ASP.NET Core 2.0, but it worked on 1.1 #1191

Closed
natemcmaster opened this issue Aug 30, 2017 · 7 comments

Comments

@natemcmaster
Copy link
Contributor

From @neooleg on August 30, 2017 19:59

STR

  1. Use .NET 2.0
  2. Follow official manual to create integration tests
  3. Use the following code for test server
_server = new TestServer(new WebHostBuilder()
    .UseEnvironment("Development")
    .UseContentRoot(projectDir)
    .UseStartup<TestStartup>());

h3. AR

  • appsettings.json doesn't read and TestStartup doesn't receive them.
  • It looks like breaking changes (not found anything documented it) because the same code worked on ASP.NET Core 1.1

h3. ER
appsettings.json should be read and passed into Startup.

Copied from original issue: aspnet/KestrelHttpServer#2042

@Tratcher
Copy link
Member

@neooleg WebHostBuilder and TestServer do not read appsettings.json in any scenarios. CreateDefaultBuilder is the only component that includes appsettins.json by default:
https://github.com/aspnet/MetaPackages/blob/dev/src/Microsoft.AspNetCore/WebHost.cs#L148

@neooleg
Copy link

neooleg commented Aug 30, 2017

@Tratcher I see, thanks!

What is idiomatic way to read appsettings.json in tests?

Except doing it explicit (which is might be good depending on case):

_server = new TestServer(new WebHostBuilder()
    .UseEnvironment("Development")
    .UseContentRoot(projectDir)
    .UseConfiguration(new ConfigurationBuilder()
        .SetBasePath(projectDir)
        .AddJsonFile("appsettings.json")
        .Build()
    )
    .UseStartup<TestStartup>());

@Tratcher
Copy link
Member

It needs to be explicit in all apps and tests unless you call CreateDefaultBuilder.

@Tratcher
Copy link
Member

Note there is a ConfigureAppConfgiuration method that is a little easier than UseConfiguration.

@neooleg
Copy link

neooleg commented Aug 30, 2017

Do you recommend to use CreateDefaultBuilder() in tests as well?

@Tratcher
Copy link
Member

It depends on what you're testing. If you're trying to mimic a full application then it may make sense.

@neooleg
Copy link

neooleg commented Aug 30, 2017

Thanks a lot, @Tratcher!

P.S. The issue might be closed from my point of view.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants