Skip to content

Commit a399fde

Browse files
authored
Merge pull request #5 from drwatson1/rel-2.2
Rel 2.2
2 parents 606a406 + b5b8f47 commit a399fde

File tree

12 files changed

+78
-48
lines changed

12 files changed

+78
-48
lines changed

ProjectTemplates/How to create new template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ After making any changes do:
55
1. Select Release configuration for a solution
66
1. Select ReferenceProject in "Solution Explorer" and click "Project/Export Template..." menu item from the VS main menu
77
1. In the appeared dialog box select "Project template" option and "ReferenceProject" in the combobox below and click Next
8-
1. Set the value `ASP.Net Core RESTful Service` as a template name and the `Project template to create production-ready RESTful service based on ASP.Net Core v2.X. It contains preconfigured DI-container, logging, CORS, some boilerplate code and other features` as a description
8+
1. Set the value `ASP.Net Core RESTful Service` as a template name and theProject template to create production-ready RESTful service based on ASP.Net Core v3.1. It contains preconfigured DI-container, logging, CORS, some boilerplate code and other features `` as a description
99
1. Don't foget to replace .Net Core version in the description above to the appropriate one.
1010
1. Clear checkbox "Automatically import the template into Visual Studio" if you don't want immediately import it and click Finish button
1111
1. Extract all files from the created zip-archive to any folder as you want. Typically, the file can be found in `C:\Users\<YOU>\Documents\Visual Studio 2019\My Exported Templates` folder
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Contrib.Extensions.Configuration;
3+
4+
namespace ReferenceProject.Configuration
5+
{
6+
public static class ApplicationSettings
7+
{
8+
public static void AddSettings(this IServiceCollection services)
9+
{
10+
// Uses AutoBind and SubstituteVariables from https://github.com/drwatson1/configuration-extensions project
11+
services.AddOptions<Settings.Products>()
12+
.AutoBind()
13+
.SubstituteVariables();
14+
}
15+
}
16+
}

