Skip to content

Commit 116d5b5

Browse files
Unai Zorrilla CastroUnai Zorrilla Castro
authored andcommitted
Updated Startup and Program to use new .NETCoreApp2.0 templates.Minor Refactorings
1 parent c4454b0 commit 116d5b5

6 files changed

Lines changed: 29 additions & 38 deletions

File tree

src/Web/WebStatus/Controllers/HomeController.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Microsoft.AspNetCore.Mvc;
6-
using Microsoft.Extensions.Options;
1+
using Microsoft.AspNetCore.Mvc;
72
using Microsoft.Extensions.HealthChecks;
3+
using System.Threading.Tasks;
84
using WebStatus.Viewmodels;
95

106
namespace WebStatus.Controllers
@@ -29,6 +25,7 @@ public async Task<IActionResult> Index()
2925
}
3026

3127
ViewBag.RefreshSeconds = 60;
28+
3229
return View(data);
3330
}
3431

src/Web/WebStatus/Extensions/HealthCheckBuilderExtensions.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using Microsoft.Extensions.HealthChecks;
22
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading.Tasks;
63

74
namespace WebStatus.Extensions
85
{
@@ -17,6 +14,5 @@ public static HealthCheckBuilder AddUrlCheckIfNotNull(this HealthCheckBuilder bu
1714

1815
return builder;
1916
}
20-
2117
}
2218
}

src/Web/WebStatus/Program.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
using System.IO;
1+
using Microsoft.AspNetCore;
22
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.Logging;
4+
using System.IO;
35

46
namespace WebStatus
57
{
68
public class Program
79
{
810
public static void Main(string[] args)
911
{
10-
var host = new WebHostBuilder()
11-
.UseKestrel()
12+
BuildWebHost(args).Run();
13+
}
14+
15+
public static IWebHost BuildWebHost(string[] args) =>
16+
WebHost.CreateDefaultBuilder(args)
1217
.UseContentRoot(Directory.GetCurrentDirectory())
13-
.UseIISIntegration()
1418
.UseStartup<Startup>()
15-
.Build();
16-
17-
host.Run();
18-
}
19+
.ConfigureLogging((hostingContext, builder) =>
20+
{
21+
builder.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
22+
builder.AddConsole();
23+
builder.AddDebug();
24+
}).Build();
1925
}
2026
}

src/Web/WebStatus/Startup.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,18 @@ namespace WebStatus
1313
{
1414
public class Startup
1515
{
16-
public Startup(IHostingEnvironment env)
16+
public Startup(IConfiguration configuration)
1717
{
18-
var builder = new ConfigurationBuilder()
19-
.SetBasePath(env.ContentRootPath)
20-
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
21-
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
22-
.AddEnvironmentVariables();
23-
Configuration = builder.Build();
18+
Configuration = configuration;
2419
}
2520

26-
public IConfigurationRoot Configuration { get; }
21+
public IConfiguration Configuration { get; }
2722

2823
// This method gets called by the runtime. Use this method to add services to the container.
2924
public void ConfigureServices(IServiceCollection services)
3025
{
3126
services.AddOptions();
27+
3228
// Add framework services.
3329
services.AddHealthChecks(checks =>
3430
{
@@ -47,15 +43,13 @@ public void ConfigureServices(IServiceCollection services)
4743
checks.AddUrlCheckIfNotNull(Configuration["mvc"], TimeSpan.FromMinutes(minutes));
4844
checks.AddUrlCheckIfNotNull(Configuration["spa"], TimeSpan.FromMinutes(minutes));
4945
});
46+
5047
services.AddMvc();
5148
}
5249

5350
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
54-
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
51+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
5552
{
56-
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
57-
loggerFactory.AddDebug();
58-
5953
if (env.IsDevelopment())
6054
{
6155
app.UseDeveloperExceptionPage();
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
using Microsoft.Extensions.HealthChecks;
2-
using System;
32
using System.Collections.Generic;
43
using System.Linq;
5-
using System.Threading.Tasks;
64

75
namespace WebStatus.Viewmodels
86
{
97
public class HealthStatusViewModel
108
{
119
private readonly CheckStatus _overall;
10+
1211
private readonly Dictionary<string, IHealthCheckResult> _results;
1312

1413
public CheckStatus OverallStatus => _overall;
14+
1515
public IEnumerable<NamedCheckResult> Results => _results.Select(kvp => new NamedCheckResult(kvp.Key, kvp.Value));
16+
1617
private HealthStatusViewModel() => _results = new Dictionary<string, IHealthCheckResult>();
18+
1719
public HealthStatusViewModel(CheckStatus overall) : this() => _overall = overall;
20+
1821
public void AddResult(string name, IHealthCheckResult result) => _results.Add(name, result);
19-
20-
2122
}
2223
}

src/Web/WebStatus/Viewmodels/NamedCheckResult.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
using Microsoft.Extensions.HealthChecks;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading.Tasks;
62

73
namespace WebStatus.Viewmodels
84
{
95
public class NamedCheckResult
106
{
117
public string Name { get; }
8+
129
public IHealthCheckResult Result { get; }
1310

1411
public NamedCheckResult(string name, IHealthCheckResult result)

0 commit comments

Comments
 (0)