作者:justmoon999 | 来源:互联网 | 2023-09-05 15:19
Ihaveanasp.netMVCwebsitewhichisconsumingarestapitoreceiveitsdata.Imusingasynchro
I have an asp.net MVC website which is consuming a rest api to receive it's data. I'm using asynchronous tasks to perform the requests as there can be many on each page. After a while of uptime the website has been throwing the following error when trying to receive data.
我有一个asp.net MVC网站,它使用一个rest api来接收它的数据。我使用异步任务来执行请求,因为每个页面上可能有很多请求。一段时间后,网站在尝试接收数据时抛出了以下错误。
The underlying connection was closed: An unexpected error occurred on a send.
底层连接被关闭:发送时发生意外错误。
I read that this could be due to the maxconnection settings on the web.config but increasing this doesn't seem to make much difference.
我读到这可能是因为web上的maxconnection设置。配置,但是增加这个似乎没有多大区别。
I'm also using caching to reduce the load on the api. The task is cached so the result can be used later.
我还使用缓存来减少api的负载。缓存任务,以便稍后使用结果。
The only way I've found to fix this is by recycling the application pool. Any help would be appreciated.
我找到的唯一修复方法是回收应用程序池。如有任何帮助,我们将不胜感激。
/* Code from page_load */
var currenciesTask = ApiClient.GetAsync("currencies");
var blogArticleTask = ApiClient.GetAsync("blog/articles", "limit=10");
var seoPageTask = ApiClient.GetAsync("seopages");
await Task.WhenAll(currenciesTask, blogArticleTask, seoPageTask);
/* Code from data access later */
public class ApiClient : HttpClient
{
public static Task GetAsync(string operation, string query = null, bool cache = true)
{
// Check if task is in cache
string cacheName = null;
if (cache)
{
cacheName = String.Format("{0}_{1}_{2}", operation, query ?? String.Empty, App.GetLanguage());
var cachedTask = HttpRuntime.Cache[cacheName];
if (cachedTask != null)
{
return (Task)cachedTask;
}
}
// Get data task
var task = GetAsyncData(operation, query);
// Add to cache if required
if (task != null && cache)
{
App.AddToCache(cacheName, task);
}
return task;
}
public static async Task GetAsyncData(string operation, string query = null)
{
using (ApiClient client = new ApiClient())
{
string url;
if (query != null)
{
url = String.Format("{0}?{1}", operation, query);
}
else
{
url = String.Format("{0}", operation);
}
var respOnse= await client.GetAsync(url);
return (await response.Content.ReadAsAsync());
}
}
}
2 个解决方案