Dotnet 8.x Update

- updated all nugets to latest 8.x versions
- linting fixes
- switched the application insights to use the connection string.
This commit is contained in:
Brett Hewitson 2025-03-21 06:46:38 +10:00
parent 5d8fe6ca90
commit f5a96f965a
19 changed files with 64 additions and 98 deletions

View file

@ -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);
}
}
}
}

View file

@ -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<string>
{
"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<string>
{
ex.Message
}
var response = new ErrorResponse
{
Errors = [ex.Message]
};
return StatusCode(StatusCodes.Status500InternalServerError, response);
}
}
}
}

View file

@ -11,8 +11,8 @@ public static class ServerQueryExtensions
{
var settings = configuration.GetSectionAs<ServerQuerySettings>();
services.AddSingleton(settings);
services.AddScoped<IServerQueryService, QueryMasterService>();
return services;
}
}
}

View file

@ -11,4 +11,4 @@ public static class ServiceCollectionExtensions
return configuration.GetSection(sectionName).Get<T>();
}
}
}

View file

@ -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;
}
}
}
}

View file

@ -11,5 +11,5 @@ public class ErrorResponse
/// </summary>
[Required]
[Description("List of errors.")]
public ICollection<string> Errors { get; set; } = new List<string>();
}
public ICollection<string> Errors { get; set; } = [];
}

View file

@ -11,4 +11,4 @@ public class ServerStatusResponse
[Required]
[Description("True if the server is available; otherwise false.")]
public string Available { get; set; } = false.ToString();
}
}

View file

@ -9,4 +9,4 @@ public class ManagerCode
public string Name { get; set; } = string.Empty;
[DataMember]
public string Code { get; set; } = string.Empty;
}
}

View file

@ -4,6 +4,6 @@ namespace ServerManager.WebApplication.Models.Data
{
public class ServerQuerySettings
{
public List<ManagerCode> ManagerCodes { get; set; }
public List<ManagerCode> ManagerCodes { get; set; } = [];
}
}
}

View file

@ -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<string> messages) : base()
public ServerManagerApiException(int statusCode, ICollection<string> messages)
{
StatusCode = statusCode;
Messages = messages;
}
public ServerManagerApiException(int statusCode, ICollection<string> messages, Exception innerException) : base(null, innerException)
public ServerManagerApiException(int statusCode, ICollection<string> 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<string> Messages { get; private set; } = new List<string>();
}
}

View file

@ -16,4 +16,4 @@ public class Program
{
webBuilder.UseStartup<Startup>();
});
}
}

View file

@ -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 @@
}
}
}
}
}

View file

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Configurations>Debug;Release</Configurations>
<ApplicationIcon>Art\favicon.ico</ApplicationIcon>
</PropertyGroup>
@ -16,14 +16,16 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.1" />
<PackageReference Include="Asp.Versioning.Mvc" Version="8.1.0" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.23.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\QueryMaster\QueryMaster.csproj" />
</ItemGroup>
</Project>
</Project>

View file

@ -3,4 +3,4 @@
public interface IServerQueryService
{
bool CheckServerStatus(string managerCode, string managerVersion, string ipString, int port);
}
}

View file

@ -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<string>();
if (string.IsNullOrWhiteSpace(managerCode))
{
errors.Add("Manager code is required.");
}
else
{
var managerCodes = _settings.ManagerCodes ?? new List<ManagerCode>();
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);
}
}
}
}

View file

@ -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<bool>("EnableSwagger");
if (enableSwagger)
{
var swaggerRoutePrefix = Configuration.GetValue<string>("SwaggerRoutePrefix");
if (!string.IsNullOrWhiteSpace(swaggerRoutePrefix) && !swaggerRoutePrefix.EndsWith("/"))
{
swaggerRoutePrefix += "/";
}
app.UseSwagger();
app.UseSwaggerUI(o =>
@ -105,4 +97,4 @@ public class Startup
});
});
}
}
}

View file

@ -12,7 +12,7 @@
}
},
"ApplicationInsights": {
"InstrumentationKey": ""
"ConnectionString": ""
},
"EnableSwagger": true,
@ -26,4 +26,4 @@
}
]
}
}
}

View file

@ -18,4 +18,4 @@
}
]
}
}
}

View file

@ -12,4 +12,4 @@
}
},
"AllowedHosts": "*"
}
}