How do you pass data into startup.cs ?
This is for integration testing using WebHostBuilder
and TestServer
I need to pass different data depending on the Test Fixture. So dont want to pull it in from a config file, for example
The data will be supplied to a registered Middleware in startup.cs
Docs seem to suggest this should work:
var configBuilder = new ConfigurationBuilder()
.AddInMemoryCollection(new[]
{
new KeyValuePair<string, string>("key", "value"),
});
var configuration = configBuilder.Build();
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseConfiguration(configuration) // config added here
.UseStartup<Startup>()
.Build();
host.Run();
but when I inspect the Configuration object in startup.cs, the key is not present. And only the providers defined in startup.cs are available.
I am trying to do this in program.cs at the moment to test the concept, then will move to integration tests later
any ideas what I'm doing wrong?
Is there a better way to pass data into startup?
When doing so, the app will include a Startup. cs file that is responsible for setting up request middleware in a way that's very similar to how ASP.NET Core behaves. If you need to run code when your ASP.NET MVC app starts up, it will typically use one of these approaches.
The startup class contains two methods: ConfigureServices(): Registers the services that your application will need. Configure(): Configures the middleware pipeline that controls how the application processes the HTTP requests and sends the response.
The Startup class in .NET and .NET CoreThe Startup class contains the ConfigureServices and Configure methods. While the former is used to configure the required services, the latter is used to configure the request processing pipeline. The Configure method is executed immediately after the ConfigureServices method.
One way to send data into the Startup
would be to register a service in Main
. WebHostBuilder
has ConfigureServices
method which can be used just like the ConfigureServices
method you can implement in the Startup
class.
For example you can make a class with static variables (not the best idea but works)
public class DataContainer
{
public static string Test;
}
Then set its values and add it as a singleton service
DataContainer.Test = "testing";
var host = new WebHostBuilder()
.ConfigureServices(s => { s.AddSingleton(typeof(DataContainer)); })
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseConfiguration(configuration) // config added here
.UseStartup<Startup>()
.Build();
After this your Startup
can just use the regular injection way to get this
public Startup(IHostingEnvironment env, DataContainer data)
{
// data.Test is available here and has the value that has been set in Main
}
The injection of course works in any class and method after this, not just the constructor.
I'm not sure if this is any better than to actually create a class with static values by itself but if the class needs to be changed sometimes it can be made into an interface and the other usual injection benefits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With