简体   繁体   English

ASP.NET Core RC2中的依赖项注入失败

[英]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. 尝试将其从1.0 RC1移植到1.0 RC2后,无法启动asp.net核心应用程序。 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(). 我的startup.cs代码对我来说看起来是正确的,在Startup(IHostingEnvironment env)中,我创建一个ConfigurationBuilder并将Startup.Configuration设置为builder.Build()。 The ConfigureServices method is also reached and Configuration object exists there. 还到达ConfigureServices方法,并且那里存在Configuration对象。

  • When the host.Run() code is reached in Program.cs, I get a crash. 在Program.cs中访问host.Run()代码时,我崩溃了。

  • Configure(app,env,loggerFactory,configuration,respository...) method is NEVER reached. 永远不会达到Configure(app,env,loggerFactory,configuration,repository ...)方法。

  • In RC1 there used to be a line like this: 在RC1中,以前是这样的一行:

     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). 在RC2中,新应用程序中不再存在(我正在阅读一个有效的新应用程序,并尝试逐行比较)。

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. 我一直在寻找如何使之工作以及如何理解IConfigurationRoot的依赖项注入的方法,但是我看不到任何似乎对此注入负责的属性或代码。

Update: It seems that I was using some strange pattern in my RC1 code that is no longer supported in RC2. 更新:似乎我在RC1代码中使用了一些奇怪的模式,而RC2不再支持该模式。 After I removed some additional parameters from my Configure() method it was once again invoked by the .net core startup code. 从Configure()方法中删除了一些其他参数后,.net核心启动代码再次调用了它。

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. 看来您已经解决了该问题,但是对configure方法的限制并不像您建议的那样困难和快速。

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. 该限制是你必须有一个公共的,实例或命名的静态方法ConfigureDevelopment ,其中发展是环境名称,或命名方法Configure ,如果环境的具体配置方法不存在将被使用。

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. 显然,您不需要将IConfiguration注入到Configure方法中,因为它将在您的Startup类上进行设置,但是如果您需要注入其他内容(并且您已经在ConfigureServices对其进行了ConfigureServices则可以这样做)。 ,以下内容将完全有效。

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. 作为参考,我在StartupLoader源中检查了Configure限制。

If you want to inject Configuration instance you can do: 如果要注入Configuration实例,可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM