-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
80 lines (72 loc) · 2.57 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using CAHealthQueries.NPPES;
using System;
using Microsoft.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
/*
* Explore and create boilerplate code for data access to SQL Server.
* This is a .Net Core Console application that leverages Dependency Injection.
* These routines rely upon data loaded by two GitHub projects; NPPES-Data-Load
* and CAHealthFacilityDBLoad.
*
*/
namespace CAHealthQueries
{
class Program
{
static private IHostEnvironment _env { get; set; }
static void Main(string[] args)
{
try
{
// Configure the host container
var hbldr = CreateHostBuilder(args).Build();
// Configure services
var config = hbldr.Services.GetService<IConfiguration>();
var serviceProvider = ConfigureServices(config);
// Execute the application
serviceProvider.GetService<ConsoleApplication>().run(args);
}
catch (Exception ex)
{
throw ex;
}
finally
{
NLog.LogManager.Shutdown();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingcontex, config) =>
{
_env = hostingcontex.HostingEnvironment;
config.AddEnvironmentVariables()
.AddCommandLine(args);
if (_env.IsDevelopment())
{
config.AddUserSecrets<ConsoleApplication>();
}
});
public static ServiceProvider ConfigureServices(IConfiguration config)
{
return new ServiceCollection()
.AddLogging(builder =>
{
builder.ClearProviders();
builder.SetMinimumLevel(LogLevel.Trace);
builder.AddNLog("nlog.config");
})
.AddTransient<ConsoleApplication>()
.AddSingleton<IConfiguration>(config)
.AddSingleton<IHostEnvironment>(_env)
.BuildServiceProvider();
}
}
}