AspNet Core WebApi 在启动时失败并出现错误 System.Collections.Generic.KeyNotFoundException

Posted

技术标签:

【中文标题】AspNet Core WebApi 在启动时失败并出现错误 System.Collections.Generic.KeyNotFoundException【英文标题】:AspNet Core WebApi fails at startup with error System.Collections.Generic.KeyNotFoundException 【发布时间】:2018-12-29 00:05:15 【问题描述】:

我的构建已成功构建 + 部署在 Azure 集群中。 VSTS Build Tasks

我没有代码更改。我比较了工作和不工作的构建工件。我的 MyCompany.MyService.Api.deps.json 文件中有新条目:

    ...
    "compilationOptions": 
        "defines": [
    ...
          "NETFRAMEWORK",
    ...
        ],
    ...
              "Microsoft.Bcl.Build": "1.0.21",
              "System.IdentityModel": "4.0.0.0",
              "System.ServiceModel.Web": "4.0.0.0",
              "System.Web.Services": "4.0.0.0",
              "System.Runtime.Caching": "4.0.0.0",
              "System.ServiceModel.Activation": "4.0.0.0"
            ,
    ...
              "System.Transactions": "4.0.0.0",
              "System.Web": "4.0.0.0",
              "System.Diagnostics.Tracing.Reference1": "4.0.20.0",
              "System.Runtime.Serialization.Primitives.Reference": "4.0.10.0",
              "System.Runtime.Serialization.Xml.Reference": "4.0.10.0"
            ,
    ...
          "System.Runtime.Reference1": "4.0.20.0",
          "System.Threading.Tasks.Reference1": "4.0.10.0",
          "System.Resources.ResourceManager.Reference1": "4.0.0.0",
          "System.Globalization.Reference1": "4.0.10.0",
          "System.Diagnostics.Tools.Reference1": "4.0.0.0",
          "System.Diagnostics.Debug.Reference1": "4.0.10.0",
          "System.Linq.Reference1": "4.0.0.0",
          "System.Collections.Reference1": "4.0.10.0",
          "System.Threading.Reference1": "4.0.10.0",
          "System.Runtime.Extensions.Reference1": "4.0.10.0",
          "System.IO.Reference1": "4.0.10.0",
          "System.Runtime.InteropServices.Reference1": "4.0.20.0",
          "System.Text.Encoding.Reference1": "4.0.10.0",
          "System.Reflection.Primitives.Reference1": "4.0.0.0",
          "System.Reflection.Reference1": "4.0.10.0",
          "System.Text.Encoding.Extensions.Reference1": "4.0.10.0",
          "System.ComponentModel.Reference": "4.0.0.0",
          "System.Reflection.Extensions.Reference1": "4.0.0.0"
...

在过去的几个月里,我成功地构建和部署了。然而在过去的 5 天里,从 VSTS 构建构建的代码无法部署到集群。我的 API 服务无法启动,并且出现以下错误。请建议我如何调试。代码在我的本地集群中成功部署和启动。

Unhealthy event: SourceId='System.RA', Property='ReplicaOpenStatus', HealthState='Warning', ConsiderWarningAsError=false.
Replica had multiple failures during open on _ntsfmain_0. API call: IStatelessServiceInstance.Open(); Error = System.Collections.Generic.KeyNotFoundException (-2146232969)
The given key was not present in the dictionary.
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary`2.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__17`2.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 Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddMvc(IServiceCollection services)
at MyCompany.MyService.Api.Startup.ConfigureDevelopmentServices(IServiceCollection services) in C:\agent_C\_work\19\s\Source\MyCompany.MyService.Api\Startup.cs:line 39
--- 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()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at MyCompany.MyService.Api.Api.<>c__DisplayClass4_0.b__1(String url, AspNetCoreCommunicationListener listener) in C:\agent_C\_work\19\s\Source\MyCompany.MyService.Api\Api.cs:line 91
at Microsoft.ServiceFabric.Services.Communication.AspNetCore.AspNetCoreCommunicationListener.OpenAsync(CancellationToken cancellationToken)
at Microsoft.ServiceFabric.Services.Runtime.StatelessServiceInstanceAdapter.d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.ServiceFabric.Services.Runtime.StatelessServiceInstanceAdapter.d__13.MoveNext()
For more information see: http://aka.ms/sfhealth

【问题讨论】:

【参考方案1】:

这是一个与 asp.net core 2.1 相关的已知错误。几周前我们遇到了同样的问题。

Core 2.1 的安装存在一些相互冲突的依赖关系,会影响其他 dot net 版本。 VSTS 构建环境或 SF 可能已升级到核心 2.1 并影响了您的旧构建。

你可以:

降级到 2.0: 在我们的案例中,我们有一些针对核心 2.1 的服务以及一些 1.1 和 2.0,我们将所有服务降级到核心 2.0 并且问题得到了解决。

或者,

您可以在您的 api 的 AddMvc() 行之前添加以下代码来修复依赖关系问题。

看起来像这样:

var manager = new ApplicationPartManager();
manager.ApplicationParts.Add(new AssemblyPart(typeof(Startup).Assembly));
manager.ApplicationParts.Add(new AssemblyPart(typeof(Project1.Project1Type).Assembly));

services.AddSingleton(manager);
services.AddMvc();

或者,

global.json 文件添加到您的解决方案中。

像这样:


  "sdk": 
    "version": "2.1.200"
  

请关注此 GitHub 问题以获取更多信息:

https://github.com/aspnet/Home/issues/3132 https://github.com/Azure/service-fabric-issues/issues/1086

【讨论】:

关于 Git Hub 的建议,在这里解决了我的问题。可悲的是,我花了 5 天的 Sprint 来调试它。 我和你有同样的感觉,我们花了大约一周的时间才发现这是 .NET 问题。如果解决了您的问题,请标记为已完成。干杯!

以上是关于AspNet Core WebApi 在启动时失败并出现错误 System.Collections.Generic.KeyNotFoundException的主要内容,如果未能解决你的问题,请参考以下文章

在 webapi .net core 3.1 上添加 app.useauthentication 时 Cors 失败

用于 aspnet-core web api 的基于令牌的身份验证

由于请求正文过大,大文件上传到 ASP.NET Core 3.0 Web API 失败

在 ASPNET Core 的路由中使用 ApiExplorerSettings GroupName

Aspnet Core - 应用程序启动事件

如何在 ASPNET.Core Web 应用程序中发送带有 CORS 标头的 HTTP 4xx-5xx 响应?