【Azure 微服務(wù)】Service Fabric中微服務(wù)在升級(jí)時(shí)摊沉,遇見Warning - System.Collections.Generic.KeyNotFoundException 服務(wù)無(wú)...

問(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

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)

而在升級(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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市鲤拿,隨后出現(xiàn)的幾起案子假褪,更是在濱河造成了極大的恐慌,老刑警劉巖近顷,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件生音,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡窒升,警方通過(guò)查閱死者的電腦和手機(jī)缀遍,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)饱须,“玉大人瑟由,你說(shuō)我怎么就攤上這事。” “怎么了歹苦?”我有些...
    開封第一講書人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵青伤,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我殴瘦,道長(zhǎng)狠角,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任蚪腋,我火速辦了婚禮丰歌,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘屉凯。我一直安慰自己立帖,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開白布悠砚。 她就那樣靜靜地躺著晓勇,像睡著了一般。 火紅的嫁衣襯著肌膚如雪灌旧。 梳的紋絲不亂的頭發(fā)上绑咱,一...
    開封第一講書人閱讀 51,370評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音枢泰,去河邊找鬼描融。 笑死,一個(gè)胖子當(dāng)著我的面吹牛衡蚂,可吹牛的內(nèi)容都是我干的窿克。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼毛甲,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼年叮!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起丽啡,我...
    開封第一講書人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤谋右,失蹤者是張志新(化名)和其女友劉穎硬猫,沒想到半個(gè)月后补箍,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡啸蜜,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年坑雅,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了朽褪。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片宜咒。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖恬吕,靈堂內(nèi)的尸體忽然破棺而出蜂林,到底是詐尸還是另有隱情遥诉,我是刑警寧澤拇泣,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站矮锈,受9級(jí)特大地震影響霉翔,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜苞笨,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一债朵、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧瀑凝,春花似錦序芦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至射窒,卻和暖如春藏杖,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背脉顿。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工蝌麸, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人艾疟。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓来吩,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親蔽莱。 傳聞我的和親對(duì)象是個(gè)殘疾皇子弟疆,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容