c# - 尝试使用 .Net Core Framework 3.0 让 .Net Boxed Framework 工作
问题描述
我被要求配置并弄清楚如何使用 .NET 框架 3.0 让 .NET 盒装框架工作。我被困在如何解决这个问题上,因为我以前从未见过它,也不知道如何让它工作。
如果有人有想法或已经让这个框架在 3.0 中工作,我们将不胜感激。
我主要一直在寻找其他使用方法,IServiceProvider
但我想出的唯一解决方案是我必须将所有服务放入一个变量中,并根据错误消息以这种方式注入它
public IServiceProvider ConfigureServices(IServiceCollection services) =>
services
.AddCorrelationIdFluent()
.AddCustomCaching()
.AddCustomOptions(this.configuration)
.AddCustomRouting()
.AddCustomResponseCompression()
.AddCustomStrictTransportSecurity()
.AddCustomHealthChecks()
.AddHttpContextAccessor()
.AddMvcCore()
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddAuthorization()
.AddJsonFormatters()
.AddCustomJsonOptions(this.hostingEnvironment)
.AddCustomCors()
.AddCustomMvcOptions(this.hostingEnvironment)
.Services
.AddCustomGraphQL(this.hostingEnvironment)
.AddCustomGraphQLAuthorization()
.AddProjectRepositories()
.AddProjectSchemas()
.BuildServiceProvider();
这就是错误消息的内容:
从应用程序代码调用“BuildServiceProvider”会导致创建单例服务的附加副本。考虑替代方案,例如依赖注入服务作为“配置”的参数
注意:我没有在框架中添加任何额外的代码,只是试图让它在 3.0 版中没有错误。
如果有人有任何想法帮助将不胜感激。
解决方案
我不确定这是否 100% 正确,因为我现在无法对其进行测试,但这是我为使上述代码部分无错误所做的工作。
public void ConfigureServices(IServiceCollection services)
{
_ = services
.AddCorrelationIdFluent()
.AddCustomCaching()
.AddCustomOptions(this.configuration)
.AddCustomRouting()
.AddCustomResponseCompression()
.AddCustomStrictTransportSecurity()
.AddCustomHealthChecks()
.AddHttpContextAccessor()
.AddMvcCore()
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddAuthorization()
.AddCustomJsonOptions(this.hostingEnvironment)
.AddCustomCors()
.AddCustomMvcOptions(this.hostingEnvironment);
_ = services
.AddCustomGraphQL(this.hostingEnvironment)
.AddCustomGraphQLAuthorization()
.AddProjectRepositories()
.AddProjectSchemas();
_ = services.AddMvc()
.AddNewtonsoftJson();
}
一位同事告诉我要取出.BuildServiceProvider
它,这就是它不再存在的原因如果这是错误的或有人知道更好的方法,请告诉我们:)
推荐阅读
- drake-r-package - 对 drake 工作流程的自定义函数进行单元测试的最佳实践
- angular - 在 Eclipse 中运行 Angular IDE 的文档在哪里
- r - 删除 R (flowCore) 中的 S4 类列
- javascript - InvalidValueError:NaN 不是可接受的纬度值
- connection - PHPMailer & Mailhog:连接被拒绝,主机未知
- mysql - 查询条件中的限制和计数器
- c# - 如何实现与 Thread.Sleep() 相同但异步的结果并保持相同的行为?
- python - Python [0]*n 语法仅适用于不可变对象?
- javascript - 使用 php 和 javascript 时如何在表单中显示错误消息
- scala - 如何在 scala 中初始化泛型变量