【问题标题】:Trying to get .Net Boxed Framework to work Using .Net Core Framework 3.0尝试使用 .Net Core Framework 3.0 让 .Net Boxed Framework 工作
【发布时间】:2019-09-18 21:08:42
【问题描述】:

我被要求配置并弄清楚如何使用 .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 版中没有错误。

如果有人有任何想法,我们将不胜感激。

【问题讨论】:

标签: c# asp.net frameworks graphql


【解决方案1】:

我不确定这是否 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,这就是为什么它不再存在如果这是错误的或有人知道更好的方法,请告诉我们:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多