Skip to content

Commit a2de05c

Browse files
Aymen TROUDIAymen TROUDI
authored andcommitted
Upgrade to net 6.0
1 parent 0e97af1 commit a2de05c

12 files changed

Lines changed: 189 additions & 77 deletions

File tree

.github/workflows/ci.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: .NET Core
1+
name: .NET
22

33
on:
44
push:
@@ -13,13 +13,13 @@ jobs:
1313

1414
steps:
1515
- uses: actions/checkout@v2
16-
- name: Setup .NET Core
16+
- name: Setup .NET
1717
uses: actions/setup-dotnet@v1
1818
with:
19-
dotnet-version: 3.1.301
20-
- name: Install dependencies
19+
dotnet-version: 6.0.x
20+
- name: Restore dependencies
2121
run: dotnet restore
2222
- name: Build
23-
run: dotnet build --configuration Release --no-restore
23+
run: dotnet build --no-restore
2424
- name: Test
25-
run: dotnet test --no-restore --verbosity normal
25+
run: dotnet test --no-build --verbosity normal

App/App - Backup.csproj

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<Project Sdk="Microsoft.NET.Sdk;Microsoft.NET.Sdk.Publish">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.33" />
8+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="4.0.1" />
9+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.2.0" />
10+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.0.1" />
11+
<PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.33" />
12+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
13+
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="6.0.0" />
14+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" />
15+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
16+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
17+
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.8" />
18+
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.8" />
19+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.8" />
20+
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.8" />
21+
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.8" />
22+
</ItemGroup>
23+
<ItemGroup>
24+
<ProjectReference Include="..\Lib\Lib.csproj" />
25+
</ItemGroup>
26+
<ItemGroup>
27+
<None Update="appsettings.dev.json">
28+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
29+
</None>
30+
<None Update="appsettings.json">
31+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
32+
</None>
33+
</ItemGroup>
34+
</Project>

App/App.csproj

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
<Project Sdk="Microsoft.NET.Sdk;Microsoft.NET.Sdk.Publish">
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<UserSecretsId>WebJobDemo-UserSecrets</UserSecretsId>
56
</PropertyGroup>
67
<ItemGroup>
7-
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.18" />
8-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.6" />
9-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.0.2" />
10-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="4.0.2" />
11-
<PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.18" />
12-
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.8" />
13-
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="3.1.8" />
14-
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.8" />
15-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.8" />
16-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.8" />
17-
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.8" />
18-
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.8" />
19-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.8" />
20-
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.8" />
21-
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.8" />
8+
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.33" />
9+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="4.0.1" />
10+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.2.0" />
11+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.0.1" />
12+
<PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.33" />
13+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
14+
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="6.0.0" />
15+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" />
16+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
17+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
18+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
19+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
20+
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
21+
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
22+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
23+
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
24+
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="6.0.0" />
2225
</ItemGroup>
2326
<ItemGroup>
2427
<ProjectReference Include="..\Lib\Lib.csproj" />
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using Microsoft.Extensions.Configuration;
3+
4+
namespace App.Extensions
5+
{
6+
public static class ConfigurationBuilderExtensions
7+
{
8+
public static void AddJsonFile(this IConfigurationBuilder configurationBuilder)
9+
{
10+
configurationBuilder.SetBasePath(PathExtensions.GetDirectoryPath());
11+
var environment = Environment.GetEnvironmentVariable("ENVIRONMENT");
12+
configurationBuilder.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
13+
configurationBuilder.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true);
14+
}
15+
16+
public static void AddUserSecrets(this IConfigurationBuilder configurationBuilder)
17+
{
18+
configurationBuilder.AddUserSecrets(typeof(Program).Assembly);
19+
}
20+
}
21+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.IO;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Logging;
5+
using Microsoft.Extensions.Logging.Console;
6+
7+
namespace App.Extensions;
8+
9+
public static class LoggingBuilderExtensions
10+
{
11+
public static void AddLogging(this ILoggingBuilder loggingBuilder, IConfiguration configuration)
12+
{
13+
loggingBuilder.ClearProviders();
14+
loggingBuilder.AddConsoleLogger();
15+
loggingBuilder.AddNonGenericLogger();
16+
loggingBuilder.AddConfiguration(configuration.GetSection("Logging"));
17+
var instrumentationKey = configuration["Settings:ApplicationInsights:InstrumentationKey"];
18+
loggingBuilder.AddApplicationInsightsWebJobs(options => options.InstrumentationKey = instrumentationKey);
19+
}
20+
21+
public static void AddConsoleLogger(this ILoggingBuilder loggingBuilder)
22+
{
23+
if (File.Exists(PathExtensions.GetSettingFilePath()))
24+
{
25+
loggingBuilder.AddConsole();
26+
}
27+
else
28+
{
29+
loggingBuilder.AddSimpleConsole(options =>
30+
{
31+
options.SingleLine = true;
32+
options.IncludeScopes = true;
33+
options.UseUtcTimestamp = true;
34+
options.TimestampFormat = "[HH:mm:ss:fff] ";
35+
options.ColorBehavior = LoggerColorBehavior.Enabled;
36+
});
37+
}
38+
}
39+
40+
public static void AddNonGenericLogger(this ILoggingBuilder loggingBuilder)
41+
{
42+
var categoryName = typeof(Program).Namespace;
43+
var services = loggingBuilder.Services;
44+
services.AddSingleton(serviceProvider =>
45+
{
46+
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
47+
return loggerFactory.CreateLogger(categoryName!);
48+
});
49+
}
50+
}

