Updated Startup and Program to use new .NETCoreApp2.0 templates.Minor Refactorings

This commit is contained in:
Unai Zorrilla Castro 2017-08-29 11:28:21 +02:00
parent c4454b005b
commit 116d5b55bb
6 changed files with 30 additions and 39 deletions

View File

@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.HealthChecks;
using System.Threading.Tasks;
using WebStatus.Viewmodels;
namespace WebStatus.Controllers
@ -29,6 +25,7 @@ namespace WebStatus.Controllers
}
ViewBag.RefreshSeconds = 60;
return View(data);
}

View File

@ -1,8 +1,5 @@
using Microsoft.Extensions.HealthChecks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebStatus.Extensions
{
@ -17,6 +14,5 @@ namespace WebStatus.Extensions
return builder;
}
}
}

View File

@ -1,5 +1,7 @@
using System.IO;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using System.IO;
namespace WebStatus
{
@ -7,14 +9,18 @@ namespace WebStatus
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.ConfigureLogging((hostingContext, builder) =>
{
builder.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
builder.AddConsole();
builder.AddDebug();
}).Build();
}
}

View File

@ -13,22 +13,18 @@ namespace WebStatus
{
public class Startup
{
public Startup(IHostingEnvironment env)
public Startup(IConfiguration configuration)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
Configuration = configuration;
}
public IConfigurationRoot Configuration { get; }
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
// Add framework services.
services.AddHealthChecks(checks =>
{
@ -47,15 +43,13 @@ namespace WebStatus
checks.AddUrlCheckIfNotNull(Configuration["mvc"], TimeSpan.FromMinutes(minutes));
checks.AddUrlCheckIfNotNull(Configuration["spa"], TimeSpan.FromMinutes(minutes));
});
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();

View File

@ -1,22 +1,23 @@
using Microsoft.Extensions.HealthChecks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebStatus.Viewmodels
{
public class HealthStatusViewModel
{
private readonly CheckStatus _overall;
private readonly Dictionary<string, IHealthCheckResult> _results;
public CheckStatus OverallStatus => _overall;
public IEnumerable<NamedCheckResult> Results => _results.Select(kvp => new NamedCheckResult(kvp.Key, kvp.Value));
private HealthStatusViewModel() => _results = new Dictionary<string, IHealthCheckResult>();
public HealthStatusViewModel(CheckStatus overall) : this() => _overall = overall;
public void AddResult(string name, IHealthCheckResult result) => _results.Add(name, result);
}
}

View File

@ -1,14 +1,11 @@
using Microsoft.Extensions.HealthChecks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebStatus.Viewmodels
{
public class NamedCheckResult
{
public string Name { get; }
public IHealthCheckResult Result { get; }
public NamedCheckResult(string name, IHealthCheckResult result)