Last active
September 23, 2017 17:33
-
-
Save gravity00/66027a85ee6ba5376ebd8b0d103be576 to your computer and use it in GitHub Desktop.
SimpleSoft.Hosting.WithoutBuilder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class Program | |
| { | |
| private static readonly CancellationTokenSource TokenSource; | |
| static Program() | |
| { | |
| TokenSource = new CancellationTokenSource(); | |
| Console.CancelKeyPress += (sender, args) => | |
| { | |
| TokenSource.Cancel(); | |
| args.Cancel = true; | |
| }; | |
| } | |
| public static int Main(string[] args) => | |
| MainAsync(args, TokenSource.Token).ConfigureAwait(false).GetAwaiter().GetResult(); | |
| private static async Task<int> MainAsync(string[] args, CancellationToken ct) | |
| { | |
| int resultCode; | |
| if (TryConfigureLogging(out var loggerFactory, out var logger)) | |
| { | |
| logger.LogInformation("Application started..."); | |
| if (TryBuildConfigurations(logger, args, out var configurationRoot)) | |
| { | |
| if (TryBuildServiceProvider(logger, loggerFactory, configurationRoot, out var serviceProvider)) | |
| { | |
| try | |
| { | |
| using (var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope()) | |
| { | |
| await scope.ServiceProvider.GetRequiredService<Application>().RunAsync(ct) | |
| .ConfigureAwait(false); | |
| } | |
| resultCode = 0; | |
| } | |
| catch (TaskCanceledException) | |
| { | |
| logger.LogWarning("The application execution was canceled by request"); | |
| resultCode = 2; | |
| } | |
| catch (Exception e) | |
| { | |
| logger.LogCritical(0, e, "Unexpected exception has occured"); | |
| resultCode = 1; | |
| } | |
| } | |
| else | |
| { | |
| resultCode = 1; | |
| } | |
| } | |
| else | |
| { | |
| resultCode = 1; | |
| } | |
| logger.LogInformation("Application terminated. Press <enter> to exit..."); | |
| } | |
| else | |
| { | |
| Console.WriteLine("Application terminated. Press <enter> to exit..."); | |
| resultCode = 1; | |
| } | |
| Console.ReadLine(); | |
| return resultCode; | |
| } | |
| private static bool TryConfigureLogging(out ILoggerFactory loggerFactory, out ILogger logger) | |
| { | |
| loggerFactory = new LoggerFactory().AddConsole(LogLevel.Trace, true); | |
| logger = loggerFactory.CreateLogger(typeof(Program)); | |
| try | |
| { | |
| logger.LogDebug("Configuring NLog"); | |
| loggerFactory | |
| .AddNLog() | |
| .ConfigureNLog( | |
| Path.Combine(Directory.GetCurrentDirectory(), "nlog.config")); | |
| return true; | |
| } | |
| catch (Exception e) | |
| { | |
| logger.LogCritical(0, e, "Failed to configure application logging"); | |
| loggerFactory = null; | |
| logger = null; | |
| return false; | |
| } | |
| } | |
| private static bool TryBuildConfigurations(ILogger logger, string[] args, out IConfigurationRoot configurationRoot) | |
| { | |
| logger.LogDebug("Building configurations"); | |
| try | |
| { | |
| var environmentName = Environment.GetEnvironmentVariable("ENVIRONMENT"); | |
| if (string.IsNullOrWhiteSpace(environmentName)) | |
| environmentName = "Production"; | |
| logger.LogDebug("Building configurations for '{environmentName}' environment", environmentName); | |
| var configurationBuilder = new ConfigurationBuilder() | |
| .SetBasePath(Directory.GetCurrentDirectory()) | |
| .AddJsonFile("appsettings.json", false, true) | |
| .AddJsonFile($"appsettings.{environmentName}.json", true, true) | |
| .AddEnvironmentVariables() | |
| .AddCommandLine(args); | |
| configurationRoot = configurationBuilder.Build(); | |
| return true; | |
| } | |
| catch (Exception e) | |
| { | |
| logger.LogCritical(0, e, "Failed to build application configurations"); | |
| configurationRoot = null; | |
| return false; | |
| } | |
| } | |
| private static bool TryBuildServiceProvider( | |
| ILogger logger, ILoggerFactory loggerFactory, IConfigurationRoot configurationRoot, out IServiceProvider serviceProvider) | |
| { | |
| logger.LogDebug("Building service provider"); | |
| try | |
| { | |
| var serviceCollection = new ServiceCollection() | |
| .AddSingleton(loggerFactory) | |
| .AddLogging() | |
| .AddSingleton(configurationRoot) | |
| .AddScoped<Application>(); | |
| serviceProvider = serviceCollection.BuildServiceProvider(true); | |
| return true; | |
| } | |
| catch (Exception e) | |
| { | |
| logger.LogCritical(0, e, "Failed to build application service provider"); | |
| serviceProvider = null; | |
| return false; | |
| } | |
| } | |
| public class Application | |
| { | |
| private readonly ILogger<Application> _logger; | |
| public Application(ILogger<Application> logger) | |
| { | |
| _logger = logger; | |
| } | |
| public Task RunAsync(CancellationToken ct) | |
| { | |
| _logger.LogInformation("Running the application logic"); | |
| return Task.CompletedTask; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment