How to avoid this Warning
warning CS8602: Dereference of a possibly null reference
The code:
public async Task<IEnumerable<T>> ExecuteQuery<T>(string commandText, string ctx, CancellationToken cancellationToken, CommandType commandType = CommandType.Text, object? paramSet = null)
{
var dtsrc = CheckConnectionToDbServer(ctx, cancellationToken);
//if (dtsrc == null) { _logger.LogTrace($"{methodName}: No datasource!"); return 0; }
var con = dtsrc.OpenConnection();//<---?
IEnumerable<T> result2 = await con.QueryAsync<T>(commandText, paramSet, commandType: commandType).ConfigureAwait(false);
return result2;
}
--
If dtsrc
is null then con
is null too and I cannot return Task<IEnumerable<T>>
from the function. How to fix it?
And the 2nd function: how to return null or something like in case of no connection?
public async Task<T> ExecuteScalarQuery<T>(string commandText, string ctx, CancellationToken cancellationToken, CommandType commandType = CommandType.Text, object? paramSet = null)
{
string methodName = nameof(ExecuteScalarQuery);
_logger.LogTrace($"{methodName}: {ctx}");
var dtsrc = CheckConnectionToDbServer(ctx, cancellationToken);
if (dtsrc == null)
{
_logger.LogTrace($"{methodName}: No datasource!");
//return // -<---???
}
var con = dtsrc.OpenConnection();
return await con.QuerySingleAsync<T>(commandText, paramSet, commandType: commandType).ConfigureAwait(false);
}