Skip to content

Commit

Permalink
Merge pull request #199 from ardalis/kylemcmaster/net-8-updates
Browse files Browse the repository at this point in the history
Update to .NET 8 and Use C#12 features
  • Loading branch information
ardalis authored Nov 20, 2023
2 parents 7ff5f75 + 7aeadd7 commit dc99232
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 88 deletions.
1 change: 1 addition & 0 deletions CleanArchitecture.WorkerService.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sln", "sln", "{2DD960C4-7461-4547-9983-3E3034D2B0C6}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
Directory.Build.props = Directory.Build.props
Directory.Packages.props = Directory.Packages.props
ef-migrations.txt = ef-migrations.txt
global.json = global.json
Expand Down
7 changes: 7 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project>

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

</Project>
20 changes: 10 additions & 10 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Ardalis.ApiEndpoints" Version="4.0.1" />
<PackageVersion Include="Ardalis.GuardClauses" Version="4.1.1" />
<PackageVersion Include="Ardalis.GuardClauses" Version="4.2.0" />
<PackageVersion Include="Ardalis.HttpClientTestExtensions" Version="3.0.0" />
<PackageVersion Include="Ardalis.ListStartupServices" Version="1.1.4" />
<PackageVersion Include="Ardalis.Result" Version="4.1.0" />
Expand All @@ -21,18 +21,18 @@
<PackageVersion Include="FastEndpoints.Swagger" Version="5.2.1" />
<PackageVersion Include="FastEndpoints.Swagger.Swashbuckle" Version="2.0.1" />
<PackageVersion Include="MediatR" Version="11.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.11" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.11" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.11" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageVersion Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.0" />
<PackageVersion Include="Moq" Version="4.18.4" />
Expand All @@ -43,7 +43,7 @@
<PackageVersion Include="SQLite" Version="3.13.0" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageVersion Include="Swashbuckle.AspNetCore.Annotations" Version="6.4.0" />
<PackageVersion Include="xunit" Version="2.5.1" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.1" />
<PackageVersion Include="xunit" Version="2.6.1" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>
</Project>
4 changes: 0 additions & 4 deletions src/CleanArchitecture.Core/CleanArchitecture.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ardalis.GuardClauses" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
Expand Down
28 changes: 5 additions & 23 deletions src/CleanArchitecture.Core/Services/EntryPointService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,12 @@ namespace CleanArchitecture.Core.Services;
/// <summary>
/// An example service that performs business logic
/// </summary>
public class EntryPointService : IEntryPointService
public class EntryPointService(ILoggerAdapter<EntryPointService> _logger,
EntryPointSettings _settings,
IQueueReceiver _queueReceiver,
IServiceLocator _serviceScopeFactoryLocator,
IUrlStatusChecker _urlStatusChecker) : IEntryPointService
{
private readonly ILoggerAdapter<EntryPointService> _logger;
private readonly EntryPointSettings _settings;
private readonly IQueueReceiver _queueReceiver;
private readonly IQueueSender _queueSender;
private readonly IServiceLocator _serviceScopeFactoryLocator;
private readonly IUrlStatusChecker _urlStatusChecker;

public EntryPointService(ILoggerAdapter<EntryPointService> logger,
EntryPointSettings settings,
IQueueReceiver queueReceiver,
IQueueSender queueSender,
IServiceLocator serviceScopeFactoryLocator,
IUrlStatusChecker urlStatusChecker)
{
_logger = logger;
_settings = settings;
_queueReceiver = queueReceiver;
_queueSender = queueSender;
_serviceScopeFactoryLocator = serviceScopeFactoryLocator;
_urlStatusChecker = urlStatusChecker;
}

public async Task ExecuteAsync()
{
_logger.LogInformation("{service} running at: {time}", nameof(EntryPointService), DateTimeOffset.Now);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@ namespace CleanArchitecture.Core.Services;
/// A wrapper around ServiceScopeFactory to make it easier to fake out with MOQ.
/// </summary>
/// <see cref="https://stackoverflow.com/a/53509491/54288"/>
public sealed class ServiceScopeFactoryLocator : IServiceLocator
public sealed class ServiceScopeFactoryLocator(IServiceScopeFactory _factory) : IServiceLocator
{
private readonly IServiceScopeFactory _factory;
private IServiceScope _scope;

public ServiceScopeFactoryLocator(IServiceScopeFactory factory)
{
_factory = factory;
}

public T Get<T>()
{
CreateScope();
Expand Down
9 changes: 1 addition & 8 deletions src/CleanArchitecture.Core/Services/UrlStatusChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,8 @@ namespace CleanArchitecture.Core.Services;
/// <summary>
/// A simple service that fetches a URL and returns a UrlStatusHistory instance with the result
/// </summary>
public class UrlStatusChecker : IUrlStatusChecker
public class UrlStatusChecker(IHttpService _httpService) : IUrlStatusChecker
{
private readonly IHttpService _httpService;

public UrlStatusChecker(IHttpService httpService)
{
_httpService = httpService;
}

public async Task<UrlStatusHistory> CheckUrlAsync(string url, string requestId)
{
Guard.Against.NullOrWhiteSpace(url, nameof(url));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ardalis.GuardClauses" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
Expand Down
8 changes: 3 additions & 5 deletions src/CleanArchitecture.Infrastructure/Http/HttpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ public class HttpService : IHttpService
{
public async Task<int> GetUrlResponseStatusCodeAsync(string url)
{
using (var client = new HttpClient())
{
var result = await client.GetAsync(url);
using var client = new HttpClient();
var result = await client.GetAsync(url);

return (int)result.StatusCode;
}
return (int)result.StatusCode;
}
}
9 changes: 1 addition & 8 deletions src/CleanArchitecture.Infrastructure/LoggerAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,8 @@ namespace CleanArchitecture.Infrastructure;
/// An ILoggerAdapter implementation that uses Microsoft.Extensions.Logging
/// </summary>
/// <typeparam name="T"></typeparam>
public class LoggerAdapter<T> : ILoggerAdapter<T>
public class LoggerAdapter<T>(ILogger<T> _logger) : ILoggerAdapter<T>
{
private readonly ILogger<T> _logger;

public LoggerAdapter(ILogger<T> logger)
{
_logger = logger;
}

public void LogError(Exception ex, string message, params object[] args)
{
_logger.LogError(ex, message, args);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<UserSecretsId>dotnet-CleanArchitecture.Worker-AF0DD401-BB78-4FCC-8186-3E275E001A54</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..</DockerfileContext>
Expand Down
4 changes: 2 additions & 2 deletions src/CleanArchitecture.Worker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/runtime:6.0 AS build
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS build
WORKDIR /src
COPY ["src/CleanArchitecture.Worker/CleanArchitecture.Worker.csproj", "src/CleanArchitecture.Worker/"]
RUN dotnet restore "src/CleanArchitecture.Worker/CleanArchitecture.Worker.csproj"
Expand Down
17 changes: 3 additions & 14 deletions src/CleanArchitecture.Worker/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,10 @@ namespace CleanArchitecture.Worker;
/// It should not contain any business logic but should call an entrypoint service that
/// execute once per time period.
/// </summary>
public class Worker : BackgroundService
public class Worker(ILoggerAdapter<Worker> _logger,
IEntryPointService _entryPointService,
WorkerSettings _settings) : BackgroundService
{
private readonly ILoggerAdapter<Worker> _logger;
private readonly IEntryPointService _entryPointService;
private readonly WorkerSettings _settings;

public Worker(ILoggerAdapter<Worker> logger,
IEntryPointService entryPointService,
WorkerSettings settings)
{
_logger = logger;
_entryPointService = entryPointService;
_settings = settings;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("CleanArchitecture.Worker service starting at: {time}", DateTimeOffset.Now);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down

0 comments on commit dc99232

Please sign in to comment.