【发布时间】:2018-11-09 04:19:33
【问题描述】:
我有一个使用 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:字典中不存在给定的键。在 System.Collections.Generic.Dictionary2.get_Item(TKey 键) 在 Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(字符串依赖) 在 Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(字符串依赖) 在 Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.ComputeClassification(字符串依赖) 在 Microsoft.AspNetCore.Mvc.Internal.DefaultAssemblyPartDiscoveryProvider.CandidateResolver.d__4.MoveNext() 在 System.Linq.Enumerable.d__172.MoveNext() 在 System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() 在 Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.GetApplicationPartManager(IServiceCollection services) 在 Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore (IServiceCollection 服务)在 Properties.API.Startup.ConfigureServices(IServiceCollection 服务)---从以前抛出异常的位置结束堆栈跟踪---在 Microsoft.AspNetCore 的 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()。 Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) 在 Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() 在 Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() 托管环境:暂存内容根路径:C:\inetpub\ wwwroot\SIR 现在正在监听:http://localhost:33007 应用程序已启动。按 Ctrl+C 关闭。
更新: 它跌倒的线是;
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>();
}
。 . .
【问题讨论】:
-
ConfigureServices内部发生异常,代码未显示。 -
我想知道你是怎么做到的?尽管如此,我还是发布了 ConfigureServices。
-
它在您发布的堆栈跟踪中这么说:“在 Properties.API.Startup.ConfigureServices(...)”
-
AddMvcCore抛出异常。你知道AddMvcCore和AddMvc之间的区别吗?为什么要专门使用AddMvcCore? -
所以你真的不知道区别。因为,
AddMvcCore中的Core基本上意味着“只添加核心组件”,而不是“添加asp.net Core”。虽然AddMvc在内部也调用了AddMvcCore,但在此之上还添加了更多必要的组件。
标签: c# iis asp.net-core-webapi