Update to .NET Core 3.1 and apply Roslyn suggestions

This commit is contained in:
2020-07-18 16:41:01 +00:00
parent e893299224
commit de0d6cdc82
20 changed files with 169 additions and 102 deletions

View File

@@ -1,30 +1,30 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (remote console)",
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "publish",
"program": "dotnet",
"args": [
"/home/ckaczor/Weather/SerialReader/SerialReader.dll"
],
"cwd": "/home/ckaczor/Weather/SerialReader",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/ChrisKaczor.HomeMonitor.Weather.SerialReader.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"console": "internalConsole",
"pipeTransport": {
"pipeCwd": "${workspaceFolder}",
"pipeProgram": "C:\\Program Files (x86)\\PuTTY\\PLINK.EXE",
"pipeArgs": [
"root@172.23.10.6"
],
"debuggerPath": "/home/ckaczor/vsdbg/vsdbg"
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"internalConsoleOptions": "openOnSessionStart"
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}

View File

@@ -7,29 +7,54 @@
"type": "process",
"args": [
"build",
"${workspaceFolder}/SerialReader.csproj"
"${workspaceFolder}/SerialReader.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile",
"group": {
"kind": "build",
"isDefault": true
}
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/SerialReader.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/SerialReader.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "build",
"command": "dotnet",
"type": "shell",
"args": [
"build",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "always",
"panel": "shared"
"reveal": "silent"
},
"options": {
"cwd": "${workspaceFolder}"
},
"windows": {
"command": "${cwd}\\publish.bat"
},
"problemMatcher": [],
"group": "build"
"problemMatcher": "$msCompile"
}
]
}

View File

@@ -139,7 +139,7 @@ namespace ChrisKaczor.HomeMonitor.Weather.SerialReader
var ports = SerialPort.GetPortNames();
var port = ports.FirstOrDefault(p => p.StartsWith(portPrefix));
var port = Array.Find(ports, p => p.StartsWith(portPrefix));
if (port != null)
{

View File

@@ -2,7 +2,9 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<CodeAnalysisRuleSet>../../ChrisKaczor.ruleset</CodeAnalysisRuleSet>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
</PropertyGroup>
<PropertyGroup>

View File

@@ -39,7 +39,7 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
var databaseExists = (bool?)command.ExecuteScalar();
// Create database if needed
if (!databaseExists.GetValueOrDefault(false))
if (!(databaseExists ?? false))
{
command.CommandText = $"CREATE DATABASE {_configuration["Weather:Database:Name"]}";
command.ExecuteNonQuery();
@@ -86,7 +86,7 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetRecentReading.sql");
return await connection.QueryFirstOrDefaultAsync<WeatherReading>(query);
return await connection.QueryFirstOrDefaultAsync<WeatherReading>(query).ConfigureAwait(false);
}
public async Task<IEnumerable<WeatherReading>> GetReadingHistory(DateTimeOffset start, DateTimeOffset end)

View File

@@ -10,7 +10,7 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Models
[PublicAPI]
public class WeatherUpdate : WeatherMessage
{
private Database _database;
private readonly Database _database;
public decimal? WindChill { get; set; }

View File

@@ -13,10 +13,7 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service
private static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddEnvironmentVariables();
}).UseStartup<Startup>();
return WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration((_, config) => config.AddEnvironmentVariables()).UseStartup<Startup>();
}
}
}

View File

@@ -9,18 +9,14 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service
public static string GetString(string resourceName)
{
var assembly = Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream == null)
{
throw new Exception($"Resource {resourceName} not found in {assembly.FullName}. Valid resources are: {string.Join(", ", assembly.GetManifestResourceNames())}.");
}
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
using var stream = assembly.GetManifestResourceStream(resourceName);
if (stream == null)
throw new Exception($"Resource {resourceName} not found in {assembly.FullName}. Valid resources are: {string.Join(", ", assembly.GetManifestResourceNames())}.");
using var reader = new StreamReader(stream);
return reader.ReadToEnd();
}
}
}

View File

@@ -1,10 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<AssemblyName>ChrisKaczor.HomeMonitor.Weather.Service</AssemblyName>
<RootNamespace>ChrisKaczor.HomeMonitor.Weather.Service</RootNamespace>
<CodeAnalysisRuleSet>../../ChrisKaczor.ruleset</CodeAnalysisRuleSet>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
</PropertyGroup>
<ItemGroup>

View File

@@ -17,10 +17,7 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service
services.AddHostedService<MessageHandler>();
services.Configure<GzipCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Optimal;
});
services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Optimal);
services.AddResponseCompression(options =>
{
@@ -47,10 +44,7 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service
applicationBuilder.UseRouting();
applicationBuilder.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
});
applicationBuilder.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute());
}
}
}