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

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

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

Application startup exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary2.get_Item(TKey key) at Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(String dependency) at Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(String dependency) at Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(String dependency) at Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.d__4.MoveNext() at System.Linq.Enumerable.d__172.MoveNext() at System.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 services) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() Hosting environment: Staging Content root path: C:\inetpub\wwwroot\SIR Now listening on: http://localhost:33007 Application started. Press Ctrl+C to shut down.

更新: 它掉落的那条线是;

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>();
    }

. . .

最佳答案

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

关于c# - 如何在 Web Core API 中调试启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50602503/

相关文章:

c# - ASP.NET Core Web API 将所有枚举作为第 0 个选项

asp.net-core - 在 .net core web api 中发送自定义错误响应的最佳方法是什么

c# - 有没有一种简单的方法可以将 32 位整数转换为 16 位整数?

c# - Azure Blob 存储 : How to enumerate blobs in long-running and recoverable fashion?

c# - 在 Windows 服务中持久化变量的公认方法是什么

c# - 按日期部分分组 (dd.mm.yyyy)

sql-server - 让 IIS 在内网环境中模拟 Windows 用户访问 SQL Server

c# - .cs 文件是否可以覆盖已编译的 Asp.net 站点的 bin 文件中的代码?

C# 不信任已安装的证书

c# - 将 ZipArchive 与 ASP.NET Core Web Api 结合使用