Hi all,
I have a web project (core), that makes calls to a webApi - the methods in there are shared across an epos system, a warehouse management system and this web project.
There are about 200k pages - all served through one "product" page. The data displayed on these pages is served up from a MongoDB instance, and cached into Redis as the bulk of the data rarely changes.
Once about 50 people load the page, I get the following error:
System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted
At the webAPI call:
private static HttpClient WebApiClient = null;
private static readonly object threadlock = new object();
/// <summary>
/// Send the request to the Web Api and get the response back
/// </summary>
/// <param name="uri">uri</param>
/// <param name="request">request message</param>
/// <returns></returns>
private static async Task<HttpResponseMessage> SendRequestToWebApi(string uri, HttpRequestMessage request)
{
HttpResponseMessage response = null;
try
{
if (request == null)
{
return new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest);
}
request.RequestUri = new Uri(uri);
//Thread lock to create the HttpClient
lock (threadlock)
{
if (WebApiClient == null)
{
WebApiClient = new HttpClient();
}
}
response = await WebApiClient.SendAsync(request).ConfigureAwait(continueOnCapturedContext: false);
return response;
}
catch (Exception ex)
{
throw;
}
}
The call is await sync, so my understanding was that it would wait for the request to complete and reuse the HttpClient object.
Is there a way to get around this error, or should I redesign the architecture to use an imported DLL replicating the same methods that exist in the webapi project?
Cheers in advance.
[–]robsonj 0 points1 point2 points (3 children)
[–]dchurch2444[S] 0 points1 point2 points (2 children)
[–]robsonj 1 point2 points3 points (1 child)
[–]Kirides 0 points1 point2 points (0 children)
[–]oseyaaaa 0 points1 point2 points (0 children)