c# - 如何解析在 ConfigureService 中注册的服务并在 .NET Core 3.0 中的 Autofac ConfigureContainer 方法中使用
问题描述
我的挑战是解决IHttpContextAccessor
我在ConfigureServices
方法中注册的服务,并使用它在方法上使用 Autofac 注册实例ConfigureContainer
。但是随着在 .net core 3.0 中使用 Autofac 的新方法,我发现这样做真的很有挑战性。我尝试的方法如下;
public void ConfigureContainer(ContainerBuilder builder)
{
var container = builder.Build();
var httpContextAccessor = container.Resolve<IHttpContextAccessor>();
var executionContextAccessor = new ExecutionContextAccessor(httpContextAccessor);
ServicesAutofacExtensions.RegisterServicesWithAutofac(executionContextAccessor, Configuration, _loggerForApi);
}
但是当我运行时它会抛出这个错误
访问 Microsoft.Extensions.Hosting 服务时出错。在没有应用程序服务提供商的情况下继续。错误:Build() 或 Update() 只能在 ContainerBuilder 上调用一次。
现在我知道在文档中,它说 ContainerBuilder 不应该以这种方法构建,工厂会自动处理这个问题。但是我该如何解析 IHttpContextAccessor 以在 ConfigureContainer 中使用它?我已经搜索并研究了可能的解决方案,但没有找到。请帮忙。
解决方案
推荐阅读
- rust - 如何在不克隆的情况下使用 Itertools::group_by 或错误“无法移出共享引用后面的值”?
- python - 如何打印真阳性、假阳性等的结果
- python - 如何从类中的 __repr__ 返回列表?
- java - 使用 ByteBuddy 的条件方法委托
- python-3.x - Azure TimerTrigger 具有不同配置的多个实例
- javascript - 如何使用 React 应用程序删除 firebase 中的文档
- facebook - 权限未显示在 Facebook 登录屏幕上
- python - 将条件分配给数据框时出错
- apache-kafka - 如何与 Kafka KUDO 内部设置交谈?
- ruby-on-rails - 删除rails中的默认范围