I have just upgraded our project from .NET 6 (working perfectly fine) to .NET 8. I encounter intermittent issue when sending out request with HttpClient
(System.Net.Http
) using GetAsync/PostAsync/SendAsync
with the following error:
System.FormatException: Input string was not in a correct format. Failure to parse near offset 2. Expected an ASCII digit.
at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ReadOnlySpan
1 args) at System.Text.StringBuilder.AppendFormat(String format, Object[] args) at Prometheus.MeterAdapter.TranslateInstrumentDescriptionToPrometheusHelp(Instrument instrument) at Prometheus.MeterAdapter.OnInstrumentPublished(Instrument instrument, MeterListener listener) at System.Diagnostics.Metrics.Instrument.Publish() at System.Diagnostics.Metrics.UpDownCounter
1..ctor(Meter meter, String name, String unit, String description, IEnumerable1 tags) > at System.Diagnostics.Metrics.Meter.<>c__DisplayClass37_0
1.b__0()
at System.Diagnostics.Metrics.Meter.GetOrCreateInstrument[T ](Type instrumentType, String name, String unit, String description, IEnumerable1 tags, Func
1 instrumentCreator)
at System.Net.Http.Metrics.MetricsHandler..ctor(HttpMessageHandler innerHandler, IMeterFactory meterFactory, Meter& meter)
at System.Net.Http.SocketsHttpHandler.SetupHandlerChain()
at System.Net.Http.SocketsHttpHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.g__Core|5_0(HttpRequestMessage request, Boolean useAsync, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.g__Core|5_0(HttpRequestMessage request, Boolean useAsync, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
at MyService.GetUsers(Guid clientid) in /app/src/Users/contact.cs:line ##
The code:
var url = $"{BaseUrl}/user?contact=12";
// Error happens on this line
var responseData = await _client.GetAsync(url);
I have another line of code which uses PostAsync
, and I get that intermittent error with that call, too:
var url = $"{BaseUrl}/users/search";
var request = new UserSearchRequest
{
Username = username,
FamilyId = familyid,
RelationId = relationid,
};
var requestBody = new StringContent(JsonConvert.SerializeObject(request, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }), Encoding.UTF8, "application/json");
// error happens on this line too
var responseData = await _client.PostAsync(url, requestBody);
From the log, it doesn't look like the request is sent out at all. What did I do wrong?
Has anybody encounter this issue before? Would anybody help please? Thank you so much.
I have tried using SendAsync
as well, and it get that error intermittently just the same. I crossed check all the package updates too for .NET 8, but correct me if I'm wrong, this System.Net.Http
library actually comes with .NET 8.
I'm expecting the GetAsync
/ SendAsync
/ PostAsync
to send out the request with no error as per previous .NET 6 which is already in PRODUCTION.
While developing a .Net Core 8 application i faced the same error.
The solution was to update the Nugget Package on all Class Libraries and API to 8.2.1 Version.
<PackageReference Include="prometheus-net" Version="8.2.1" />
<PackageReference Include="prometheus-net.AspNetCore" Version="8.2.1" />
<PackageReference Include="prometheus-net.AspNetCore.HealthChecks" Version="8.2.1" />