From 6f70dc1a3dcba9b72ca5f51631df2f0c3699ebd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Mon, 22 May 2023 00:29:04 +0200 Subject: [PATCH] Add project files. --- StandardConsole/Program.cs | 30 +++++++++++++++++++ .../Properties/launchSettings.json | 18 +++++++++++ .../Repositories/ISampleRepository.cs | 7 +++++ .../Repositories/SampleRepository.cs | 28 +++++++++++++++++ StandardConsole/StandardConsole.csproj | 27 +++++++++++++++++ StandardConsole/Worker.cs | 29 ++++++++++++++++++ StandardConsole/appsettings.Development.json | 5 ++++ StandardConsole/appsettings.Production.json | 5 ++++ StandardConsole/appsettings.json | 5 ++++ StandardConsoleApp.sln | 25 ++++++++++++++++ 10 files changed, 179 insertions(+) create mode 100644 StandardConsole/Program.cs create mode 100644 StandardConsole/Properties/launchSettings.json create mode 100644 StandardConsole/Repositories/ISampleRepository.cs create mode 100644 StandardConsole/Repositories/SampleRepository.cs create mode 100644 StandardConsole/StandardConsole.csproj create mode 100644 StandardConsole/Worker.cs create mode 100644 StandardConsole/appsettings.Development.json create mode 100644 StandardConsole/appsettings.Production.json create mode 100644 StandardConsole/appsettings.json create mode 100644 StandardConsoleApp.sln diff --git a/StandardConsole/Program.cs b/StandardConsole/Program.cs new file mode 100644 index 0000000..e1803d3 --- /dev/null +++ b/StandardConsole/Program.cs @@ -0,0 +1,30 @@ +// See https://aka.ms/new-console-template for more information +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using StandardConsole.Repositories; +using System.Security.Cryptography.X509Certificates; + + + +IHost host = CreateHostBuilder(args).Build(); +var worker = ActivatorUtilities.CreateInstance((IServiceProvider)host.Services); +worker.Run(); + +static IHostBuilder CreateHostBuilder(string[] args) +{ + return Host.CreateDefaultBuilder(args) + .ConfigureAppConfiguration((context, configuration) => + { + configuration.Sources.Clear(); + var environmentName = Environment.GetEnvironmentVariable("DOTNETCORE_ENVIRONMENT"); + configuration.AddJsonFile("appsettings.json",optional: true, reloadOnChange: true); + configuration.AddJsonFile($"appsettings.{environmentName}.json", optional: true, reloadOnChange: true); + configuration.AddCommandLine(args); + + }) + .ConfigureServices((context, services) => + { + services.AddScoped(); + }); +} \ No newline at end of file diff --git a/StandardConsole/Properties/launchSettings.json b/StandardConsole/Properties/launchSettings.json new file mode 100644 index 0000000..c676432 --- /dev/null +++ b/StandardConsole/Properties/launchSettings.json @@ -0,0 +1,18 @@ +{ + "profiles": { + "StandardConsole": { + "commandName": "Project", + "commandLineArgs": "key1=val1 key2=val2", + "environmentVariables": { + "DOTNETCORE_ENVIRONMENT": "Development" + } + }, + "ProductVersion": { + "commandName": "Project", + "commandLineArgs": "key1=val1 key2=val2", + "environmentVariables": { + "DOTNETCORE_ENVIRONMENT": "Production" + } + } + } +} \ No newline at end of file diff --git a/StandardConsole/Repositories/ISampleRepository.cs b/StandardConsole/Repositories/ISampleRepository.cs new file mode 100644 index 0000000..e8d4cb1 --- /dev/null +++ b/StandardConsole/Repositories/ISampleRepository.cs @@ -0,0 +1,7 @@ +namespace StandardConsole.Repositories +{ + public interface ISampleRepository + { + void DoSomething(); + } +} \ No newline at end of file diff --git a/StandardConsole/Repositories/SampleRepository.cs b/StandardConsole/Repositories/SampleRepository.cs new file mode 100644 index 0000000..b3a06ba --- /dev/null +++ b/StandardConsole/Repositories/SampleRepository.cs @@ -0,0 +1,28 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StandardConsole.Repositories; + +public class SampleRepository : ISampleRepository +{ + private readonly IConfiguration _configuration; + private readonly ILogger _logger; + + public void DoSomething() + { + _logger.LogInformation($"{nameof(SampleRepository)}.Dosomething - just did something"); + var connString = _configuration.GetConnectionString("DefaultConnection"); + _logger.LogInformation($"The Connection string from Sample Repository: {connString}"); + } + public SampleRepository(IConfiguration configuration, ILogger logger) + { + _configuration = configuration; + _logger = logger; + } + +} diff --git a/StandardConsole/StandardConsole.csproj b/StandardConsole/StandardConsole.csproj new file mode 100644 index 0000000..dd4d2b9 --- /dev/null +++ b/StandardConsole/StandardConsole.csproj @@ -0,0 +1,27 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + + + + Always + + + Always + + + Always + + + + diff --git a/StandardConsole/Worker.cs b/StandardConsole/Worker.cs new file mode 100644 index 0000000..b07a29d --- /dev/null +++ b/StandardConsole/Worker.cs @@ -0,0 +1,29 @@ +// See https://aka.ms/new-console-template for more information +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using StandardConsole.Repositories; + +public class Worker +{ + private readonly IConfiguration _configuration; + private readonly ILogger _logger; + private readonly ISampleRepository _sampleRepository; + + public Worker(IConfiguration configuration, ILogger logger, ISampleRepository sampleRepository) + { + _configuration = configuration; + _logger = logger; + _sampleRepository = sampleRepository; + } + public void Run() + { + _logger.LogInformation("Hello, world!!!"); + var connString = _configuration.GetConnectionString("DefaultConnection"); + _logger.LogInformation($"Connection string: {connString}"); + _logger.LogInformation($"key1: {_configuration["key1"]}"); + _logger.LogInformation($"key2: {_configuration["key2"]}"); + + _sampleRepository.DoSomething(); + } + +} \ No newline at end of file diff --git a/StandardConsole/appsettings.Development.json b/StandardConsole/appsettings.Development.json new file mode 100644 index 0000000..10abc64 --- /dev/null +++ b/StandardConsole/appsettings.Development.json @@ -0,0 +1,5 @@ +{ + "ConnectionStrings": { + "DefaultConnection": "Server=Devserver;Database=DevDb;User Id==MeMyselfAndI;Password=SuperSecret" + } +} diff --git a/StandardConsole/appsettings.Production.json b/StandardConsole/appsettings.Production.json new file mode 100644 index 0000000..58a25e9 --- /dev/null +++ b/StandardConsole/appsettings.Production.json @@ -0,0 +1,5 @@ +{ + "ConnectionStrings": { + "DefaultConnection": "Server=Prodserver;Database=ProdDb;User Id==MeMyselfAndI;Password=SuperSecret" + } +} diff --git a/StandardConsole/appsettings.json b/StandardConsole/appsettings.json new file mode 100644 index 0000000..5d77b7b --- /dev/null +++ b/StandardConsole/appsettings.json @@ -0,0 +1,5 @@ +{ + "ConnectionStrings": { + "DefaultConnection": "Server(local);Database=StdDb;User Id==MeMyselfAndI;Password=SuperSecret" + } +} diff --git a/StandardConsoleApp.sln b/StandardConsoleApp.sln new file mode 100644 index 0000000..c0c665c --- /dev/null +++ b/StandardConsoleApp.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.33627.172 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StandardConsole", "StandardConsole\StandardConsole.csproj", "{8C92891D-C347-4A5C-AAE4-D50F705F00A4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8C92891D-C347-4A5C-AAE4-D50F705F00A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8C92891D-C347-4A5C-AAE4-D50F705F00A4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8C92891D-C347-4A5C-AAE4-D50F705F00A4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8C92891D-C347-4A5C-AAE4-D50F705F00A4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {305DAD26-537A-41D7-9A94-9B4911FAF44A} + EndGlobalSection +EndGlobal