I've just migrated an .NET3.1 application to .NET6 (I am using VS 2022) and I noticed that they've introduced some constraints related to nullable types. I fixed all my code to follow best practices, but not sure how to go about this issue:
When creating a new HttpClient instance it returns a nullable HttpClient type (code is executed inside a Polly retry policy):
public async Task<List<InterestedParty>> GetInterestedPartiesAsync()
{
return await _retryPolicy.ExecuteAsync(async () =>
{
using var client = new HttpClient()
const string endpoint = "my-endpoint";
var url = $"{endpoint}/test";
var response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
var jsonString = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<List<InterestedParty>>(jsonString);
});
}
The compiler is complaining about a possible null reference return. I know I could suppress the warning by using:
#pragma warning disable CS8603 // Possible null reference return.
but I don't think it's the best solution.
Any other suggestions? Thank you.
> but the problem is on the returned object. I am returning a deserialized object which in theory may be null, that's the root cause. thanks for pointing me in the right direction
– Dragos Stoica Dec 14 '21 at 10:46?>`
– DavidG Dec 14 '21 at 11:02