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

Is there a way to parameterize and pass an instance of Startup rather than just the type? #333

Closed
Tratcher opened this issue Aug 16, 2015 · 9 comments
Assignees
Milestone

Comments

@Tratcher
Copy link
Member

From @humblelistener on August 14, 2015 5:8

The intention is to avoid creating a separate TestStartUp class rather - rather inject an instance of Configuration

Sample start up below,

public class Startup
{
  public IConfiguration Configuration {get; set;}

  public void Startup()
  {
    Configuration = new ConfigurationBuilder()
              .AddEnvironmentVariables()
              .Build();
   }

  public void ConfigureServices(IServiceCollection services)
  {
     //uses Configuration object to set up everything
  }
}

In the above implementation, if only

TestServer.CreateBuilder().UseStartup(instanceOfStartup)
is permitted

I can do this,

Startup instanceOfStartup= new Startup();
instanceOfStartup.Configuration = myOwnconfigurationWithKeysRequiredForTest;
//create a test server with 
TestServer.CreateBuilder().UseStartup(instanceOfStartup) 

Advantage: I dont have to maintain multiple startup class.

Does it make sense?

Copied from original issue: dotnet/aspnetcore#831

@Tratcher
Copy link
Member Author

How about the UseStartup(Action<IApplicationBuilder>) overload? So your sample above becomes: TestServer.CreateBuilder().UseStartup(instanceOfStartup.Configure)

@humblelistener
Copy link

These overloads are limited - It wont work because

  1. Configure method can be using just IApplicationBuilder or taking ILoggerFactory and so on as parameters
  2. If I then have a configure services method - it complicates further because the overload pointed out takes a different signature of configureservices compared to the Startup class

Instead of trying to draw two parallels - using StartUp class and StartUpMethods and having supported UseStartUp extension method, IMHO - it will be simple and easy to just start support taking an instance of StartUp thats got environment specific configuration as state in it.

@humblelistener
Copy link

and this is what I have to do to keep it working - right now

var startup = new Startup();

//update configuration
startup.Configuration["XYZ_ENV_VAR"] = testEnvValue;

var Action<IApplicationBuilder> configureMethod = (app) => {
  app.UseMvc();
};

var configureServicesStartupMethodInfo = typeof(Startup).GetMethod("ConfigureServices");

var configureServicesMethod = new ConfigureServicesBuilder(configureServicesStartupMethodInfo).Build(startup);

using (var testServer = new TestServer(TestServer.CreateBuilder().UseStartup(configureMethod, configureServicesMethod)))
{
  ...
}

Doesn't feel good! any suggestions?

@HaoK
Copy link
Member

HaoK commented Aug 25, 2015

Just to clarify the scenario you are trying to accomplish, you mostly want a way to configure a test to run with specific configuration (i.e. just tweak the configuration per test?)

@humblelistener
Copy link

that is right, I would prefer to keep the same startup while injecting a different configuration compared to having two startup files or having conditions in the start up file.

@muratg
Copy link

muratg commented Jan 21, 2016

Moving this to Backlog as we will be in RC2 ask mode very soon. If you feel strongly about this issue, please ping me.

@davidfowl davidfowl self-assigned this Apr 19, 2016
@davidfowl
Copy link
Member

Fixed with 8f5f8d2 which is post RC2. You can add an IStartup class to the DI container and that will get resolved for app startup.

@davidfowl
Copy link
Member

davidfowl commented Apr 19, 2016

PS: it would look like this:

Startup instanceOfStartup= new Startup();
instanceOfStartup.Configuration = myOwnconfigurationWithKeysRequiredForTest;
//create a test server with 
var builder = new WebHostBuilder()
                      .ConfigureServices(services =>
                       {
                            services.AddSingleton<IStartup>(instanceOfStartup);
                       });
var server = new TestServer(builder);

@ohadschn
Copy link

And here's a nice blog post about the new IStartup interface: https://www.strathweb.com/2017/06/resolving-asp-net-core-startup-class-from-the-di-container/.

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

No branches or pull requests

7 participants