![](/img/trans.png)
[英]ASP.NET Core Web API, How can I access HttpContext in startup class
[英]How to debug startup in 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 )我收到此錯誤消息;
應用程序啟動異常: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鍵)在Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(字符串依賴)在Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.d__4.MoveNext()在System.Linq.Enumerable.d__172.MoveNext()在系統.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()at Microsoft.Extensions.DependencyInject.MvcCoreServiceCollectionExtensions.GetApplicationPartManager(IServiceCollection services)at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection services)at Properties.API.Startup.ConfigureServices(IServiceCollection) SER 惡意)---拋出異常的前一個位置的堆棧跟蹤結束---在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<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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.