简体   繁体   English

测试失败:没有注册“Microsoft.Extensions.DependencyInjection.IServiceProviderFactory`1[Autofac.ContainerBuilder]”类型的服务

[英]Tests fail: No service for type 'Microsoft.Extensions.DependencyInjection.IServiceProviderFactory`1[Autofac.ContainerBuilder]' has been registered

When I run my E2E tests I get the exception:当我运行 E2E 测试时,出现异常:

No service for type 'Microsoft.Extensions.DependencyInjection.IServiceProviderFactory`1[Autofac.ContainerBuilder]' has been registered.

I've followed the official Autofac documentation of implementing the Autofac in projects using .NET versions 3+ and I can run the solution just fine, but my tests fail.我遵循在使用 .NET 版本 3+ 的项目中实现 Autofac 的官方 Autofac 文档,我可以很好地运行该解决方案,但我的测试失败了。

What's weird is that when I use a solution code from this topic , it's the opposite - I can't run the solution (returning IServiceProvider is invalid), but my tests succeed:奇怪的是,当我使用本主题中的解决方案代码时,情况正好相反 - 我无法运行该解决方案(返回 IServiceProvider 无效),但我的测试成功了:

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddMvc(options => options.EnableEndpointRouting = false)
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

    services.AddCustomDbContext(Configuration);
    services.AddOptions();

    var container = new ContainerBuilder();
    container.Populate(services);
    return new AutofacServiceProvider(container.Build());
}

What am I doing wrong?我究竟做错了什么?

After a long battle I managed to get it to work, all thanks to this article .经过长时间的战斗,我设法让它工作,这一切都归功于这篇文章

Your application needs to implement Autofac in .NET 3.0+ way and you need to look at steps "Adding XUnit logging with WebApplicationFactory in ASP.NET Core 2.x" and "Using WebApplicationFactory in ASP.NET Core 3.0" from the article to make tests work, ie add the tests fixture and use it instead to create a client.您的应用程序需要以 .NET 3.0+ 方式实现 Autofac,您需要查看文章中的步骤“在 ASP.NET Core 2.x 中使用 WebApplicationFactory 添加 XUnit 日志记录”和“在 ASP.NET Core 3.0 中使用 WebApplicationFactory”来制作测试工作,即添加测试装置并使用它来创建客户端。

暂无
暂无

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

相关问题 没有注册“IServiceProviderFactory[Autofac.ContainerBuilder]”类型的服务 - No service for type 'IServiceProviderFactory[Autofac.ContainerBuilder]' has been registered 更改MVC主页登录页面:没有为“Microsoft.Extensions.DependencyInjection.IServiceCollection”类型提供服务 - Change MVC Home Landing Page: No service for type 'Microsoft.Extensions.DependencyInjection.IServiceCollection' has been registered 没有注册“Microsoft.Extensions.Logging.ILoggingBuilder”类型的服务 - No service for type 'Microsoft.Extensions.Logging.ILoggingBuilder' has been registered 没有注册类型'Microsoft.Framework.DependencyInjection.ServiceLookup.IServiceManifest'的服务 - No service for type 'Microsoft.Framework.DependencyInjection.ServiceLookup.IServiceManifest' has been registered 没有注册“Microsoft.Extensions.Http.DefaultHttpClientFactory”类型的服务 - No service for type 'Microsoft.Extensions.Http.DefaultHttpClientFactory' has been registered 无法使用 dotnet 核心和 autofac 将 ServiceCollection 类型的对象转换为类型“Autofac.ContainerBuilder” - Unable to cast object of type ServiceCollection to type 'Autofac.ContainerBuilder' using dotnet core and autofac “ InvalidOperationException:没有注册类型'Microsoft.Extensions.Configuration.IConfiguration'的服务。” - “InvalidOperationException: No service for type 'Microsoft.Extensions.Configuration.IConfiguration' has been registered.” 没有为类型“Microsoft.Extensions.Hosting.IHostEnvironment”注册的服务 net core 6.0 - No service for type 'Microsoft.Extensions.Hosting.IHostEnvironment' has been registered net core 6.0 单元测试模拟 ControllerContext HttpContext 没有为类型“Microsoft.AspNetCore.Mvc.View...ITempDataDictionaryFactory”注册服务 - Unit Tests Mock ControllerContext HttpContext No service for type 'Microsoft.AspNetCore.Mvc.View...ITempDataDictionaryFactory' has been registered Autofac - 请求的服务 (lSomeDbContext) 尚未注册 - Autofac - The requested service (lSomeDbContext) has not been registered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM