問(wèn)題描述
使用.Net Framework 4.5.2為架構(gòu)的Service Fabric微服務(wù)應(yīng)用,在升級(jí)后發(fā)布到Azure Fabric中哲嘲,服務(wù)無(wú)法運(yùn)行。通過(guò)Service Fabric Explorer查看到服務(wù)出現(xiàn)Warning媳禁。全部的錯(cuò)誤消息為:
SF Explorer中查看狀態(tài)
SF副本節(jié)點(diǎn)中的全部狀態(tài)錯(cuò)誤
'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.
問(wèn)題分析及解決
在錯(cuò)誤消息中眠副,Microsoft.AspNetCore.Mvc組件拋出了“System.Collections.Generic.KeyNotFoundException?(-2146232969)The given key was not present in the dictionary.” 異常。在Stack Overflow中竣稽,查到KeyNotFoundException是當(dāng)前ASP.NET Core 2.1的一個(gè)已知Issue囱怕。
是因?yàn)樵诎惭b .Net Core 2.1后,與舊版本之間存在環(huán)境變量的依賴沖突問(wèn)題丧枪」馔浚可以通過(guò)修改版本(如2.0)來(lái)避免這個(gè)問(wèn)題。
如在項(xiàng)目文件中修改Microsoft.AspNetCore.Mvc版本(PS: 最便捷的方式是在Visual Studio 2019 IDE中通過(guò)NuGet修改版本拧烦,它會(huì)同步更新相關(guān)依賴),
Microsoft.AspNetCore2.0.4
.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)
而在升級(jí)AspNetCore的版本后忘闻,在項(xiàng)目中有些依賴也會(huì)同步升級(jí)(https://www.nuget.org/packages/Microsoft.AspNetCore/2.0.4),所以想要注意以下幾點(diǎn):
1)Microsoft.ServiceFabric.AspNetCore.WebListener升級(jí)后恋博,UseWebListener?已經(jīng)被替換成UseHttpSys齐佳。(Link:?https://github.com/aspnet/Hosting/issues/1128)
2)實(shí)際使用中,發(fā)現(xiàn)UseHttpSys后需要的依賴包债沮,與Microsoft.AspNetCore包含相同的依賴炼吴,而引起包沖突。應(yīng)使用UseKestrel方法
///<summary>
/// Optional override to create listeners (like tcp, http) for this service instance.
///</summary>///<returns>The collection of listeners.</returns>
protected override IEnumerable CreateServiceInstanceListeners()
? ? ? ? {
? ? ? ? ? ? returnnew ServiceInstanceListener[]
? ? ? ? ? ? {
? ? ? ? ? ? ? ? new ServiceInstanceListener(serviceContext =>new KestrelCommunicationListener(serviceContext,"ServiceEndpoint", (url, listener) =>? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}");
? ? ? ? ? ? ? ? ? ? ? ? returnnewWebHostBuilder().UseKestrel().ConfigureServices(? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? services=> services
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .AddSingleton(serviceContext))
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .UseContentRoot(Directory.GetCurrentDirectory())
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .UseStartup()
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .UseUrls(url)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .Build();
? ? ? ? ? ? ? ? ? ? }))
? ? ? ? ? ? };
? ? ? ? }
3) 部署包中依賴混亂疫衩,拋出加載Abstractions 1.1.1.0舊版本的Dll硅蹦。正確的版本應(yīng)該為2.0及以上
'System.RA'reported Warningforproperty'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 tracefromprevious locationwhereexception 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
在修改版本問(wèn)題方面,除了在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