简体   繁体   English

没有为X类型注册任何服务,但是服务注册看起来还可以吗?

[英]No service for type X has been registered, but service registration looks ok?

I have some very simple code in Startup.cs in a ASP.NET Core 2.0 web project: 我在ASP.NET Core 2.0 Web项目的Startup.cs中有一些非常简单的代码:

public async void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    await CreateContainer();

    services.AddSingleton<Infrastructure.Logging.Contracts.ILogger, Infrastructure.Logging.Standard.Logger>();
}

private async Task CreateContainer()
{
    string connectionString = Configuration["TestConnectionString"];
    var storageAccount = CloudStorageAccount.Parse(connectionString);
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference("logs");
    await container.CreateIfNotExistsAsync();
}

In my home page view, I have: 在我的主页视图中,我有:

@using Infrastructure.Logging.Contracts
@inject ILogger logger

The problem is that the application crashes (when running the code in the view) with: 问题是应用程序崩溃(在视图中运行代码时)与:

An unhandled exception occurred while processing the request.
InvalidOperationException: No service for type 'Infrastructure.Logging.Contracts.ILogger' has been registered.
Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)

All the code in Startup.cs runs successful with no exceptions. Startup.cs中的所有代码均无例外地成功运行。

But, if I change the ConfigureServices method by moving a single line of code like this: 但是,如果我通过移动单行代码来更改ConfigureServices方法,如下所示:

public async void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    services.AddSingleton<Infrastructure.Logging.Contracts.ILogger, Infrastructure.Logging.Standard.Logger>();

    await CreateContainer();
}

Everything now runs successfully with no error messages. 现在一切都成功运行,没有错误消息。

How can the call to the CreateContainer method cause an error when trying to inject the registered class? 尝试注入已注册的类时,对CreateContainer方法的调用如何导致错误? There should be no relationship whatsoever between these operations. 这些操作之间应该没有任何关系。

Note that the call to register the service runs successful in any case and in the debugger I can see the entry for the registered service by examining the services object. 请注意,注册服务的调用在任何情况下都会成功运行,并且在调试器中,我可以通过检查服务对象来查看已注册服务的条目。 There are no exceptions, which could alter the flow of execution somehow. 没有例外,这可以以某种方式改变执行流程。 I have stepped through in the debugger and checked everything. 我已进入调试器并检查了所有内容。

The problem is caused by calling await on an async method inside the ConfigureServices method. 该问题是由在ConfigureServices方法内的async方法上调用await引起的。 I tried to replace this line of code: 我试图替换此行代码:

await CreateContainer();

with: 与:

await Task.Delay(200);

and the problem still reproduced. 问题仍然存在。

This is the fixed version of the ConfigureServices method: 这是ConfigureServices方法的固定版本:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    CreateContainer().Wait();

    services.AddSingleton<Infrastructure.Logging.Contracts.ILogger, Infrastructure.Logging.Standard.Logger>();
}

I'm not exactly sure why this caused a problem, but it must have caused some sort of synchronization issues in the framework code. 我不确定为什么会导致问题,但是它一定会在框架代码中引起某种同步问题。

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

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