【问题标题】:How to debug startup in Web Core API?如何在 Web Core API 中调试启动?
【发布时间】:2018-11-09 04:19:33
【问题描述】:

我有一个使用 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)并收到此错误消息;

应用程序启动异常:System.Collections.Generic.KeyNotFoundException:字典中不存在给定的键。在 System.Collections.Generic.Dictionary2.get_Item(TKey 键) 在 Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(字符串依赖) 在 Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(字符串依赖) 在 Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(字符串依赖) 在 Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.d__4.MoveNext() 在 System.Linq.Enumerable.d__172.MoveNext() 在 System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() 在 Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.GetApplicationPartManager(IServiceCollection services) 在 Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore (IServiceCollection 服务)在 Properties.API.Startup.ConfigureServices(IServiceCollection 服务)---从以前抛出异常的位置结束堆栈跟踪---在 Microsoft.AspNetCore 的 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()。 Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) 在 Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() 在 Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() 托管环境:暂存内容根路径:C:\inetpub\ wwwroot\SIR 现在正在监听:http://localhost:33007 应用程序已启动。按 Ctrl+C 关闭。

更新: 它跌倒的线是;

services.AddMvcCore().AddJsonFormatters();

在配置服务中。

如何调试它以找出导致此问题的原因?

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<IISOptions>(options => new IISOptions
        {
            AutomaticAuthentication = true,
            ForwardClientCertificate = false,
            ForwardWindowsAuthentication = false
        });
        var connectionStringMSurveyV2 = Configuration.GetConnectionString("MSurveyV2Db");
        services.AddScoped<MSurveyV2Db>(_ => new MSurveyV2Db(connectionStringMSurveyV2));
        var connectionStringSir = Configuration.GetConnectionString("SirDb");
        services.AddScoped<SirDb>(_ => new SirDb(connectionStringSir));
        services.AddScoped<IPropertiesRepo, PropertiesRepo>();
        services.AddScoped<ISirUoW, SirUoW>();
        services.AddScoped<IPropertyUoW, PropertyUoW>();
        services.AddScoped<Services.IMailService, Services.MailService>();
    }

。 . .

【问题讨论】:

  • ConfigureServices 内部发生异常,代码未显示。
  • 我想知道你是怎么做到的?尽管如此,我还是发布了 ConfigureServices。
  • 它在您发布的堆栈跟踪中这么说:“在 Properties.API.Startup.ConfigureServices(...)”
  • AddMvcCore 抛出异常。你知道AddMvcCoreAddMvc 之间的区别吗?为什么要专门使用AddMvcCore
  • 所以你真的不知道区别。因为,AddMvcCore 中的Core 基本上意味着“只添加核心组件”,而不是“添加asp.net Core”。虽然AddMvc 在内部也调用了AddMvcCore,但在此之上还添加了更多必要的组件。

标签: c# iis asp.net-core-webapi


【解决方案1】:

您没有说您使用的是哪个版本的 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<Startup>()
            .UseNLog()
            .Build();
    }
}

【讨论】:

  • 抱歉没有说明我使用的是版本 1
猜你喜欢
  • 2020-10-21
  • 2018-12-09
  • 2021-11-20
  • 2017-05-02
  • 2016-12-14
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多