From 83f3d2b7200902346b53f9bcbe92b153f3cde180 Mon Sep 17 00:00:00 2001 From: jahav Date: Fri, 4 Oct 2024 00:58:13 +0200 Subject: [PATCH] SonarQube worthless nagging. --- sample/WebApi/Program.cs | 13 ++++++++----- .../SqlDatabaseTenantFactory.cs | 8 ++++++-- src/DataIsland.SqlServer/SqlServerSpecChecker.cs | 8 ++++++++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/sample/WebApi/Program.cs b/sample/WebApi/Program.cs index 994397f..3b911ac 100644 --- a/sample/WebApi/Program.cs +++ b/sample/WebApi/Program.cs @@ -3,13 +3,13 @@ var builder = WebApplication.CreateBuilder(args); // Add services to the container. -builder.Services.AddDbContext(options => +builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("AppDb")) ); var app = builder.Build(); -app.MapGet("/db-name", async (AppDbContext dbContext) => +app.MapGet("/db-name", async (WebApi.AppDbContext dbContext) => { // Wait a little before and after, so we know two requests are handled at the same time. await Task.Delay(2500); @@ -18,9 +18,12 @@ return Results.Text(dbName); }); -app.Run(); +await app.RunAsync(); public partial class Program; -public class AppDbContext(DbContextOptions options) - : DbContext(options); +namespace WebApi +{ + public class AppDbContext(DbContextOptions options) + : DbContext(options); +} diff --git a/src/DataIsland.SqlServer/SqlDatabaseTenantFactory.cs b/src/DataIsland.SqlServer/SqlDatabaseTenantFactory.cs index aab38d1..fe23b33 100644 --- a/src/DataIsland.SqlServer/SqlDatabaseTenantFactory.cs +++ b/src/DataIsland.SqlServer/SqlDatabaseTenantFactory.cs @@ -30,7 +30,7 @@ public async Task AddTenantAsync(SqlServerComponent component // Because connection string of component doesn't differ, it will be pooled. using var connection = new SqlConnection(component.ConnectionString); - connection.Open(); + await connection.OpenAsync(); var command = connection.CreateCommand(); if (!spec.HasDataSource) @@ -61,12 +61,16 @@ public async Task AddTenantAsync(SqlServerComponent component command.CommandText = cmd.ToString(); } - command.ExecuteNonQuery(); + await command.ExecuteNonQueryAsync(); if (spec.MaxDop is { } maxDop) await SetMaxDopAsync(connection, tenantDbName, maxDop); +#if NET6_0_OR_GREATER + await connection.CloseAsync(); +#else connection.Close(); +#endif var tenantConnectionString = new SqlConnectionStringBuilder(component.ConnectionString) { diff --git a/src/DataIsland.SqlServer/SqlServerSpecChecker.cs b/src/DataIsland.SqlServer/SqlServerSpecChecker.cs index 70940aa..6aaf3f6 100644 --- a/src/DataIsland.SqlServer/SqlServerSpecChecker.cs +++ b/src/DataIsland.SqlServer/SqlServerSpecChecker.cs @@ -15,7 +15,11 @@ internal static class SqlServerSpecChecker var command = connection.CreateCommand(); command.CommandText = "SELECT SERVERPROPERTY('Collation')"; var collation = (string?)await command.ExecuteScalarAsync(); +#if NET6_0_OR_GREATER + await connection.CloseAsync(); +#else connection.Close(); +#endif if (collation != componentSpec.Collation) return $"Unable to find a server with collation {componentSpec.Collation}."; } @@ -38,7 +42,11 @@ [name] nvarchar(35), """; var clrConfigValue = (int?)await command.ExecuteScalarAsync(); var actualClrEnabled = clrConfigValue is not null && clrConfigValue.Value != 0; +#if NET6_0_OR_GREATER + await connection.CloseAsync(); +#else connection.Close(); +#endif if (actualClrEnabled != desiredClrEnabled) return $"Unable to find a server with clr {(desiredClrEnabled ? "enabled" : "disabled")}."; }