Reputation: 87
I'm having a problem with SNAT exhaustion in one of our Azure App Service based APIs:
Our HTTPClient is written into a singleton that should instance only once (C#/.net 4.72)...
public class CSClient : HttpClient
{
private static readonly CSClient Inst = new CSClient();
static CSClient()
{
}
private CSClient() : base()
{
Timeout = TimeSpan.FromSeconds(60);
BaseAddress = new Uri(ConfigurationManager.AppSettings["***.BaseURL"]);
}
public static HttpClient Instance
{
get
{
return Inst;
}
}
}
Then called
public class ContentRepository : IContentRepository
{
protected HttpClient htc = CSClient.Instance;
public async Task<Content> GetContentAsync(Content ct)
{
using (var req = new HttpRequestMessage(HttpMethod.Get, ConfigurationManager.AppSettings["***.BaseUrl"] + "/api/v2/nodes/" + ct.Id))
{
var response = await htc.SendAsync(req);
if (response.IsSuccessStatusCode)
{
var job = JObject.Parse(await response.Content.ReadAsStringAsync());
var respct = OTtoContent(job["results"]);
return respct;
}
else
{
return null;
}
}
}
}
I'm not sure why the extra connections are being made. Is my singleton correct? Anything else I can do with the httpclient? Anything to do with the app service, short of adding resources? Thanks for any help in advance.
Upvotes: 2
Views: 1569
Reputation: 142008
HttpClient allow multiple requests in parallel to the same endpoint(by default).
The following methods are thread safe:
So one instance of HttpClient
can make several connections at a time via SendAsync
. You can try to control number of connections via ServicePointManager or HttpClientHandler.MaxConnectionsPerServer(on Core) property.
Upvotes: 0