diff --git a/ProjectTemplates/How to create new template.md b/ProjectTemplates/How to create new template.md index 253d8da..0a0d80d 100644 --- a/ProjectTemplates/How to create new template.md +++ b/ProjectTemplates/How to create new template.md @@ -5,7 +5,7 @@ After making any changes do: 1. Select Release configuration for a solution 1. Select ReferenceProject in "Solution Explorer" and click "Project/Export Template..." menu item from the VS main menu 1. In the appeared dialog box select "Project template" option and "ReferenceProject" in the combobox below and click Next -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 +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 1. Don't foget to replace .Net Core version in the description above to the appropriate one. 1. Clear checkbox "Automatically import the template into Visual Studio" if you don't want immediately import it and click Finish button 1. Extract all files from the created zip-archive to any folder as you want. Typically, the file can be found in `C:\Users\\Documents\Visual Studio 2019\My Exported Templates` folder diff --git a/ProjectTemplates/ReferenceProject/Configuration/ApplicationSettings.cs b/ProjectTemplates/ReferenceProject/Configuration/ApplicationSettings.cs new file mode 100644 index 0000000..e7f3f57 --- /dev/null +++ b/ProjectTemplates/ReferenceProject/Configuration/ApplicationSettings.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.DependencyInjection; +using Contrib.Extensions.Configuration; + +namespace ReferenceProject.Configuration +{ + public static class ApplicationSettings + { + public static void AddSettings(this IServiceCollection services) + { + // Uses AutoBind and SubstituteVariables from https://github.com/drwatson1/configuration-extensions project + services.AddOptions() + .AutoBind() + .SubstituteVariables(); + } + } +} diff --git a/ProjectTemplates/ReferenceProject/Configuration/DependenciesConfig.cs b/ProjectTemplates/ReferenceProject/Configuration/SwaggerConfig.cs similarity index 96% rename from ProjectTemplates/ReferenceProject/Configuration/DependenciesConfig.cs rename to ProjectTemplates/ReferenceProject/Configuration/SwaggerConfig.cs index 24d46ec..c204999 100644 --- a/ProjectTemplates/ReferenceProject/Configuration/DependenciesConfig.cs +++ b/ProjectTemplates/ReferenceProject/Configuration/SwaggerConfig.cs @@ -7,7 +7,7 @@ namespace ReferenceProject { - public static class DependenciesConfig + public static class SwaggerConfig { /// /// Add Swagger middleware diff --git a/ProjectTemplates/ReferenceProject/Controllers/ProductsController.cs b/ProjectTemplates/ReferenceProject/Controllers/ProductsController.cs index fe5d3f0..87eb138 100644 --- a/ProjectTemplates/ReferenceProject/Controllers/ProductsController.cs +++ b/ProjectTemplates/ReferenceProject/Controllers/ProductsController.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using ReferenceProject.Repo; using System; using System.Collections.Generic; @@ -14,15 +15,17 @@ namespace ReferenceProject.Controllers [Produces("application/json")] public class ProductsController: ControllerBase { - Repo.IProductsRepo ProductsRepo { get; } + IProductsRepo ProductsRepo { get; } + IOptionsSnapshot Settings { get; } IMapper Mapper { get; } ILogger Logger { get; } - public ProductsController(IProductsRepo productsRepo, IMapper mapper, ILogger logger) + public ProductsController(IProductsRepo productsRepo, IOptionsSnapshot options, IMapper mapper, ILogger logger) { Logger = logger ?? throw new ArgumentNullException(nameof(logger)); Mapper = mapper ?? throw new ArgumentNullException(nameof(mapper)); ProductsRepo = productsRepo ?? throw new ArgumentNullException(nameof(productsRepo)); + Settings = options ?? throw new ArgumentNullException(nameof(options)); } /// @@ -111,5 +114,17 @@ public IActionResult ThrowAnException() { throw new Exception("Example exception"); } + + /// + /// Demonstrate how to use application settings + /// + /// Application settings + /// Don't do this in production! You can unintentionally unclose sensitive information + [HttpGet("Settings")] + [ProducesResponseType(StatusCodes.Status200OK)] + public Settings.Products GetSettings() + { + return Settings.Value; + } } } diff --git a/ProjectTemplates/ReferenceProject/ReferenceProject.csproj b/ProjectTemplates/ReferenceProject/ReferenceProject.csproj index c154253..2dd4d8e 100644 --- a/ProjectTemplates/ReferenceProject/ReferenceProject.csproj +++ b/ProjectTemplates/ReferenceProject/ReferenceProject.csproj @@ -30,17 +30,20 @@ - - - + + + + + - + + - - + + diff --git a/ProjectTemplates/ReferenceProject/Settings.cs b/ProjectTemplates/ReferenceProject/Settings.cs deleted file mode 100644 index f762293..0000000 --- a/ProjectTemplates/ReferenceProject/Settings.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Microsoft.Extensions.Configuration; -using System; - -namespace ReferenceProject -{ - /// - /// Use this class to get an access to options - /// - public static class Settings - { - public static string GetConnectionString(string name) => Environment.ExpandEnvironmentVariables(Startup.Configuration.GetConnectionString(name)); - public static string Get(string name) => Environment.ExpandEnvironmentVariables(Startup.Configuration[name]); - public static string TrimUrl(string url) => url.Trim(' ', '/'); - public static string JoinPath(this string src, string path) => $"{TrimUrl(src)}/{TrimUrl(path)}"; - - public static class Services - { - public static string CoolService { get; } = TrimUrl(Get("CoolServiceEndpoint")); // %ENDPOINT_HOST% will be substituted with the value from an environment variable - public static string AnotherService { get; } = TrimUrl(Get("AnotherServiceEndpoint")); - } - } -} diff --git a/ProjectTemplates/ReferenceProject/Settings/Products.cs b/ProjectTemplates/ReferenceProject/Settings/Products.cs new file mode 100644 index 0000000..72b77bd --- /dev/null +++ b/ProjectTemplates/ReferenceProject/Settings/Products.cs @@ -0,0 +1,8 @@ +namespace ReferenceProject.Settings +{ + public class Products + { + public string TempFolder { get; set; } + public string BackendServiceUrl { get; set; } + } +} diff --git a/ProjectTemplates/ReferenceProject/Startup.cs b/ProjectTemplates/ReferenceProject/Startup.cs index fdfcbf6..ae4c9f9 100644 --- a/ProjectTemplates/ReferenceProject/Startup.cs +++ b/ProjectTemplates/ReferenceProject/Startup.cs @@ -1,5 +1,6 @@ using Autofac; using Autofac.Configuration; +using Autofac.Core; using AutoMapper; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics.HealthChecks; @@ -15,6 +16,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Serialization; +using ReferenceProject.Configuration; using ReferenceProject.Filters; using ReferenceProject.Modules; using System.IO; @@ -37,10 +39,7 @@ public Startup(IConfiguration configuration, IHostEnvironment env) // https://github.com/drwatson1/AspNet-Core-REST-Service/wiki#using-environment-variables-in-configuration-options var envPath = Path.Combine(env.ContentRootPath, ".env"); - if (File.Exists(envPath)) - { - DotNetEnv.Env.Load(envPath); - } + DotNetEnv.Env.Load(envPath); // See: https://github.com/drwatson1/AspNet-Core-REST-Service/wiki#content-formatting JsonConvert.DefaultSettings = () => @@ -97,6 +96,8 @@ public void ConfigureServices(IServiceCollection services) services.AddRouting(); services.AddControllers(); services.AddHealthChecks(); + + services.AddSettings(); } /// diff --git a/ProjectTemplates/ReferenceProject/appsettings.Development.json b/ProjectTemplates/ReferenceProject/appsettings.Development.json index e996f3c..07332dd 100644 --- a/ProjectTemplates/ReferenceProject/appsettings.Development.json +++ b/ProjectTemplates/ReferenceProject/appsettings.Development.json @@ -1,11 +1,7 @@ { "Serilog": { "MinimumLevel": { - "Default": "Debug", - "Override": { - "Microsoft": "Debug", - "System": "Debug" - } + "Default": "Debug" } } } diff --git a/ProjectTemplates/ReferenceProject/appsettings.json b/ProjectTemplates/ReferenceProject/appsettings.json index 7c994e8..86c63f2 100644 --- a/ProjectTemplates/ReferenceProject/appsettings.json +++ b/ProjectTemplates/ReferenceProject/appsettings.json @@ -1,28 +1,41 @@ { "Serilog": { "MinimumLevel": { - "Default": "Warning", + "Default": "Information", "Override": { "Microsoft": "Warning", + "Microsoft.Hosting": "Information", "System": "Warning" } }, + "Filter": [], "WriteTo": [ { "Name": "File", "Args": { "path": "%AppData%/Logs/ReferenceProject.log", - "rollingInterval": "Day", - "buffered": false + "rollOnFileSizeLimit": true, + "fileSizeLimitBytes": 10485760, + "retainedFileCountLimit": 10, + "buffered": false, + "outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz}] [{Level:u3}] {Message:lj}{NewLine}{Exception}" } }, { - "Name": "Console" + "Name": "Console", + "Args": { + "outputTemplate": "[{Timestamp:HH:mm:ss}] [{Level:u3}] {Message:lj}{NewLine}{Exception}" + } } + ], + "Enrich": [ + "FromLogContext" ] }, + "Products": { + "TempFolder": "%TEMP%", + "BackendServiceUrl": "http://%gateway%/backend" + }, "AllowedHosts": "*", - "CoolServiceEndpoint": "http://%ENDPOINT_HOST%/cool", - "AnotherServiceEndpoint": "http://%ENDPOINT_HOST%/another", "Urls": "http://localhost:5000" } diff --git a/ProjectTemplates/ReferenceProjectVSIX/ProjectTemplates/CSharp/.NET Core/ASP.Net Core RESTful Service.zip b/ProjectTemplates/ReferenceProjectVSIX/ProjectTemplates/CSharp/.NET Core/ASP.Net Core RESTful Service.zip index 801143e..099b61f 100644 Binary files a/ProjectTemplates/ReferenceProjectVSIX/ProjectTemplates/CSharp/.NET Core/ASP.Net Core RESTful Service.zip and b/ProjectTemplates/ReferenceProjectVSIX/ProjectTemplates/CSharp/.NET Core/ASP.Net Core RESTful Service.zip differ diff --git a/ProjectTemplates/ReferenceProjectVSIX/source.extension.vsixmanifest b/ProjectTemplates/ReferenceProjectVSIX/source.extension.vsixmanifest index f40a497..3e55405 100644 --- a/ProjectTemplates/ReferenceProjectVSIX/source.extension.vsixmanifest +++ b/ProjectTemplates/ReferenceProjectVSIX/source.extension.vsixmanifest @@ -1,7 +1,7 @@ - + ASP.Net Core 3.1 RESTful Service Template Project template to create production-ready RESTful service based on ASP.Net Core 3.1. It contains preconfigured DI-container, logging, CORS, some boilerplate code and other features https://github.com/drwatson1/AspNet-Core-REST-Service