3
votes

I am unable to start up my asp.net core application after trying to port it to 1.0 RC2 from 1.0 RC1. The startup error I get is this:

Unhandled Exception: System.Exception: Could not resolve a service of type 'Microsoft.Extensions.Configuration.IConfiguration' for the parameter 'configuration' of method 'Configure' on type 'MYCOMPANY.MYTHING.Api.Startup'.
   at Microsoft.AspNetCore.Hosting.Startup.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at MYCOMPANY.MYTHING.Api.Program.Main(String[] args)

I can't figure out how this is supposed to work.

The parts I do understand are:

  • My startup.cs code looks correct to me, in Startup(IHostingEnvironment env) I create a ConfigurationBuilder and set Startup.Configuration equal to builder.Build(). The ConfigureServices method is also reached and Configuration object exists there.

  • When the host.Run() code is reached in Program.cs, I get a crash.

  • Configure(app,env,loggerFactory,configuration,respository...) method is NEVER reached.

  • In RC1 there used to be a line like this:

    services.AddInstance<IConfiguration>(Configuration);
    
  • In RC2 that doesn't exist anymore in a new application (I am reading a new application which works and trying to compare line by line).

I have been looking for how to make this work, and how to understand dependency injection of the IConfigurationRoot, but I don't see any attribute or code that seems to be responsible for this injection.

Update: It seems that I was using some strange pattern in my RC1 code that is no longer supported in RC2. After I removed some additional parameters from my Configure() method it was once again invoked by the .net core startup code.

2
I'd like to see the Configure method of your Startup, note that services.AddInstance changed to .AddSingleton for rc2, but you should not need to inject IConfiguration into the Configure method, it should be persisted as a property in Startup contstructor, then you can access the property in Configure methodJoe Audette

2 Answers

4
votes

It looks like you have already fixed the issue, but the restriction with respect to the configure method is not as hard and fast as you suggest.

The restriction is you must have a public, instance or static method named ConfigureDevelopment, where Development is the environment name, or a method named Configure, which will be used if an environment specific configure method does not exist.

Obviously, you shouldn't need to inject your IConfiguration in to the Configure method as it will be set on your Startup class, but if you need something else injected (and you've configured it in ConfigureServices then you can do so. For example, the following would be perfectly valid.

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<ISuperSecretClass, SuperSecretClass>();
}

public void Configure(IApplicationBuilder app, ISuperSecretClass instance)
{
   //do something with instance
}

For reference, I checked in the StartupLoader source for the Configure restrictions.

3
votes

If you want to inject Configuration instance you can do:

services.AddSingleton((provider)=>
{
     return Configuration;
});