Apply ILogger, IOptions, IHostedService patterns to .NET Core 3.1 example (#514)

This commit is contained in:
Crossbow78
2020-10-06 12:24:45 +02:00
committed by GitHub
parent b303f7cf89
commit 477f3b5cd3
8 changed files with 131 additions and 152 deletions

View File

@@ -5,7 +5,7 @@
--> -->
<system.webServer> <system.webServer>
<handlers> <handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" /> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers> </handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" /> <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
</system.webServer> </system.webServer>

View File

@@ -1,22 +1,28 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Hosting;
using System.Threading;
using System.Threading.Tasks;
namespace WireMock.Net.WebApplication namespace WireMock.Net.WebApplication
{ {
public class App public class App : IHostedService
{ {
private readonly IWireMockService _service; private readonly IWireMockService _service;
private readonly ILogger _logger;
public App(IWireMockService service, ILogger logger) public App(IWireMockService service)
{ {
_service = service; _service = service;
_logger = logger;
} }
public void Run() public Task StartAsync(CancellationToken cancellationToken)
{ {
_logger.LogInformation("WireMock.Net App running"); _service.Start();
_service.Run(); return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
_service.Stop();
return Task.CompletedTask;
} }
} }
} }

View File

@@ -2,6 +2,8 @@
{ {
public interface IWireMockService public interface IWireMockService
{ {
void Run(); void Start();
void Stop();
} }
} }

View File

@@ -1,9 +1,8 @@
using System; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using WireMock.Settings; using WireMock.Settings;
// using Microsoft.Extensions.Logging.Console;
namespace WireMock.Net.WebApplication namespace WireMock.Net.WebApplication
{ {
@@ -11,49 +10,21 @@ namespace WireMock.Net.WebApplication
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
// Create service collection CreateHostBuilder(args).Build().Run();
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
// Create service provider
var serviceProvider = serviceCollection.BuildServiceProvider();
// Run app
serviceProvider.GetService<App>().Run();
} }
private static void ConfigureServices(IServiceCollection serviceCollection) private static IHostBuilder CreateHostBuilder(string[] args)
=> Host.CreateDefaultBuilder(args)
.ConfigureServices((host, services) => ConfigureServices(services, host.Configuration));
private static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{ {
// Build configuration services.AddLogging(logging => logging.AddConsole().AddDebug());
var configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables() // <-- this is needed to to override settings via the Azure Portal App Settings
.Build();
// Add LoggerFactory services.AddTransient<IWireMockService, WireMockService>();
var factory = new LoggerFactory(); services.Configure<WireMockServerSettings>(configuration.GetSection("WireMockServerSettings"));
//var consoleLog = new ConsoleLoggerProvider(configuration.GetSection("Logging"));
serviceCollection.AddSingleton(factory
// .AddProvider(configuration.GetSection("Logging"))
// .AddDebug()
// .AddAzureWebAppDiagnostics()
);
serviceCollection.AddSingleton(factory.CreateLogger("WireMock.Net Logger")); services.AddHostedService<App>();
// Add access to generic IConfigurationRoot
serviceCollection.AddSingleton(configuration);
// Add access to WireMockServerSettings
var settings = configuration.GetSection("WireMockServerSettings").Get<WireMockServerSettings>();
serviceCollection.AddSingleton<WireMockServerSettings>(settings);
// Add services
serviceCollection.AddTransient<IWireMockService, WireMockService>();
// Add app
serviceCollection.AddTransient<App>();
} }
} }
} }

View File

@@ -8,7 +8,7 @@
}, },
"iisExpress": { "iisExpress": {
"applicationUrl": "http://localhost:56513/", "applicationUrl": "http://localhost:56513/",
"sslPort": 0 "sslPort": 44304
} }
}, },
"profiles": { "profiles": {

View File

@@ -7,6 +7,7 @@
<AssemblyName>WireMock.Net.WebApplication</AssemblyName> <AssemblyName>WireMock.Net.WebApplication</AssemblyName>
<RootNamespace>WireMock.Net.WebApplication</RootNamespace> <RootNamespace>WireMock.Net.WebApplication</RootNamespace>
<UserSecretsId>efcf4a18-fd7c-4622-1111-336d65290599</UserSecretsId> <UserSecretsId>efcf4a18-fd7c-4622-1111-336d65290599</UserSecretsId>
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@@ -1,6 +1,6 @@
using System; using System;
using System.Threading;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json; using Newtonsoft.Json;
using WireMock.Admin.Requests; using WireMock.Admin.Requests;
using WireMock.Logging; using WireMock.Logging;
@@ -11,8 +11,7 @@ namespace WireMock.Net.WebApplication
{ {
public class WireMockService : IWireMockService public class WireMockService : IWireMockService
{ {
private static int sleepTime = 30000; private WireMockServer _server;
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly WireMockServerSettings _settings; private readonly WireMockServerSettings _settings;
@@ -57,27 +56,27 @@ namespace WireMock.Net.WebApplication
} }
} }
public WireMockService(ILogger logger, WireMockServerSettings settings) public WireMockService(ILogger<WireMockService> logger, IOptions<WireMockServerSettings> settings)
{ {
_logger = logger; _logger = logger;
_settings = settings; _settings = settings.Value;
_settings.Logger = new Logger(logger); _settings.Logger = new Logger(logger);
} }
public void Run() public void Start()
{ {
_logger.LogInformation("WireMock.Net server starting"); _logger.LogInformation("WireMock.Net server starting");
WireMockServer.Start(_settings); _server = WireMockServer.Start(_settings);
_logger.LogInformation($"WireMock.Net server settings {JsonConvert.SerializeObject(_settings)}"); _logger.LogInformation($"WireMock.Net server settings {JsonConvert.SerializeObject(_settings)}");
}
while (true) public void Stop()
{ {
_logger.LogInformation("WireMock.Net server running"); _logger.LogInformation("WireMock.Net server stopping");
Thread.Sleep(sleepTime); _server?.Stop();
}
} }
} }
} }

View File

@@ -7,6 +7,6 @@
<handlers> <handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers> </handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" hostingModel="InProcess" /> <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
</system.webServer> </system.webServer>
</configuration> </configuration>