【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 ...
随机推荐
- ws & websocket & 掉线重连
ws & websocket & 掉线重连 reconnecting websocket https://github.com/joewalnes/reconnecting-webso ...
- js 创建XML
// 创建xml数据 let doc = document.implementation.createDocument('', 'root', null); // 创建一个文档 let root =d ...
- BGV再度爆发,流通市值破500万美金!
BGV似乎以超乎寻常的姿态,开启了爆发的模式.这两天,BGV一路上涨,日内最高涨至548.78美金,24小时成交额达到了98.07万美金,24小时成交量达到1844.93枚BGV,流通市值更是突破了5 ...
- 深入解析 HTTP 缓存控制
缓存(Cache)是计算机领域里的一个重要概念,是优化系统性能的利器. 由于链路漫长,网络时延不可控,浏览器使用 HTTP 获取资源的成本较高.所以,非常有必要把"来之不易"的数据 ...
- yum install valgrind.x86_64
Reference: https://cloudlinux.zendesk.com/hc/en-us/articles/115004075294-Fix-rpmdb-Thread-died-in-Be ...
- 使用 Tye 辅助开发 dotnet 应用程序
newbe.pro 已经给我们写了系列文章介绍Tye 辅助开发k8s 应用: 使用 Tye 辅助开发 k8s 应用竟如此简单(一) 使用 Tye 辅助开发 k8s 应用竟如此简单(二) 使用 Tye ...
- 企业安全_检测SQL注入的一些方式探讨
目录 寻找SQL注入点的 way MySQL Inject 入门案例 自动化审计的尝试之旅 人工审计才能保证精度 寻找SQL注入点的 way 在企业中有如下几种方式可以选择: 自动化 - 白盒基于源码 ...
- [MIT 18.06 线性代数]Intordution to Vectors向量初体验
目录 1.1. Vectors and Linear Combinations向量和线性组合 REVIEW OF THE KEY IDEAS 1.2 Lengths and Dot Products向 ...
- CCF(地铁修建):向前星+dijikstra+求a到b所有路径中最长边中的最小值
地铁修建 201703-4 这题就是最短路的一种变形,不是求两点之间的最短路,而是求所有路径中的最长边的最小值. 这里还是使用d数组,但是定义不同了,这里的d[i]就是表示从起点到i的路径中最长边中的 ...
- 大牛带你学会java类加载机制,不要错过,值得收藏!
很多人对java类加载机制都是非常抗拒的,因为这个太难理解了,但是我们作为一名优秀的java工程师,还是要把java类加载机制研究和学习明白的,因为这对于我们在以后的工作中有很大的帮助,因为它在jav ...
