【Azure 微服务】Service Fabric中微服务在升级时,遇见Warning - System.Collections.Generic.KeyNotFoundException 服务无法正常运行

简介: 【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

而在升级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

相关文章
|
20天前
|
数据安全/隐私保护
【Azure Service Fabric】关于Service Fabric的相关问题
【Azure Service Fabric】关于Service Fabric的相关问题
|
1天前
|
存储 搜索推荐 数据库
MarkLogic在微服务架构中的应用:提供服务间通信和数据共享的机制
随着微服务架构的发展,服务间通信和数据共享成为关键挑战。本文介绍MarkLogic数据库在微服务架构中的应用,阐述其多模型支持、索引搜索、事务处理及高可用性等优势,以及如何利用MarkLogic实现数据共享、服务间通信、事件驱动架构和数据分析,提升系统的可伸缩性和可靠性。
10 5
|
20天前
|
安全 数据可视化 数据安全/隐私保护
【Azure 微服务】新创建的Service Fabric集群,如何从本地机器上连接到Service Fabric Explorer(Service Fabric状态/错误查看工具)呢?
【Azure 微服务】新创建的Service Fabric集群,如何从本地机器上连接到Service Fabric Explorer(Service Fabric状态/错误查看工具)呢?
【Azure 微服务】新创建的Service Fabric集群,如何从本地机器上连接到Service Fabric Explorer(Service Fabric状态/错误查看工具)呢?
|
6天前
|
XML Java 数据库
在微服务架构中,请求常跨越多个服务,涉及多组件交互,问题定位因此变得复杂
【9月更文挑战第8天】在微服务架构中,请求常跨越多个服务,涉及多组件交互,问题定位因此变得复杂。日志作为系统行为的第一手资料,传统记录方式因缺乏全局视角而难以满足跨服务追踪需求。本文通过一个电商系统的案例,介绍如何在Spring Boot应用中手动实现日志链路追踪,提升调试效率。我们生成并传递唯一追踪ID,确保日志记录包含该ID,即使日志分散也能串联。示例代码展示了使用过滤器设置追踪ID,并在日志记录及配置中自动包含该ID。这种方法不仅简化了问题定位,还具有良好的扩展性,适用于各种基于Spring Boot的微服务架构。
18 3
|
8天前
|
自然语言处理 Java 网络架构
解锁跨平台微服务新纪元:Micronaut与Kotlin联袂打造的多语言兼容服务——代码、教程、实战一次打包奉送!
【9月更文挑战第6天】Micronaut是一款轻量级、高性能的Java框架,适用于微服务开发。它支持Java、Groovy和Kotlin等多种语言,提供灵活的多语言开发环境。本文通过创建一个简单的多语言兼容服务,展示如何使用Micronaut及其注解驱动特性实现REST接口,并引入国际化支持。无论是个人项目还是企业应用,Micronaut都能提供高效、一致的开发体验,成为跨平台开发的利器。通过简单的配置和代码编写,即可实现多语言支持,展现其强大的跨平台优势。
21 2
|
13天前
|
Java 数据库连接 Spring
当在线购物遇上数据危机:Hibernate 事务管理如何力挽狂澜,确保每一次交易都万无一失?
【8月更文挑战第31天】数据一致性和事务管理对任何企业级应用至关重要,尤其是在使用 Hibernate 时。本文通过在线购物系统的具体案例,介绍了正确管理事务的重要性。以 `Product` 和 `Order` 实体为例,阐述了如何通过编程式或声明式事务管理(如 Java 代码示例中的 `@Transactional` 注解)来确保数据一致性。正确配置事务能显著提升应用质量和系统稳定性。
24 0
|
14天前
|
JSON API 数据格式
揭秘微服务间沟通的神秘语言!Ruby如何化身“信使”,让服务间通信畅通无阻?
【8月更文挑战第31天】随着应用程序规模的不断扩大,微服务架构因高度模块化、可扩展性和可维护性成为现代软件开发的首选。本文通过示例代码展示如何使用 Ruby 实现微服务间的通信,重点介绍 RESTful API 的应用。首先,通过安装 HTTParty 库简化 HTTP 请求处理;然后创建微服务客户端,演示如何调用用户服务的 API 并获取用户信息;最后,介绍如何解析 JSON 响应,使数据处理更加便捷。这种方式不仅简单高效,还能满足大多数微服务架构下的通信需求。
23 0
|
19天前
【Azure Fabric Service】Service Fabric部署失败问题 Provisioning of VM extension ConfigureVM has timed out.
【Azure Fabric Service】Service Fabric部署失败问题 Provisioning of VM extension ConfigureVM has timed out.
|
11月前
|
开发框架 .NET 区块链
Hyperledger fabric部署链码(五)初始化与链码升级
fabric部署chaincode-go(智能合约)系列之五
157 0
|
11月前
|
测试技术 Go 区块链
Hyperledger fabric 测试环境部署
Hyperledger fabric 测试环境部署及相关问题解答
202 3
Hyperledger fabric 测试环境部署