ProjectTemplates/ReferenceProject/Configuration/DependenciesConfig.cs renamed to ProjectTemplates/ReferenceProject/Configuration/SwaggerConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace ReferenceProject
99
{
10-
public static class DependenciesConfig
10+
public static class SwaggerConfig
1111
{
1212
/// <summary>
1313
/// Add Swagger middleware

ProjectTemplates/ReferenceProject/Controllers/ProductsController.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.AspNetCore.Http;
33
using Microsoft.AspNetCore.Mvc;
44
using Microsoft.Extensions.Logging;
5+
using Microsoft.Extensions.Options;
56
using ReferenceProject.Repo;
67
using System;
78
using System.Collections.Generic;
@@ -14,15 +15,17 @@ namespace ReferenceProject.Controllers
1415
[Produces("application/json")]
1516
public class ProductsController: ControllerBase
1617
{
17-
Repo.IProductsRepo ProductsRepo { get; }
18+
IProductsRepo ProductsRepo { get; }
19+
IOptionsSnapshot<Settings.Products> Settings { get; }
1820
IMapper Mapper { get; }
1921
ILogger Logger { get; }
2022

21-
public ProductsController(IProductsRepo productsRepo, IMapper mapper, ILogger<ProductsController> logger)
23+
public ProductsController(IProductsRepo productsRepo, IOptionsSnapshot<Settings.Products> options, IMapper mapper, ILogger<ProductsController> logger)
2224
{
2325
Logger = logger ?? throw new ArgumentNullException(nameof(logger));
2426
Mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
2527
ProductsRepo = productsRepo ?? throw new ArgumentNullException(nameof(productsRepo));
28+
Settings = options ?? throw new ArgumentNullException(nameof(options));
2629
}
2730

2831
/// <summary>
@@ -111,5 +114,17 @@ public IActionResult ThrowAnException()
111114
{
112115
throw new Exception("Example exception");
113116
}
117+
118+
/// <summary>
119+
/// Demonstrate how to use application settings
120+
/// </summary>
121+
/// <returns>Application settings</returns>
122+
/// <remarks>Don't do this in production! You can unintentionally unclose sensitive information</remarks>
123+
[HttpGet("Settings")]
124+
[ProducesResponseType(StatusCodes.Status200OK)]
125+
public Settings.Products GetSettings()
126+
{
127+
return Settings.Value;
128+
}
114129
}
115130
}

ProjectTemplates/ReferenceProject/ReferenceProject.csproj

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,20 @@
3030
</ItemGroup>
3131

3232
<ItemGroup>
33-
<PackageReference Include="Autofac.Configuration" Version="4.1.0" />
34-
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="5.0.1" />
35-
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
33+
<PackageReference Include="Autofac.Configuration" Version="5.1.0" />
34+
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="6.0.0" />
35+
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.0.1" />
36+
<PackageReference Include="Contrib.Extensions.Configuration.AutoBind" Version="1.0.0" />
37+
<PackageReference Include="Contrib.Extensions.Configuration.VariablesSubstitution" Version="1.1.0" />
3638
<PackageReference Include="DotNetEnv" Version="1.4.0" />
3739
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
38-
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
40+
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
41+
<PackageReference Include="Serilog.Filters.Expressions" Version="2.1.0" />
3942
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
4043
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
4144
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
42-
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0" />
43-
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0" />
45+
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.5.1" />
46+
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.5.1" />
4447
<PackageReference Include="System.Net.Http" Version="4.3.4" />
4548
</ItemGroup>
4649

ProjectTemplates/ReferenceProject/Settings.cs

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace ReferenceProject.Settings
2+
{
3+
public class Products
4+
{
5+
public string TempFolder { get; set; }
6+
public string BackendServiceUrl { get; set; }
7+
}
8+
}

ProjectTemplates/ReferenceProject/Startup.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Autofac;
22
using Autofac.Configuration;
3+
using Autofac.Core;
34
using AutoMapper;
45
using Microsoft.AspNetCore.Builder;
56
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
@@ -15,6 +16,7 @@
1516
using Newtonsoft.Json;
1617
using Newtonsoft.Json.Converters;
1718
using Newtonsoft.Json.Serialization;
19+
using ReferenceProject.Configuration;
1820
using ReferenceProject.Filters;
1921
using ReferenceProject.Modules;
2022
using System.IO;
@@ -37,10 +39,7 @@ public Startup(IConfiguration configuration, IHostEnvironment env)
3739

3840
// https://github.com/drwatson1/AspNet-Core-REST-Service/wiki#using-environment-variables-in-configuration-options
3941
var envPath = Path.Combine(env.ContentRootPath, ".env");
40-
if (File.Exists(envPath))
41-
{
42-
DotNetEnv.Env.Load(envPath);
43-
}
42+
DotNetEnv.Env.Load(envPath);
4443

4544
// See: https://github.com/drwatson1/AspNet-Core-REST-Service/wiki#content-formatting
4645
JsonConvert.DefaultSettings = () =>
@@ -97,6 +96,8 @@ public void ConfigureServices(IServiceCollection services)
9796
services.AddRouting();
9897
services.AddControllers();
9998
services.AddHealthChecks();
99+
100+
services.AddSettings();
100101
}
101102

102103
/// <summary>
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
{
22
"Serilog": {
33
"MinimumLevel": {
4-
"Default": "Debug",
5-
"Override": {
6-
"Microsoft": "Debug",
7-
"System": "Debug"
8-
}
4+
"Default": "Debug"
95
}
106
}
117
}
Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
{
22
"Serilog": {
33
"MinimumLevel": {
4-
"Default": "Warning",
4+
"Default": "Information",
55
"Override": {
66
"Microsoft": "Warning",
7+
"Microsoft.Hosting": "Information",
78
"System": "Warning"
89
}
910
},
11+
"Filter": [],
1012
"WriteTo": [
1113
{
1214
"Name": "File",
1315
"Args": {
1416
"path": "%AppData%/Logs/ReferenceProject.log",
15-
"rollingInterval": "Day",
16-
"buffered": false
17+
"rollOnFileSizeLimit": true,
18+
"fileSizeLimitBytes": 10485760,
19+
"retainedFileCountLimit": 10,
20+
"buffered": false,
21+
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u3}] {Message:lj}{NewLine}{Exception}"
1722
}
1823
},
1924
{
20-
"Name": "Console"
25+
"Name": "Console",
26+
"Args": {
27+
"outputTemplate": "[{Timestamp:HH:mm:ss}] [{Level:u3}] {Message:lj}{NewLine}{Exception}"
28+
}
2129
}
30+
],
31+
"Enrich": [
32+
"FromLogContext"
2233
]
2334
},
35+
"Products": {
36+
"TempFolder": "%TEMP%",
37+
"BackendServiceUrl": "http://%gateway%/backend"
38+
},
2439
"AllowedHosts": "*",
25-
"CoolServiceEndpoint": "http://%ENDPOINT_HOST%/cool",
26-
"AnotherServiceEndpoint": "http://%ENDPOINT_HOST%/another",
2740
"Urls": "http://localhost:5000"
2841
}

0 commit comments

Comments
 (0)