HttpClientJsonExtensions.PostAsJsonAsync Throws NullReferenceException When Serializing the Property/Value Message = "Message:"
xcjs opened this issue · 2 comments
xcjs commented
We have a LogMessage record defined as:
public record LogMessage
{
// LogLevel is an enumeration.
public LogLevel LogLevel { get; set; }
public Exception? Exception { get; set; }
public string? Message { get; set; }
}
I have so far narrowed this down to using PostAsJsonAsync with the following characteristics:
A NullReferenceException
is thrown from the MockHttpMessageHandler
when we assign the string literal "Message:"
to LogMessage.Message
.
await _http.PostAsJsonAsync(
RoutesAndFunctions.LogMessagesFunctionUrl,
new LogMessage
{
LogLevel = logLevel,
Exception = null,
Message = "Message:"
});
The default HttpMessageHandler in .NET Core 6 does not exhibit the same behavior.
richardszalay commented
Can you provide the stack trace of the exception and provide a self-contained minimal repro? The sample test passes.
public enum LogLevel
{
Information = 1
}
public record LogMessage
{
// LogLevel is an enumeration.
public LogLevel LogLevel { get; set; }
public Exception? Exception { get; set; }
public string? Message { get; set; }
}
public class Issue108Tests
{
[Fact]
public async Task Can_post_specific_message()
{
var mockHttp = new MockHttpMessageHandler();
mockHttp.When("http://localhost/test")
.Respond(HttpStatusCode.OK);
var client = mockHttp.ToHttpClient();
var response = await client.PostAsJsonAsync("http://localhost/test",
new LogMessage
{
LogLevel = LogLevel.Information,
Exception = null,
Message = "Message:"
});
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
richardszalay commented
I'm going to close this issue as I haven't heard from you with a repro for the issue.