1

我将一个 asp.net 核心项目从 2.2 版迁移到 3.1 版。我已经添加了所有依赖项和 docker 相关组件。它编译得很好,但是在运行项目时我得到了下面提到的错误:

引发异常:Microsoft.Extensions.DependencyInjection.dll 中的“System.AggregateException”Microsoft.Extensions.DependencyInjection.dll 中发生“System.AggregateException”类型的未处理异常:“无法构造某些服务”堆栈跟踪:在Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable 1 serviceDescriptors, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder) at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter1.CreateServiceProvider(Object containerBuilder) 在 Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() 在 Microsoft.Extensions.Hosting.HostBuilder.Build() 在 Sample.API.Program。 /src/QueryStack/Sample.API/Program.cs:line 20 中的 Main(String[] args) 程序“kubectl.exe”已退出,代码为 -1 (0xffffffff)。

这是我的代码:

程序.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Sample.API
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Use the W3C Trace Context format to propagate distributed trace identifiers.
            // See https://devblogs.microsoft.com/aspnet/improvements-in-net-core-3-0-for-troubleshooting-and-monitoring-distributed-apps/
            Activity.DefaultIdFormat = ActivityIdFormat.W3C;
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) => 
            Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseKestrel(options => options.AddServerHeader = false);
                webBuilder.UseStartup<Startup>();
            });
    }
}

启动.cs

using Sample.API.Constants;
using Sample.API.Extensions;
using Sample.API.GraphQL;
using Sample.API.Middleware;
using Boxed.AspNetCore;
using GraphQL.Server;
using GraphQL.Server.Ui.Playground;
using GraphQL.Server.Ui.Voyager;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Sample.API
{
    public class Startup
    {
        private readonly IConfiguration configuration;
        private readonly IWebHostEnvironment webHostEnvironment;
        /// <summary>
        /// Initializes a new instance of the <see cref = "Startup"/> class.
        /// </summary>
        /// <param name = "configuration">The application configuration, where key value pair settings are stored. See
        /// http://docs.asp.net/en/latest/fundamentals/configuration.html</param>
        /// <param name = "webHostEnvironment">The environment the application is running under. This can be Development,
        /// Staging or Production by default. See http://docs.asp.net/en/latest/fundamentals/environments.html</param>
        public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
        {
            this.configuration = configuration;
            this.webHostEnvironment = webHostEnvironment;
        }

        /// <summary>
        /// Configures the services to add to the ASP.NET Core Injection of Control (IoC) container. This method gets
        /// called by the ASP.NET runtime. See
        /// http://blogs.msdn.com/b/webdev/archive/2014/06/17/dependency-injection-in-asp-net-vnext.aspx
        /// </summary>
        public virtual void ConfigureServices(IServiceCollection services) => 
            services
            .AddCosmosDBConfiguration(configuration)
            .AddAutoMapperConfiguration()
            .AddCustomResponseCompression(configuration)
            .AddCustomCors()
            .AddCustomOptions(configuration)
            .AddHttpContextAccessor()
            .AddCustomRouting()
            .AddCustomStrictTransportSecurity()
            .AddCustomHealthChecks()
            .AddServerTiming()
            .AddControllers()
               .AddCustomJsonOptions(webHostEnvironment)
               .AddCustomMvcOptions(configuration)
            .Services
            .AddCustomGraphQL(configuration, webHostEnvironment)
            .AddGraphQLResolvers()
            .AddGraphQLResponse()
            .AddProjectRepositories()
            .AddProjectSchemas();

        /// <summary>
        /// Configures the application and HTTP request pipeline. Configure is called after ConfigureServices is
        /// called by the ASP.NET runtime.
        /// </summary>
        public virtual void Configure(IApplicationBuilder application) =>
            application
                .UseIf(
                    this.webHostEnvironment.IsDevelopment(),
                    x => x.UseServerTiming())
            .UseForwardedHeaders()
            .UseResponseCompression()
            .UseFetchLocaleMiddleware()
            .UseIf(
                    !this.webHostEnvironment.IsDevelopment(),
                    x => x.UseHsts())
                .UseIf(
                    this.webHostEnvironment.IsDevelopment(),
                    x => x.UseDeveloperExceptionPage())
            .UseRouting()
                .UseCors(CorsPolicyName.AllowAny)
            .UseEndpoints(
                    builder =>
                    {
                        builder
                            .MapHealthChecks("/status")
                            .RequireCors(CorsPolicyName.AllowAny);
                        builder
                            .MapHealthChecks("/status/self", new HealthCheckOptions() { Predicate = _ => false })
                            .RequireCors(CorsPolicyName.AllowAny);
                    })
            .UseWebSockets()
                // Use the GraphQL subscriptions in the specified schema and make them available at /graphql.
                .UseGraphQLWebSockets<MainSchema>()
                // Use the specified GraphQL schema and make them available at /graphql.
                .UseGraphQL<MainSchema>()
                .UseIf(
                    this.webHostEnvironment.IsDevelopment(),
                    x => x
                        // Add the GraphQL Playground UI to try out the GraphQL API at /.
                        .UseGraphQLPlayground(new GraphQLPlaygroundOptions() { Path = "/" })
                        // Add the GraphQL Voyager UI to let you navigate your GraphQL API as a spider graph at /voyager.
                        .UseGraphQLVoyager(new GraphQLVoyagerOptions() { Path = "/voyager" }));
    }
}

我遵循了以下提到的模板指南: https ://github.com/Dotnet-Boxed/Templates/blob/master/Docs/GraphQL.md

基于以上内容,我创建了一个工作正常但不是上述代码的 POC。

任何人都可以通过提供他们的指导来帮助我解决这个问题

4

2 回答 2

2

一般来说:

  • 在 Visual Studio 中,勾选异常设置窗口以中断所有 CLR 异常
  • 它现在将向您显示该行以及哪个服务失败

因此,当从哪个服务调用时,哪个服务失败就很清楚了,例如

  • 忘记一个“我”
  • 忘记在选项参数之前放置IOptions<>或放置IOptionsMonitor<>
  • ETC
于 2021-12-21T19:57:40.987 回答
0

检查您的项目是否已经在某个终端或后台运行。当我在调试中运行项目而它已经在终端中运行时,我遇到了同样的问题。

于 2020-03-28T15:28:32.007 回答