-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Open
Labels
area-System.Net.HttpquestionAnswer questions and provide assistance, not an issue with source code or documentation.Answer questions and provide assistance, not an issue with source code or documentation.
Milestone
Description
We try to migrate our .NetFramework application to .NetCore 8.0.
Our Application process thousands of requests per minute.
After this migration, we get 30/40 errors per day. (different times / different load periods – It does not depends load traffic)
How do we fix this error? Do you have any suggestions about code?
Trace:
System.Net.Http.HttpRequestException: An error occurred while sending the request.
---> System.Net.Http.HttpIOException: The response ended prematurely. (ResponseEnded)
at System.Net.Http.HttpConnection.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.HttpConnection.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.AuthenticationHelper.SendWithNtAuthAsync(HttpRequestMessage request, Uri authUri, Boolean async, ICredentials credentials, Boolean isProxyAuth, HttpConnection connection, HttpConnectionPool connectionPool, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.AuthenticationHelper.SendWithAuthAsync(HttpRequestMessage request, Uri authUri, Boolean async, ICredentials credentials, Boolean preAuthenticate, Boolean isProxyAuth, Boolean doRequestAuth, HttpConnectionPool pool, CancellationToken cancellationToken)
at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.<SendCoreAsync>g__Core|5_0(HttpRequestMessage request, Boolean useAsync, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.<SendCoreAsync>g__Core|5_0(HttpRequestMessage request, Boolean useAsync, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
Code:
using (var serviceScope = SiriusServiceScopeFactory.Instance.CreateScope())
{
var client = serviceScope.GetHttpClient(HttpClientConstants.CoreHttpClient);
using (var httpRequest = new HttpRequestMessage(HttpMethod.Post, requestUri))
{
SetHttpHeaders(httpRequest);
byte[] zippedBytes;
using (var ms = new MemoryStream())
{
CoreGZipUtils.Zip(ms, request);
zippedBytes = ms.ToArray();
}
HttpContent content = new ByteArrayContent(zippedBytes);
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
httpRequest.Content = content;
using (var cts = new CancellationTokenSource(timeout))
{
HttpResponseMessage response = null;
try
{
response = await client.SendAsync(httpRequest, cts.Token).ConfigureAwait(false);
}
catch (TaskCanceledException ex)
{
throw new SrSystemException(string.Empty, string.Format("Timeout exception occured while connecting to plm. PLM Uri:{0}, Service Name:{1}, Timeout:{2} second", this.plmUri, this.corePlmClientHeaderInfo.ServiceName, timeout));
}
return await ReadHttpResponseFromMessageAsync<TResponse>(response).ConfigureAwait(false);
}
}
}
internal static IServiceCollection AddCoreHttpClient(this IServiceCollection services)
{
services.AddHttpClient(HttpClientConstants.CoreHttpClient).ConfigurePrimaryHttpMessageHandler(x =>
new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
UseCookies = false,
AllowAutoRedirect = false,
UseDefaultCredentials = true,
});
return services;
}Metadata
Metadata
Assignees
Labels
area-System.Net.HttpquestionAnswer questions and provide assistance, not an issue with source code or documentation.Answer questions and provide assistance, not an issue with source code or documentation.