From f5a96f965a31a9ac8b947b0da06d157ad4890caf Mon Sep 17 00:00:00 2001 From: Brett Hewitson Date: Fri, 21 Mar 2025 06:46:38 +1000 Subject: [PATCH] Dotnet 8.x Update - updated all nugets to latest 8.x versions - linting fixes - switched the application insights to use the connection string. --- .../ApiVersion1/PluginController.cs | 5 +-- .../ApiVersion1/ServerController.cs | 36 ++++++++----------- .../Extensions/ServerQueryExtensions.cs | 4 +-- .../Extensions/ServiceCollectionExtensions.cs | 2 +- .../Middleware/SwaggerDefaultValues.cs | 16 +++------ .../Models/ApiVersion1/ErrorResponse.cs | 4 +-- .../ApiVersion1/ServerStatusResponse.cs | 2 +- .../Models/Data/ManagerCode.cs | 2 +- .../Models/Data/ServerQuerySettings.cs | 4 +-- .../Models/ServerManagerApiException.cs | 15 ++++---- src/ServerManager.WebApplication/Program.cs | 2 +- .../Properties/launchSettings.json | 4 +-- .../ServerManager.WebApplication.csproj | 14 ++++---- .../Services/IServerQueryService.cs | 2 +- .../Services/QueryMasterService.cs | 22 +++--------- src/ServerManager.WebApplication/Startup.cs | 20 ++++------- .../appsettings.Development.json | 4 +-- .../appsettings.Production.json | 2 +- .../appsettings.json | 2 +- 19 files changed, 64 insertions(+), 98 deletions(-) diff --git a/src/ServerManager.WebApplication/Controllers/ApiVersion1/PluginController.cs b/src/ServerManager.WebApplication/Controllers/ApiVersion1/PluginController.cs index 04ce271b..281ef7a9 100644 --- a/src/ServerManager.WebApplication/Controllers/ApiVersion1/PluginController.cs +++ b/src/ServerManager.WebApplication/Controllers/ApiVersion1/PluginController.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Http; +using Asp.Versioning; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; @@ -36,4 +37,4 @@ public class PluginController : ControllerBase return StatusCode(StatusCodes.Status500InternalServerError, false); } } -} +} \ No newline at end of file diff --git a/src/ServerManager.WebApplication/Controllers/ApiVersion1/ServerController.cs b/src/ServerManager.WebApplication/Controllers/ApiVersion1/ServerController.cs index 140e31e5..967d72c4 100644 --- a/src/ServerManager.WebApplication/Controllers/ApiVersion1/ServerController.cs +++ b/src/ServerManager.WebApplication/Controllers/ApiVersion1/ServerController.cs @@ -1,5 +1,5 @@ -using System; -using System.Collections.Generic; +using Asp.Versioning; +using System; using System.Diagnostics; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -59,12 +59,9 @@ public class ServerController : ControllerBase // check for valid service if (_serverQueryService == null) { - var response = new ErrorResponse - { - Errors = new List - { - "Server query service not available." - } + var response = new ErrorResponse + { + Errors = ["Server query service not available."] }; return StatusCode(StatusCodes.Status503ServiceUnavailable, response); } @@ -74,9 +71,9 @@ public class ServerController : ControllerBase var stopWatch = Stopwatch.StartNew(); var result = _serverQueryService.CheckServerStatus(managerCode, managerVersion, ipString, port); - var response = new ServerStatusResponse - { - Available = result.ToString() + var response = new ServerStatusResponse + { + Available = result.ToString() }; stopWatch.Stop(); @@ -85,22 +82,19 @@ public class ServerController : ControllerBase } catch (ServerManagerApiException ex) { - var response = new ErrorResponse - { - Errors = ex.Messages + var response = new ErrorResponse + { + Errors = ex.Messages }; return StatusCode(ex.StatusCode, response); } catch (Exception ex) { - var response = new ErrorResponse - { - Errors = new List - { - ex.Message - } + var response = new ErrorResponse + { + Errors = [ex.Message] }; return StatusCode(StatusCodes.Status500InternalServerError, response); } } -} +} \ No newline at end of file diff --git a/src/ServerManager.WebApplication/Extensions/ServerQueryExtensions.cs b/src/ServerManager.WebApplication/Extensions/ServerQueryExtensions.cs index d7350088..cfa04ed7 100644 --- a/src/ServerManager.WebApplication/Extensions/ServerQueryExtensions.cs +++ b/src/ServerManager.WebApplication/Extensions/ServerQueryExtensions.cs @@ -11,8 +11,8 @@ public static class ServerQueryExtensions { var settings = configuration.GetSectionAs(); services.AddSingleton(settings); - + services.AddScoped(); return services; } -} +} \ No newline at end of file diff --git a/src/ServerManager.WebApplication/Extensions/ServiceCollectionExtensions.cs b/src/ServerManager.WebApplication/Extensions/ServiceCollectionExtensions.cs index 192aa414..27c926d7 100644 --- a/src/ServerManager.WebApplication/Extensions/ServiceCollectionExtensions.cs +++ b/src/ServerManager.WebApplication/Extensions/ServiceCollectionExtensions.cs @@ -11,4 +11,4 @@ public static class ServiceCollectionExtensions return configuration.GetSection(sectionName).Get(); } -} +} \ No newline at end of file diff --git a/src/ServerManager.WebApplication/Middleware/SwaggerDefaultValues.cs b/src/ServerManager.WebApplication/Middleware/SwaggerDefaultValues.cs index 606a9796..f9bce892 100644 --- a/src/ServerManager.WebApplication/Middleware/SwaggerDefaultValues.cs +++ b/src/ServerManager.WebApplication/Middleware/SwaggerDefaultValues.cs @@ -21,27 +21,19 @@ public class SwaggerDefaultValues : IOperationFilter foreach (var contentType in response.Content.Keys) { - if (!responseType.ApiResponseFormats.Any(x => x.MediaType == contentType)) - { + if (responseType.ApiResponseFormats.All(x => x.MediaType != contentType)) response.Content.Remove(contentType); - } } } if (operation.Parameters is null) - { return; - } foreach (var parameter in operation.Parameters) { - var description = apiDescription.ParameterDescriptions - .First(p => p.Name == parameter.Name); + var description = apiDescription.ParameterDescriptions.First(p => p.Name == parameter.Name); - if (parameter.Description is null) - { - parameter.Description = description.ModelMetadata?.Description; - } + parameter.Description ??= description.ModelMetadata?.Description; if (parameter.Schema.Default is null && description.DefaultValue is not null) { @@ -52,4 +44,4 @@ public class SwaggerDefaultValues : IOperationFilter parameter.Required |= description.IsRequired; } } -} +} \ No newline at end of file diff --git a/src/ServerManager.WebApplication/Models/ApiVersion1/ErrorResponse.cs b/src/ServerManager.WebApplication/Models/ApiVersion1/ErrorResponse.cs index 9b3de18c..97bb6818 100644 --- a/src/ServerManager.WebApplication/Models/ApiVersion1/ErrorResponse.cs +++ b/src/ServerManager.WebApplication/Models/ApiVersion1/ErrorResponse.cs @@ -11,5 +11,5 @@ public class ErrorResponse /// [Required] [Description("List of errors.")] - public ICollection Errors { get; set; } = new List(); -} + public ICollection Errors { get; set; } = []; +} \ No newline at end of file diff --git a/src/ServerManager.WebApplication/Models/ApiVersion1/ServerStatusResponse.cs b/src/ServerManager.WebApplication/Models/ApiVersion1/ServerStatusResponse.cs index 0d69cf76..6afd577e 100644 --- a/src/ServerManager.WebApplication/Models/ApiVersion1/ServerStatusResponse.cs +++ b/src/ServerManager.WebApplication/Models/ApiVersion1/ServerStatusResponse.cs @@ -11,4 +11,4 @@ public class ServerStatusResponse [Required] [Description("True if the server is available; otherwise false.")] public string Available { get; set; } = false.ToString(); -} +} \ No newline at end of file diff --git a/src/ServerManager.WebApplication/Models/Data/ManagerCode.cs b/src/ServerManager.WebApplication/Models/Data/ManagerCode.cs index 73f1a373..22a82502 100644 --- a/src/ServerManager.WebApplication/Models/Data/ManagerCode.cs +++ b/src/ServerManager.WebApplication/Models/Data/ManagerCode.cs @@ -9,4 +9,4 @@ public class ManagerCode public string Name { get; set; } = string.Empty; [DataMember] public string Code { get; set; } = string.Empty; -} +} \ No newline at end of file diff --git a/src/ServerManager.WebApplication/Models/Data/ServerQuerySettings.cs b/src/ServerManager.WebApplication/Models/Data/ServerQuerySettings.cs index 20993d8f..3b854384 100644 --- a/src/ServerManager.WebApplication/Models/Data/ServerQuerySettings.cs +++ b/src/ServerManager.WebApplication/Models/Data/ServerQuerySettings.cs @@ -4,6 +4,6 @@ namespace ServerManager.WebApplication.Models.Data { public class ServerQuerySettings { - public List ManagerCodes { get; set; } + public List ManagerCodes { get; set; } = []; } -} +} \ No newline at end of file diff --git a/src/ServerManager.WebApplication/Models/ServerManagerApiException.cs b/src/ServerManager.WebApplication/Models/ServerManagerApiException.cs index be4d76d4..5d7126be 100644 --- a/src/ServerManager.WebApplication/Models/ServerManagerApiException.cs +++ b/src/ServerManager.WebApplication/Models/ServerManagerApiException.cs @@ -1,30 +1,27 @@ using System; using System.Collections.Generic; -using System.Runtime.Serialization; namespace ServerManager.WebApplication.Models; public class ServerManagerApiException : Exception { - public ServerManagerApiException() : base() + public ServerManagerApiException() { } - public ServerManagerApiException(int statusCode, ICollection messages) : base() + public ServerManagerApiException(int statusCode, ICollection messages) { StatusCode = statusCode; Messages = messages; } - public ServerManagerApiException(int statusCode, ICollection messages, Exception innerException) : base(null, innerException) + public ServerManagerApiException(int statusCode, ICollection messages, Exception innerException) + : base(null, innerException) { StatusCode = statusCode; Messages = messages; } - protected ServerManagerApiException(SerializationInfo info, StreamingContext context) : base(info, context) - { } - - public int StatusCode { get; private set; } = 0; + public int StatusCode { get; private set; } public ICollection Messages { get; private set; } = new List(); -} +} \ No newline at end of file diff --git a/src/ServerManager.WebApplication/Program.cs b/src/ServerManager.WebApplication/Program.cs index 987d9163..8017b883 100644 --- a/src/ServerManager.WebApplication/Program.cs +++ b/src/ServerManager.WebApplication/Program.cs @@ -16,4 +16,4 @@ public class Program { webBuilder.UseStartup(); }); -} +} \ No newline at end of file diff --git a/src/ServerManager.WebApplication/Properties/launchSettings.json b/src/ServerManager.WebApplication/Properties/launchSettings.json index 765a1f22..32c0dd56 100644 --- a/src/ServerManager.WebApplication/Properties/launchSettings.json +++ b/src/ServerManager.WebApplication/Properties/launchSettings.json @@ -19,7 +19,7 @@ }, "ServerManager.WebApplication": { "commandName": "Project", - "dotnetRunMessages": "true", + "dotnetRunMessages": true, "launchBrowser": true, "launchUrl": "swagger", "applicationUrl": "https://localhost:5001;http://localhost:5000", @@ -28,4 +28,4 @@ } } } -} +} \ No newline at end of file diff --git a/src/ServerManager.WebApplication/ServerManager.WebApplication.csproj b/src/ServerManager.WebApplication/ServerManager.WebApplication.csproj index e6e58b0e..50d58538 100644 --- a/src/ServerManager.WebApplication/ServerManager.WebApplication.csproj +++ b/src/ServerManager.WebApplication/ServerManager.WebApplication.csproj @@ -1,7 +1,7 @@  - net6.0 + net8.0 Debug;Release Art\favicon.ico @@ -16,14 +16,16 @@ - - - - + + + + + + - + \ No newline at end of file diff --git a/src/ServerManager.WebApplication/Services/IServerQueryService.cs b/src/ServerManager.WebApplication/Services/IServerQueryService.cs index bd0e4de3..47554b38 100644 --- a/src/ServerManager.WebApplication/Services/IServerQueryService.cs +++ b/src/ServerManager.WebApplication/Services/IServerQueryService.cs @@ -3,4 +3,4 @@ public interface IServerQueryService { bool CheckServerStatus(string managerCode, string managerVersion, string ipString, int port); -} +} \ No newline at end of file diff --git a/src/ServerManager.WebApplication/Services/QueryMasterService.cs b/src/ServerManager.WebApplication/Services/QueryMasterService.cs index 741b52ba..624322e0 100644 --- a/src/ServerManager.WebApplication/Services/QueryMasterService.cs +++ b/src/ServerManager.WebApplication/Services/QueryMasterService.cs @@ -25,7 +25,7 @@ public class QueryMasterService : IServerQueryService try { using var server = ServerQuery.GetServerInstance(EngineType.Source, ipString, (ushort)port); - return server.GetInfo() != null; + return server.GetInfo() is not null; } catch { @@ -38,35 +38,23 @@ public class QueryMasterService : IServerQueryService var errors = new List(); if (string.IsNullOrWhiteSpace(managerCode)) - { errors.Add("Manager code is required."); - } else { - var managerCodes = _settings.ManagerCodes ?? new List(); + var managerCodes = _settings.ManagerCodes ?? []; if (!managerCodes.Any(c => c.Code.Equals(managerCode, StringComparison.OrdinalIgnoreCase))) - { errors.Add("Manager code is invalid."); - } } if (string.IsNullOrWhiteSpace(ipString)) - { errors.Add("IP Address is required."); - } - else if (!IPAddress.TryParse(ipString, out IPAddress _)) - { + else if (!IPAddress.TryParse(ipString, out _)) errors.Add("IP Address is invalid."); - } - if (port <= ushort.MinValue || port >= ushort.MaxValue) - { + if (port is <= ushort.MinValue or >= ushort.MaxValue) errors.Add($"Valid port is required ({ushort.MinValue} to {ushort.MaxValue})."); - } if (errors.Count > 0) - { throw new ServerManagerApiException(StatusCodes.Status400BadRequest, errors); - } } -} +} \ No newline at end of file diff --git a/src/ServerManager.WebApplication/Startup.cs b/src/ServerManager.WebApplication/Startup.cs index 2b9ac13b..adefd20a 100644 --- a/src/ServerManager.WebApplication/Startup.cs +++ b/src/ServerManager.WebApplication/Startup.cs @@ -1,9 +1,8 @@ +using Asp.Versioning; +using Asp.Versioning.ApiExplorer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.ApiExplorer; -using Microsoft.AspNetCore.Mvc.Versioning; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -40,13 +39,10 @@ public class Startup new MediaTypeApiVersionReader("Version"), new HeaderApiVersionReader("X-Version") ); - }); - - services.AddVersionedApiExplorer(o => + }).AddApiExplorer(options => { - // add the versioned api explorer, which also adds IApiVersionDescriptionProvider service - // note: the specified format code will format the version as "'v'major[.minor][-status]" - o.GroupNameFormat = "'v'VVV"; + options.GroupNameFormat = "'v'VVV"; + options.SubstituteApiVersionInUrl = true; }); services.AddServerQueryServices(Configuration); @@ -64,18 +60,14 @@ public class Startup public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApiVersionDescriptionProvider provider) { if (env.IsDevelopment()) - { app.UseDeveloperExceptionPage(); - } var enableSwagger = Configuration.GetValue("EnableSwagger"); if (enableSwagger) { var swaggerRoutePrefix = Configuration.GetValue("SwaggerRoutePrefix"); if (!string.IsNullOrWhiteSpace(swaggerRoutePrefix) && !swaggerRoutePrefix.EndsWith("/")) - { swaggerRoutePrefix += "/"; - } app.UseSwagger(); app.UseSwaggerUI(o => @@ -105,4 +97,4 @@ public class Startup }); }); } -} +} \ No newline at end of file diff --git a/src/ServerManager.WebApplication/appsettings.Development.json b/src/ServerManager.WebApplication/appsettings.Development.json index b9619833..53d2fdc4 100644 --- a/src/ServerManager.WebApplication/appsettings.Development.json +++ b/src/ServerManager.WebApplication/appsettings.Development.json @@ -12,7 +12,7 @@ } }, "ApplicationInsights": { - "InstrumentationKey": "" + "ConnectionString": "" }, "EnableSwagger": true, @@ -26,4 +26,4 @@ } ] } -} +} \ No newline at end of file diff --git a/src/ServerManager.WebApplication/appsettings.Production.json b/src/ServerManager.WebApplication/appsettings.Production.json index 28acc945..62f4542a 100644 --- a/src/ServerManager.WebApplication/appsettings.Production.json +++ b/src/ServerManager.WebApplication/appsettings.Production.json @@ -18,4 +18,4 @@ } ] } -} +} \ No newline at end of file diff --git a/src/ServerManager.WebApplication/appsettings.json b/src/ServerManager.WebApplication/appsettings.json index 02420ea3..df9cabd0 100644 --- a/src/ServerManager.WebApplication/appsettings.json +++ b/src/ServerManager.WebApplication/appsettings.json @@ -12,4 +12,4 @@ } }, "AllowedHosts": "*" -} +} \ No newline at end of file