C
C#17mo ago
Eistee

✅ Dealing/resolving nullable warnings

I try to deal with nullable warnings among others e. g. CS8604 "Possible null reference argument for parameter." I have the following sample code of a C# 7.0 worker service:
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
if (!PrerequisitesMet())
{
_logger.LogError("the options checked an is NULL");
throw new InvalidOperationException("prerequisites not met.");
}

while (!stoppingToken.IsCancellationRequested)
{
try
{
XDocument doc;

if (_options.ApiQueryURL is not null)
{
doc = await _httpService.GetXmlResponseAsync(_options.ApiQueryURL);

if (doc.Nodes().Any())
{
devices = GetDevices(doc);
await _dataService.UpdateDevicesAsync(devices);
}
}
}
catch (Exception ex)
{
if (OperatingSystem.IsWindows() && _eventLog != null)
{
_eventLog.WriteEntry($"Error occurred: {ex.Message}", EventLogEntryType.Error);
}
}

await Task.Delay(TimeSpan.FromHours(1), stoppingToken);
}
}

private bool PrerequisitesMet()
{
// options
if (_options == null)
return false;

// option "ApiQueryURL"
if (string.IsNullOrWhiteSpace(_options.ApiQueryURL))
return false;

// option "TargetString"
if (string.IsNullOrWhiteSpace(_options.TargetString))
return false;

return true;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
if (!PrerequisitesMet())
{
_logger.LogError("the options checked an is NULL");
throw new InvalidOperationException("prerequisites not met.");
}

while (!stoppingToken.IsCancellationRequested)
{
try
{
XDocument doc;

if (_options.ApiQueryURL is not null)
{
doc = await _httpService.GetXmlResponseAsync(_options.ApiQueryURL);

if (doc.Nodes().Any())
{
devices = GetDevices(doc);
await _dataService.UpdateDevicesAsync(devices);
}
}
}
catch (Exception ex)
{
if (OperatingSystem.IsWindows() && _eventLog != null)
{
_eventLog.WriteEntry($"Error occurred: {ex.Message}", EventLogEntryType.Error);
}
}

await Task.Delay(TimeSpan.FromHours(1), stoppingToken);
}
}

private bool PrerequisitesMet()
{
// options
if (_options == null)
return false;

// option "ApiQueryURL"
if (string.IsNullOrWhiteSpace(_options.ApiQueryURL))
return false;

// option "TargetString"
if (string.IsNullOrWhiteSpace(_options.TargetString))
return false;

return true;
}
Is that so well solved that you always check for is not null?
5 Replies
Denis
Denis17mo ago
I suggest not extracting your null checks into a separate method. If you want a cleaner look, you can use Guards from the communitytoolkit by microsoft Or just use plain old if statements The logging of the issue should be done higher - once the exception is caught, you log the issue Because this way you lose information as to what requirement was not met and you make it more difficult for yourself this way
Denis
Denis17mo ago
I think Guard will give you the clean look you are looking for, Microsoft uses it
Eistee
EisteeOP17mo ago
Okay the guard class is really very cool. I try to use it
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server