App/Extensions/PathExtensions.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.IO;
2+
using System.Reflection;
3+
4+
namespace App.Extensions;
5+
6+
public static class PathExtensions
7+
{
8+
public static string GetSettingFilePath() => Path.GetFullPath(Path.Combine(GetDirectoryPath(), @"appsettings.json"));
9+
10+
public static string GetDirectoryPath()
11+
{
12+
try
13+
{
14+
return Path.GetDirectoryName(GetAssemblyLocation())!;
15+
}
16+
catch
17+
{
18+
return Directory.GetCurrentDirectory();
19+
}
20+
}
21+
22+
private static string GetAssemblyLocation() => Assembly.GetExecutingAssembly().Location;
23+
}

App/Functions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,27 @@ namespace App
1111
{
1212
public class Functions
1313
{
14+
private const string FunctionName = "WebJobDemo";
1415
private const string TimeoutExpression = "00:00:30";
1516
private const string ScheduleExpression = "*/5 * * * * *";
1617

1718
private readonly IApiProxy _apiProxy;
1819

1920
public Functions(IApiProxy apiProxy)
2021
{
21-
_apiProxy = apiProxy;
22+
_apiProxy = apiProxy ?? throw new ArgumentNullException(nameof(apiProxy));
2223
}
2324

2425
[Singleton]
2526
[Timeout(TimeoutExpression)]
27+
[FunctionName(FunctionName)]
2628
public async Task RunAsync(
2729
[TimerTrigger(ScheduleExpression, RunOnStartup = true)]
2830
TimerInfo timer,
2931
CancellationToken cancellationToken,
3032
ILogger logger)
3133
{
32-
var data = await _apiProxy.GetRandomDataAsync();
34+
var data = await _apiProxy.GetRandomDataAsync(cancellationToken);
3335
var json = GetFormattedJson(data);
3436
var now = DateTime.Now;
3537
logger.LogInformation("Found result at {now}: {json}", now, json);

App/Program.cs

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System;
22
using System.Threading.Tasks;
3+
using App.Extensions;
34
using Lib.Configuration;
45
using Lib.Proxies;
56
using Microsoft.Extensions.Configuration;
67
using Microsoft.Extensions.DependencyInjection;
78
using Microsoft.Extensions.Hosting;
8-
using Microsoft.Extensions.Logging;
99
using Microsoft.Extensions.Options;
1010

1111
namespace App
@@ -14,62 +14,39 @@ public static class Program
1414
{
1515
public static async Task Main(string[] args)
1616
{
17-
var hostBuilder = new HostBuilder();
18-
hostBuilder
17+
using var host = CreateHostBuilder(args).Build();
18+
await host.RunAsync();
19+
}
20+
21+
public static IHostBuilder CreateHostBuilder(string[] args) =>
22+
Host.CreateDefaultBuilder(args)
1923
.ConfigureWebJobs(builder =>
2024
{
2125
builder.AddAzureStorageCoreServices();
22-
builder.AddAzureStorage();
2326
builder.AddTimers();
2427
})
25-
.ConfigureAppConfiguration(builder =>
28+
.ConfigureAppConfiguration((_, config) =>
2629
{
27-
builder.AddCommandLine(args);
28-
builder.AddEnvironmentVariables();
29-
var environment = Environment.GetEnvironmentVariable("ENVIRONMENT") ?? "DEV";
30-
builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
31-
builder.AddJsonFile($"appsettings.{environment}.json", optional: false, reloadOnChange: true);
30+
config.AddJsonFile();
31+
config.AddUserSecrets();
32+
config.AddEnvironmentVariables();
33+
config.AddCommandLine(args);
3234
})
33-
.ConfigureLogging((context, builder) =>
35+
.ConfigureLogging((hostingContext, loggingBuilder) =>
3436
{
35-
builder.AddNonGenericLogger();
36-
builder.AddConsole(options =>
37-
{
38-
options.DisableColors = false;
39-
options.TimestampFormat = "[HH:mm:ss:fff] ";
40-
});
41-
builder.AddConfiguration(context.Configuration.GetSection("Logging"));
42-
var instrumentationKey = context.Configuration["Settings:ApplicationInsights:InstrumentationKey"];
43-
builder.AddApplicationInsightsWebJobs(options => options.InstrumentationKey = instrumentationKey);
37+
loggingBuilder.AddLogging(hostingContext.Configuration);
4438
})
45-
.ConfigureServices((context, services) =>
39+
.ConfigureServices((hostingContext, services) =>
4640
{
47-
services.Configure<Settings>(context.Configuration.GetSection(nameof(Settings)));
41+
services.Configure<Settings>(hostingContext.Configuration.GetSection("Settings"));
4842
services.AddHttpClient<IApiProxy, ApiProxy>()
4943
.ConfigureHttpClient((provider, client) =>
5044
{
51-
var settings = provider.GetService<IOptions<Settings>>().Value;
52-
var baseAddress = settings.ExternalApi.BaseAddress;
45+
var options = provider.GetRequiredService<IOptions<Settings>>();
46+
var baseAddress = options.Value.ExternalApi.BaseAddress;
5347
client.BaseAddress = new Uri(baseAddress);
5448
client.DefaultRequestHeaders.Add("Accept", "application/json");
5549
});
5650
});
57-
58-
var host = hostBuilder.Build();
59-
using (host)
60-
{
61-
await host.RunAsync();
62-
}
63-
}
64-
65-
private static void AddNonGenericLogger(this ILoggingBuilder loggingBuilder)
66-
{
67-
var services = loggingBuilder.Services;
68-
services.AddSingleton(serviceProvider =>
69-
{
70-
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
71-
return loggerFactory.CreateLogger("ContinuousWebJobDemo");
72-
});
73-
}
7451
}
7552
}

Lib/Lib.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.8" />
8+
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
99
</ItemGroup>
1010

1111
</Project>

Lib/Proxies/ApiProxy.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Net.Http;
3+
using System.Threading;
34
using System.Threading.Tasks;
45
using Lib.Configuration;
56
using Microsoft.Extensions.Logging;
@@ -15,23 +16,23 @@ public class ApiProxy : IApiProxy
1516

1617
public ApiProxy(HttpClient client, IOptions<Settings> options, ILogger logger)
1718
{
18-
_client = client;
19-
_logger = logger;
19+
_client = client ?? throw new ArgumentNullException(nameof(client));
20+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
2021
_queryString = options.Value.ExternalApi.QueryString;
21-
_logger.LogInformation("call to ctor ApiProxy at {now}", DateTime.Now);
22+
_logger.LogInformation("Call to ApiProxy ctor at {now}", DateTime.Now);
2223
}
2324

24-
public async Task<string> GetRandomDataAsync()
25+
public async Task<string> GetRandomDataAsync(CancellationToken cancellationToken)
2526
{
2627
try
2728
{
28-
return await _client.GetStringAsync(_queryString);
29+
return await _client.GetStringAsync(_queryString, cancellationToken);
2930
}
3031
catch (Exception ex)
3132
{
32-
_logger.LogCritical("An error has occurred: {ex}", ex);
33+
_logger.LogCritical(ex, "An error has occurred: {error}", ex.Message);
3334
}
34-
35+
3536
return null;
3637
}
3738
}

0 commit comments

Comments
 (0)