![](/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.