【Azure 微服务】Service Fabric中微服务在升级时,遇见Warning - System.Collections.Generic.KeyNotFoundException 服务无法正常运行
问题描述
使用.Net Framework 4.5.2为架构的Service Fabric微服务应用,在升级后发布到Azure Fabric中,服务无法运行。通过Service Fabric Explorer查看到服务出现Warning。全部的错误消息为:
| SF Explorer中查看状态 | ![]() |
| SF副本节点中的全部状态错误 |
'System.RA' reported Warning for property 'ReplicaOpenStatus'. Replica had multiple failures during open on _ggamenode_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) --- 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 Microsoft.ServiceFabric.Services.Communication.AspNetCore.AspNetCoreCommunicationListener.OpenAsync(CancellationToken cancellationToken) at Microsoft.ServiceFabric.Services.Runtime.StatelessServiceInstanceAdapter.d__13. |
问题分析及解决
在错误消息中,Microsoft.AspNetCore.Mvc组件抛出了“System.Collections.Generic.KeyNotFoundException (-2146232969)The given key was not present in the dictionary.” 异常。在Stack Overflow中,查到KeyNotFoundException是当前ASP.NET Core 2.1的一个已知Issue。
是因为在安装 .Net Core 2.1后,与旧版本之间存在环境变量的依赖冲突问题。可以通过修改版本(如2.0)来避免这个问题。
如在项目文件中修改Microsoft.AspNetCore.Mvc版本(PS: 最便捷的方式是在Visual Studio 2019 IDE中通过NuGet修改版本,它会同步更新相关依赖),
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.0.3" />
Microsoft.AspNetCore 2.0.4
Dependencies
.NETStandard 2.0
- Microsoft.AspNetCore.Diagnostics (>= 2.0.3)
- Microsoft.AspNetCore.Hosting (>= 2.0.3)
- Microsoft.AspNetCore.Routing (>= 2.0.3)
- Microsoft.AspNetCore.Server.IISIntegration (>= 2.0.3)
- Microsoft.AspNetCore.Server.Kestrel (>= 2.0.4)
- Microsoft.AspNetCore.Server.Kestrel.Https (>= 2.0.4)
- Microsoft.Extensions.Configuration.CommandLine (>= 2.0.2)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 2.0.2)
- Microsoft.Extensions.Configuration.FileExtensions (>= 2.0.2)
- Microsoft.Extensions.Configuration.Json (>= 2.0.2)
- Microsoft.Extensions.Configuration.UserSecrets (>= 2.0.2)
- Microsoft.Extensions.Logging (>= 2.0.2)
- Microsoft.Extensions.Logging.Configuration (>= 2.0.2)
- Microsoft.Extensions.Logging.Console (>= 2.0.2)
- Microsoft.Extensions.Logging.Debug (>= 2.0.2)
而在升级AspNetCore的版本后,在项目中有些依赖也会同步升级(https://www.nuget.org/packages/Microsoft.AspNetCore/2.0.4),所以想要注意以下几点:
1) Microsoft.ServiceFabric.AspNetCore.WebListener升级后,UseWebListener 已经被替换成UseHttpSys。(Link: https://github.com/aspnet/Hosting/issues/1128)

2)实际使用中,发现UseHttpSys后需要的依赖包,与Microsoft.AspNetCore包含相同的依赖,而引起包冲突。应使用UseKestrel方法
/// <summary>
/// Optional override to create listeners (like tcp, http) for this service instance.
/// </summary>
/// <returns>The collection of listeners.</returns>
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new ServiceInstanceListener[]
{
new ServiceInstanceListener(serviceContext =>
new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
{
ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}"); return new WebHostBuilder()
.UseKestrel()
.ConfigureServices(
services => services
.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
.UseUrls(url)
.Build();
}))
};
}
3) 部署包中依赖混乱,抛出加载Abstractions 1.1.1.0旧版本的Dll。正确的版本应该为2.0及以上
'System.RA' reported Warning for property 'ReplicaOpenStatus'. Replica had multiple failures during open on _ggamenode_0.
API call: IStatelessServiceInstance.Open(); Error = System.IO.FileLoadException (-2146234304)
Could not load file or assembly
'Microsoft.AspNetCore.Hosting.Abstractions, Version=1.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
System.IO.FileLoadException (-2146234304) Could not load file or assembly '
Microsoft.AspNetCore.Hosting.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829
at SmsServiceApi.SmsServiceApi.<>c.b__1_0(StatelessServiceContext serviceContext)
at Microsoft.ServiceFabric.Services.Runtime.StatelessServiceInstanceAdapter.d__13.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__0.MoveNext()
For more information see: https://aka.ms/sfhealth
在修改版本问题方面,除了在project的config文件中修改为正确的版本,还需要检查打包后的部署包中Dll的版本。

