没有为X类型注册任何服务,但是服务注册看起来还可以吗? [英] No service for type X has been registered, but service registration looks ok?

查看:166
本文介绍了没有为X类型注册任何服务,但是服务注册看起来还可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET Core 2.0 Web项目的Startup.cs中,我有一些非常简单的代码:

I have some very simple code in Startup.cs in a ASP.NET Core 2.0 web project:

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)

全部

但是,如果我通过移动一行这样的代码来更改ConfigureServices方法:

But, if I change the ConfigureServices method by moving a single line of code like this:

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.

尝试注入已注册的类时,对CreateContainer方法的调用如何导致错误?这些操作之间应该没有任何关系。

How can the call to the CreateContainer method cause an error when trying to inject the registered class? 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.

推荐答案

问题是由在ConfigureServices内部的async方法上调用await引起的方法。我试图替换以下代码行:

The problem is caused by calling await on an async method inside the ConfigureServices method. I tried to replace this line of code:

await CreateContainer();

具有:

await Task.Delay(200);

问题仍然存在。

这是ConfigureServices方法的固定版本:

This is the fixed version of the ConfigureServices method:

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.

这篇关于没有为X类型注册任何服务,但是服务注册看起来还可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