Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass data to startup.cs

Tags:

c#

.net-core

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?

like image 269
ChrisCa Avatar asked Jun 23 '17 12:06

ChrisCa


People also ask

What does startup CS file do?

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.

What ConfigureServices () method does in startup CS?

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.

What is startup Cs in .NET Core?

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.


1 Answers

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.

like image 155
Sami Kuhmonen Avatar answered Sep 20 '22 07:09

Sami Kuhmonen