如何在Web Core API中调试启动?

我有一个使用Web Core API的MVC网站。 在进行了微小的更改和部署后,我意外地收到了错误; 响应状态代码不表示成功:500(内部服务器错误)。 所以我启用了Web Core API的日志文件(请参阅https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/troubleshoot?view=aspnetcore-2.0#aspnet-core -module-stdout-log )我收到此错误消息;

应用程序启动exception:System.Collections.Generic.KeyNotFoundException:给定的键不存在于字典中。 Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(String依赖项)中的Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(String依赖项)中的System.Collections.Generic.Dictionary2.get_Item(TKey键)在System.Linq.Enumerable.d__172.MoveNext()的Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(String dependency)中的Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.d__4.MoveNext() .Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.GetApplicationPartManager(IServiceCollection services)at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection services)at Properties.API.Startup.ConfigureServices(IServiceCollection) SER 恶意)—抛出exception的前一个位置的堆栈跟踪结束—在Microsoft.AspNetCore的Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection服务)的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()处。 Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()中的Hosting.Internal.WebHost.EnsureApplicationServices()托管环境:暂存内容根路径:C:\ inetpub \ wwwroot \ SIR现在正在侦听: http:// localhost:33007申请开始了。 按Ctrl + C关闭。

更新:它倒下的线是;

services.AddMvcCore()AddJsonFormatters();

在ConfigureServices中。

我如何调试它以找出导致这种情况的原因?

public class Startup { public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appSettings.json", optional: false, reloadOnChange: true) .AddJsonFile($ "appSettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables(); Configuration = builder.Build(); } 



  public void ConfigureServices(IServiceCollection services) { services.AddCors(); services.AddMvcCore().AddJsonFormatters(); services.Configure(options => new IISOptions { AutomaticAuthentication = true, ForwardClientCertificate = false, ForwardWindowsAuthentication = false }); 
  var connectionStringMSurveyV2 = Configuration.GetConnectionString("MSurveyV2Db"); services.AddScoped(_ => new MSurveyV2Db(connectionStringMSurveyV2)); var connectionStringSir = Configuration.GetConnectionString("SirDb"); services.AddScoped(_ => new SirDb(connectionStringSir)); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); } 

。 。 。

您没有说明您使用的是哪个版本的ASP.NET Core。 如果它是2.0+,您可以在Program.cs配置NLog以在启动之前加载:

 public static class Program { public static void Main(string[] args) { var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); try { var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("hosting.json", optional: true) .Build(); BuildWebHost(args, config).Run(); } catch (System.Exception ex) { logger.Error(ex, "An error occurred during program startup"); throw; } } private static IWebHost BuildWebHost(string[] args, IConfigurationRoot config) { return WebHost.CreateDefaultBuilder(args) .UseConfiguration(config) .UseStartup() .UseNLog() .Build(); } }