Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug startup in Web Core API?

I have an MVC website that uses a Web Core API. After making a minor change and a deployment I unexpectedly got an error; Response status code does not indicate success: 500 (Internal Server Error). So I enabled the log file for the Web Core API (see https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/troubleshoot?view=aspnetcore-2.0#aspnet-core-module-stdout-log) and I get this error message;

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.

UPDATE: The line where it is falling over is;

services.AddMvcCore().AddJsonFormatters();

in ConfigureServices.

How do I debug this to find out what is causing this?

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

. . .

like image 343
arame3333 Avatar asked May 30 '18 10:05

arame3333


People also ask

How do I debug .NET core Web API in IIS?

Select the ASP.NET Core project in Visual Studio Solution Explorer and click the Properties icon, or press Alt+Enter, or right-click and choose Properties. Select the Debug tab. In the Properties pane, next to Profile, For IIS Express, select IIS Express from the dropdown.

How do I debug Web API hosted in IIS?

We can easily debug any web application that is hosted on IIS by attaching worker process of that application to Visual Studio as shown below. From Visual Studio IDE > Tools > Attach to Process Select the particular Process, then start debugging. Now, enjoy debugging with Visual Studio.


1 Answers

You didn't say which version of ASP.NET Core you are using. If it's 2.0+, you can configure NLog in Program.cs to load before Startup:

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();
    }
}
like image 195
Saša Ćetković Avatar answered Sep 22 '22 10:09

Saša Ćetković