gpt4 book ai didi

c# - Async GetAwaiter() 抛出奇怪的异常

转载 作者:行者123 更新时间:2023-12-03 21:14:51 26 4
gpt4 key购买 nike

下面我粘贴了一些代码。在正文的第一行,我调用了一个等待调用 Task<IEnumerable<SkuGcn>> GetSkuGcnList();最后一行,在 mapPromise.GetAwaiter() 中获取结果时抛出此异常:

An exception of type 'System.IO.FileNotFoundException' occurred in System.Private.CoreLib.ni.dll but was not handled in user code

Additional information: Could not load file or assembly 'System.Runtime.Serialization.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

我知道我的服务 GetSkuGcnList() call 正在调用,正在被调用,因为我可以设置一个断点,并在调用时在它处中断。然后服务似乎恢复了。

此代码在 ASP.NET 5 RC1 中运行得很好,但现在在 ASP.Net Core 1.0 版本中运行得不太好。

任何帮助将不胜感激!

        public ProductItemsCacheStore(IItemsProductCacheRepository itemsRepo, IProductAvailabilityPricing proxyGoliathApi,
IProductItemListRepo itemsListsRepo, IItemPricesCacheRepo itemPricesRepo)
{
var mapPromise = proxyGoliathApi.GetSkuGcnList();
_items = itemsRepo.GetAllProductOrderListForCache();
_itemsLists = itemsListsRepo.GetItemListsForCache();
_priceZones = itemPricesRepo.GetAllPriceZonesAndItemPrices();
MergeProductsWithGcn(_items, mapPromise.GetAwaiter().GetResult());
}

我的project.json看起来像这样:

{
"version": "1.0.0-alpha.1",
"description": "AvailabilityPricingClient Class Library",
"authors": [ "irving.lennert" ],
"packOptions": {
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": ""
},

"tooling": {
"defaultNamespace": "AvailabilityPricingClient"
},

"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
},
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.AspNet.WebApi.Client": "5.2.3"
},

"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
}
}

执行GetSkuGcnList()是这样的:

    public async Task<IEnumerable<SkuGcn>> GetSkuGcnList()
{
HttpResponseMessage response = _client.GetAsync("/api/skuGcnList").Result;

if (response.IsSuccessStatusCode)
{
var skuGcns = await response.Content.ReadAsAsync<IEnumerable<SkuGcn>>();
return skuGcns;
}

return null;
}

最佳答案

我已确定我调用的程序集对于 ASP.Net Core 版本无法正常工作。如果你查看上面的project.json,就会发现这一行:

"Microsoft.AspNet.WebApi.Client": "5.2.3"

我已将该行更改为这一行:

"System.Net.Http": "4.1.0" 

此程序集不公开相同的 API,因此我必须将实现代码更改为:

    public async Task<IEnumerable<SkuGcn>> GetSkuGcnList()
{
HttpResponseMessage response = _client.GetAsync("/api/skuGcnList").Result;

if (response.IsSuccessStatusCode)
{
var skuGcns = await response.Content.ReadAsStringAsync()
.ContinueWith<IEnumerable<SkuGcn>>(getTask =>
{
return JsonConvert.DeserializeObject<IEnumerable<SkuGcn>>(getTask.Result);
});
return skuGcns;
}

return null;
}

这个解决方案运行良好。重要的是要注意,我在帖子方面也遇到了同样的问题,而且它们有点复杂,所以我将提供另一个我打的电话,这是给任何感兴趣的人的帖子。

    public async Task<IEnumerable<Availablity>> GetAvailabilityBySkuList(IEnumerable<string> skuList)
{
var output = JsonConvert.SerializeObject(skuList);
HttpContent contentPost = new StringContent(output, System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response = _client.PostAsync("/api/availabilityBySkuList", contentPost).Result;

if (response.IsSuccessStatusCode)
{
var avail = await response.Content.ReadAsStringAsync()
.ContinueWith<IEnumerable<Availablity>>(postTask =>
{
return JsonConvert.DeserializeObject<IEnumerable<Availablity>>(postTask.Result);
});
return avail;
}

return null;
}

感谢所有贡献者!

关于c# - Async GetAwaiter() 抛出奇怪的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38314370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com