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

标签 c# asp.net asp.net-core .net-core asp.net-core-1.0

下面我粘贴了一些代码。在正文的第一行,我调用了一个等待调用 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/

相关文章:

c# - 来自 API 的 ResponseStream 导致反序列化时出现空值

c# - 多边形差异 - 来自 Clipperlib 的奇怪结果

asp.net - 如何在不使用 <%@ register %> 或 <%@ Reference %> 的情况下动态添加用户控件?

asp.net - 在 EF 的自联接表中选择最后一个 child

c# - 在哪里定义 aspnetcore 授权失败的 url?

c# - Azure Service Fabric 无法使用 ASP.NET Core 在 VSTS 上执行 CI

c# - 将 POCO 模型转换为 MongoDB 的 Bson 格式

c# - .Net 中的 LDAP 目录条目 - 不适用于 OU=Users

c# - 通过函数指针在 C# 中调用 C 函数

asp.net - 添加属性路由会破坏基于配置的路由 (.NET MVC5)