Warren  P
Warren P

Reputation: 68862

Dependency injection failure in ASP.NET Core RC2

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:

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.

Upvotes: 3

Views: 3246

Answers (2)

Sock
Sock

Reputation: 5413

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.

Upvotes: 4

adem caglin
adem caglin

Reputation: 24063

If you want to inject Configuration instance you can do:

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

Upvotes: 3

Related Questions