首页 > 解决方案 > 尝试使用 .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 版中没有错误。

如果有人有任何想法帮助将不胜感激。

标签: c#asp.netframeworksgraphql

解决方案


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


推荐阅读