[英]Method HttpContentExtensions.ReadAsAsync not found runtime
我最近在我的应用程序中添加了一些代码以使用webAPI。 这是通过HttpClient完成的。 在我的开发机器上,一切正常。 但是,一旦我(直接或通过构建过程)发布到Azure WebApp,一旦部署了应用程序,我就会遇到运行时错误。
异常指向第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>();
}
关于这个问题,我已经两天了。 但是我不知道它是什么。。这段代码的设置类似于我在应用程序中使用的另一个API。 那里工作正常。 因此它必须是我在此特定类中使用的引用。 希望有人可以在这里帮助我,或者向我推向正确的方向。
经过长时间的搜索,我发现问题是由我使用异步方式引起的。 非常感谢@JohnRasch将我推向正确的方向!
我怀疑在许多引用的NuGet和MyGet软件包之一中,发生了更新,导致发生此错误。 我更新了响应的处理方式,并将其转换为所需的类型。 这就解决了问题。 尽管我仍然不完全了解导致此问题的根本原因,但我至少知道如何正确解决它。
对于此特定的服务/ 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>();
}
在另一项服务中,我们也遇到了此错误,我们可以通过全程异步来解决它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.