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

时间:2018-05-30 10:35:43

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

我有一个使用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:字典中不存在给定的键。 at System.Collections.Generic.Dictionary2.get_Item(TKey key)        在Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(String dependency)        在Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(String dependency)        在Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(String dependency)        在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服务)在Properties.API.Startup.ConfigureServices(IServiceCollection services)---从抛出异常的先前位置开始的堆栈跟踪---在Microsoft.AspNetCore的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()处。 Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()中的Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()中的Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection服务)主机环境:暂存内容根路径: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<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>();
    }

。 。 。

1 个答案:

答案 0 :(得分:2)

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