参考资料
AspNet Core WebApi fails at startup with error System.Collections.Generic.KeyNotFoundException :https://stackoverflow.com/questions/51446570/aspnet-core-webapi-fails-at-startup-with-error-system-collections-generic-keynot
Service fabric API cannot run in azure SF cluster : https://github.com/microsoft/service-fabric-issues/issues/1190
Service Fabric & ASP.NET Core 2.0 fails to run/start site: https://github.com/aspnet/Hosting/issues/1128
Application services not starting - KeyNotFoundException (AspNetCore 1.1): https://github.com/microsoft/service-fabric-issues/issues/1086
Microsoft.AspNetCore: https://www.nuget.org/packages/Microsoft.AspNetCore/2.0.4
【Azure 微服务】Service Fabric中微服务在升级时,遇见Warning - System.Collections.Generic.KeyNotFoundException 服务无法正常运行的更多相关文章
- 【Azure微服务 Service Fabric 】Service Fabric中应用开启外部访问端口及微服务之间通过反向代理端口访问问题
问题描述 1) 当成功的在Service Fabric集群中部署了应用后,如何来访问呢?如果是一个Web服务,它的URL又是什么呢? 2) 当Service Fabric集群中,服务之间如需要相互访问 ...
- 【Azure微服务 Service Fabric 】因证书过期导致Service Fabric集群挂掉(升级无法完成,节点不可用)
问题描述 创建Service Fabric时,证书在整个集群中是非常重要的部分,有着用户身份验证,节点之间通信,SF升级时的身份及授权认证等功能.如果证书过期则会导致节点受到影响集群无法正常工作. 当 ...
- 如何把遗留的Java应用托管在Service Fabric中
一.概述 众所周知,微服务化尤其对遗留系统进行微服务化一般采用"Lift and Shift"的模式进行. Service Fabric作为一个微服务托管平台,不仅仅可以在上面跑. ...
- Web Service接口返回泛型的问题(System.InvalidCastException: 无法将类型为“System.Collections.Generic.List`1[System.String]”的对象强制转换为类型“System.String[]”)
在使用C#写Web Service时遇到了个很奇怪的问题.返回值的类型是泛型(我用的是类似List<string>)的接口,测试时发现总是报什么无法转换为对象的错误,百思不得其解. 后来在 ...
- 【Azure微服务 Service Fabric 】如何转移Service Fabric集群中的种子节点(Seed Node)
注意:在对Service Fabric的节点做操作之前,请务必确认是否是种子节点(Seed Node)且当前节点的数量是否与SF的持久层要求的数量一致. 可靠性级别是 Service Fabric 群 ...
- 【Azure微服务 Service Fabric 】在SF节点中开启Performance Monitor及设置抓取进程的方式
前提条件 当我们观察到SF中某一个节点出现CPU不正常的情况,但是由于不能肉眼长期观察,所以可以通过开启Performance Monitor的方式来获取每一个进程的%Processer Time的方 ...
- 【Azure微服务 Service Fabric 】使用az命令创建Service Fabric集群
问题描述 在使用Service Fabric的快速入门文档: 将 Windows 容器部署到 Service Fabric. 其中在创建Service Fabric时候,示例代码中使用的是PowerS ...
- Jenkins中使用Azure Powershell连接Service Fabric报错not recognized的原因与解决办法
一.使用背景 在涉及Azure service Fabric的自动化应用场景中,依赖于Service Fabric的Azure Powershell cmdlets,我们可以使用Jenkins能实现c ...
- MVC开发中的常见错误-05-无法将类型“System.Data.Entity.Infrastructure.DbQuery<BBFJ.OA.Model.RoleInfo>”转换为“System.Collections.Generic.List<BBFJ.OA.Model.RoleInfo>”
List<RoleInfo> roleInfoList = (List<RoleInfo>)ViewBag.AllRoles; 错误原因很明确了 ViewBag.AllRole ...
随机推荐
- Taro UI
Taro UI 一套基于 Taro 框架开发的多端 UI 组件库 https://github.com/NervJS/taro-ui-demo https://taro-ui.aotu.io/#/do ...
- 1月22日第二轮空投来袭,SPC算力福利币究竟能带来什么?
行情数据显示,比特币于14日23时30分再次突破40000美元,市值回升至7400亿美元.根据行情频道数据,比特币于14日2时展露上行态势,价格于34000美元附近起跳,至12时站上37000美元.此 ...
- 「NGK每日快讯」11.18日NGK公链第15期官方快讯
- Fast R-CNN训练自己的数据集时遇到的报错及解决方案
最近使用Fast R-CNN训练了实验室的数据集,期间遇到一些报错,主要还是在配置环境上比较麻烦,但可以根据提示在网上找到解决这些错误的办法.这里我只记录一些难改的报错,以后再遇见这些时希望能尽快解决 ...
- Java并发包源码学习系列:线程池ScheduledThreadPoolExecutor源码解析
目录 ScheduledThreadPoolExecutor概述 类图结构 ScheduledExecutorService ScheduledFutureTask FutureTask schedu ...
- 使用stress进行压力测试
本文转载自使用stress进行压力测试 导语 stress,顾名思义是一款压力测试工具.你可以用它来对系统CPU,内存,以及磁盘IO生成负载. 安装stress 几乎所有主流的linux发行版的软件仓 ...
- 上天的源码要不要——GitHub 热点速览 v.21.08
作者:HelloGitHub-小鱼干 前几天,"机智号" 所用的飞行软件框架 F´ 被 NASA 开源了,想看 F´ 这个嵌入式的代码不妨考虑下 Sourcetrail 这个神器, ...
- VMware vSphere 虚拟化平台的安装及使用
首先解释一下这些名词, vSphere是什么? vSphere 是VMware公司发布的一整套产品包,是VMware公司推出的一套服务器虚拟化解决方案,包含VMware ESXi hypervisor ...
- 【重磅】iNeuOS工业互联平台,系统集成业务模型和WEB组态视图建模集成3D模型
目 录 1. 概述... 1 2. 平台演示... 2 3. 系统集成业务模型... 2 4. WEB组态视图建模集成3D模型... 3 5. ...
- 树莓派4B安装官方Ubuntu20 Server版(64位)
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
