简体   繁体   English

在运行时未找到方法HttpContentExtensions.ReadAsAsync

[英]Method HttpContentExtensions.ReadAsAsync not found runtime

I recently added some code to my application to consume an webAPI. 我最近在我的应用程序中添加了一些代码以使用webAPI。 This is done with an HttpClient. 这是通过HttpClient完成的。 On my development machine everything is working fine. 在我的开发机器上,一切正常。 But as soon as I publish to an Azure WebApp (directly or via a buildprocess) I'm getting a runtime error once the application has been deployed. 但是,一旦我(直接或通过构建过程)发布到Azure WebApp,一旦部署了应用程序,我就会遇到运行时错误。

This is the exception I'm getting; 这是我得到的例外; 在此处输入图片说明

The exception points to line 246. Which is equivalent to this method; 异常指向第246行。

public IList<SubscriberDefinition> GetAllSubscribers()
    {
        IList<SubscriberDefinition> allSubscribers = Caching.Get<IList<SubscriberDefinition>>(CacheKeys.ListAllSubscribers);

        if (allSubscribers != null && allSubscribers.Count > 0)
            return allSubscribers;

        try
        {
            SetBearerToken(HttpClient);
            HttpResponseMessage response = HttpClient.GetAsync("api/getallsubscribers").Result;
            if (response.IsSuccessStatusCode)
            {
                allSubscribers = response.Content.ReadAsAsync<IList<SubscriberDefinition>>().Result;

                Caching.Add(CacheKeys.ListAllSubscribers, allSubscribers, ECacheDuration.TwentyFourHours);

                return allSubscribers;
            }
        }
        catch
        {
            return new List<SubscriberDefinition>();
        }
        return new List<SubscriberDefinition>();
    }

I've been pulling my hair out for two days now about this issue. 关于这个问题,我已经两天了。 But I can't figure out what it is.. The setup of this piece of code is similar to another API I'm consuming in the application. 但是我不知道它是什么。。这段代码的设置类似于我在应用程序中使用的另一个API。 And there it is working fine. 那里工作正常。 So it must be the references I'm using in this particular class. 因此它必须是我在此特定类中使用的引用。 Hopefully somebody can help me out here, or give me a push in the right direction. 希望有人可以在这里帮助我,或者向我推向正确的方向。

After a long search I found out that the problem was caused by the way I was using async. 经过长时间的搜索,我发现问题是由我使用异步方式引起的。 Many thanks to @JohnRasch for pushing me in the right direction! 非常感谢@JohnRasch将我推向正确的方向!

I suspect that in one of the many referenced NuGet and MyGet packages an update took place causing this error to occur. 我怀疑在许多引用的NuGet和MyGet软件包之一中,发生了更新,导致发生此错误。 I updated the way the response was handled and converted to the type I wanted. 我更新了响应的处理方式,并将其转换为所需的类型。 And that solved the issue. 这就解决了问题。 Though I still don't completely understand what the root cause for this problem was, I atleast know how to fix it properly. 尽管我仍然不完全了解导致此问题的根本原因,但我至少知道如何正确解决它。

For this specific service / API-call I was able to get it working with the following code; 对于此特定的服务/ API调用,我可以使用以下代码进行操作;

public IList<SubscriberDefinition> GetAllSubscribers()
    {
        IList<SubscriberDefinition> allSubscribers = Caching.Get<IList<SubscriberDefinition>>(CacheKeys.ListAllSubscribers);

        if (allSubscribers != null && allSubscribers.Count > 0)
            return allSubscribers;

        try
        {
            SetBearerToken(HttpClient);
            HttpResponseMessage response = Task.Run(async () => await HttpClient.GetAsync("api/getallsubscribers")).Result;
            if (response.IsSuccessStatusCode)
            {
                var jsonstring = response.Content.ReadAsStringAsync();
                jsonstring.Wait();

                allSubscribers = JsonConvert.DeserializeObject<IList<SubscriberDefinition>>(jsonstring.Result);

                Caching.Add(CacheKeys.ListAllSubscribers, allSubscribers, ECacheDuration.TwentyFourHours);

                return allSubscribers;
            }
        }
        catch
        {
            return new List<SubscriberDefinition>();
        }
        return new List<SubscriberDefinition>();
    }

In another service we also had this error and there we could fix it by going async all-the-way. 在另一项服务中,我们也遇到了此错误,我们可以通过全程异步来解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM