Remove usage of IApplicationEnvironment

This commit is contained in:
Pranav K 2016-04-25 16:11:48 -07:00
parent 5a8263bb26
commit b9048889db
7 changed files with 38 additions and 42 deletions

View File

@ -4,9 +4,9 @@ using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore; using Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -21,19 +21,19 @@ namespace MusicStore
{ {
public class StartupOpenIdConnectTesting public class StartupOpenIdConnectTesting
{ {
private readonly IRuntimeEnvironment _runtimeEnvironment; private readonly Platform _platform;
public StartupOpenIdConnectTesting(IApplicationEnvironment env, IRuntimeEnvironment runtimeEnvironment) public StartupOpenIdConnectTesting(IHostingEnvironment env)
{ {
//Below code demonstrates usage of multiple configuration sources. For instance a setting say 'setting1' is found in both the registered sources, //Below code demonstrates usage of multiple configuration sources. For instance a setting say 'setting1' is found in both the registered sources,
//then the later source will win. By this way a Local config can be overridden by a different setting while deployed remotely. //then the later source will win. By this way a Local config can be overridden by a different setting while deployed remotely.
var builder = new ConfigurationBuilder() var builder = new ConfigurationBuilder()
.SetBasePath(env.ApplicationBasePath) .SetBasePath(env.ContentRootPath)
.AddJsonFile("config.json") .AddJsonFile("config.json")
.AddEnvironmentVariables(); //All environment variables in the process's context flow in as configuration values. .AddEnvironmentVariables(); //All environment variables in the process's context flow in as configuration values.
Configuration = builder.Build(); Configuration = builder.Build();
_runtimeEnvironment = runtimeEnvironment; _platform = new Platform();
} }
public IConfiguration Configuration { get; private set; } public IConfiguration Configuration { get; private set; }
@ -43,7 +43,7 @@ namespace MusicStore
services.Configure<AppSettings>(Configuration.GetSection("AppSettings")); services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
//Sql client not available on mono //Sql client not available on mono
var useInMemoryStore = !_runtimeEnvironment.OperatingSystem.Equals("Windows", StringComparison.OrdinalIgnoreCase); var useInMemoryStore = _platform.IsRunningOnMono;
// Add EF services to the services container // Add EF services to the services container
if (useInMemoryStore) if (useInMemoryStore)

View File

@ -5,10 +5,10 @@ using Microsoft.AspNetCore.Authentication.Twitter;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore; using Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -25,20 +25,20 @@ namespace MusicStore
{ {
public class StartupSocialTesting public class StartupSocialTesting
{ {
private readonly IRuntimeEnvironment _runtimeEnvironment; private readonly Platform _platform;
public StartupSocialTesting(IApplicationEnvironment appEnvironment, IRuntimeEnvironment runtimeEnvironment) public StartupSocialTesting(IHostingEnvironment hostingEnvironment)
{ {
//Below code demonstrates usage of multiple configuration sources. For instance a setting say 'setting1' is found in both the registered sources, // Below code demonstrates usage of multiple configuration sources. For instance a setting say 'setting1' is found in both the registered sources,
//then the later source will win. By this way a Local config can be overridden by a different setting while deployed remotely. // then the later source will win. By this way a Local config can be overridden by a different setting while deployed remotely.
var builder = new ConfigurationBuilder() var builder = new ConfigurationBuilder()
.SetBasePath(appEnvironment.ApplicationBasePath) .SetBasePath(hostingEnvironment.ContentRootPath)
.AddJsonFile("config.json") .AddJsonFile("config.json")
.AddEnvironmentVariables() //All environment variables in the process's context flow in as configuration values. .AddEnvironmentVariables() // All environment variables in the process's context flow in as configuration values.
.AddJsonFile("configoverride.json", optional: true); // Used to override some configuration parameters that cannot be overridden by environment. .AddJsonFile("configoverride.json", optional: true); // Used to override some configuration parameters that cannot be overridden by environment.
Configuration = builder.Build(); Configuration = builder.Build();
_runtimeEnvironment = runtimeEnvironment; _platform = new Platform();
} }
public IConfiguration Configuration { get; private set; } public IConfiguration Configuration { get; private set; }
@ -47,8 +47,8 @@ namespace MusicStore
{ {
services.Configure<AppSettings>(Configuration.GetSection("AppSettings")); services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
//Sql client not available on mono // Sql client not available on mono
var useInMemoryStore = !_runtimeEnvironment.OperatingSystem.Equals("Windows", StringComparison.OrdinalIgnoreCase); var useInMemoryStore = _platform.IsRunningOnMono;
// Add EF services to the services container // Add EF services to the services container
if (useInMemoryStore) if (useInMemoryStore)

View File

@ -3,11 +3,11 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Security.Claims; using System.Security.Claims;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
namespace MusicStore.Models namespace MusicStore.Models
{ {
@ -78,10 +78,10 @@ namespace MusicStore.Models
/// <returns></returns> /// <returns></returns>
private static async Task CreateAdminUser(IServiceProvider serviceProvider) private static async Task CreateAdminUser(IServiceProvider serviceProvider)
{ {
var appEnv = serviceProvider.GetService<IApplicationEnvironment>(); var env = serviceProvider.GetService<IHostingEnvironment>();
var builder = new ConfigurationBuilder() var builder = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath) .SetBasePath(env.ContentRootPath)
.AddJsonFile("config.json") .AddJsonFile("config.json")
.AddEnvironmentVariables(); .AddEnvironmentVariables();
var configuration = builder.Build(); var configuration = builder.Build();

View File

@ -17,24 +17,17 @@ namespace MusicStore
int dwSpMinorVersion, int dwSpMinorVersion,
out int pdwReturnedProductType); out int pdwReturnedProductType);
private readonly IRuntimeEnvironment _runtimeEnvironment;
private bool? _isNano; private bool? _isNano;
private bool? _isMono; private bool? _isMono;
private bool? _isWindows; private bool? _isWindows;
public Platform(IRuntimeEnvironment runtimeEnvironment)
{
_runtimeEnvironment = runtimeEnvironment;
}
public bool IsRunningOnWindows public bool IsRunningOnWindows
{ {
get get
{ {
if (_isWindows == null) if (_isWindows == null)
{ {
_isWindows = _runtimeEnvironment.OperatingSystem.Equals( _isWindows = PlatformServices.Default.Runtime.OperatingSystem.Equals(
"Windows", StringComparison.OrdinalIgnoreCase); "Windows", StringComparison.OrdinalIgnoreCase);
} }
@ -48,7 +41,10 @@ namespace MusicStore
{ {
if (_isMono == null) if (_isMono == null)
{ {
_isMono = _runtimeEnvironment.RuntimeType.Equals("Mono", StringComparison.OrdinalIgnoreCase); _isMono = string.Equals(
PlatformServices.Default.Runtime.RuntimeType,
"Mono",
StringComparison.OrdinalIgnoreCase);
} }
return _isMono.Value; return _isMono.Value;
@ -61,7 +57,7 @@ namespace MusicStore
{ {
if (_isNano == null) if (_isNano == null)
{ {
var osVersion = new Version(_runtimeEnvironment.OperatingSystemVersion ?? ""); var osVersion = new Version(PlatformServices.Default.Runtime.OperatingSystemVersion ?? "");
try try
{ {
@ -86,6 +82,5 @@ namespace MusicStore
return _isNano.Value; return _isNano.Value;
} }
} }
} }
} }

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@ -14,19 +15,19 @@ namespace MusicStore
{ {
private readonly Platform _platform; private readonly Platform _platform;
public Startup(IApplicationEnvironment applicationEnvironment, IRuntimeEnvironment runtimeEnvironment) public Startup(IHostingEnvironment hostingEnvironment)
{ {
// Below code demonstrates usage of multiple configuration sources. For instance a setting say 'setting1' // Below code demonstrates usage of multiple configuration sources. For instance a setting say 'setting1'
// is found in both the registered sources, then the later source will win. By this way a Local config // is found in both the registered sources, then the later source will win. By this way a Local config
// can be overridden by a different setting while deployed remotely. // can be overridden by a different setting while deployed remotely.
var builder = new ConfigurationBuilder() var builder = new ConfigurationBuilder()
.SetBasePath(applicationEnvironment.ApplicationBasePath) .SetBasePath(hostingEnvironment.ContentRootPath)
.AddJsonFile("config.json") .AddJsonFile("config.json")
//All environment variables in the process's context flow in as configuration values. //All environment variables in the process's context flow in as configuration values.
.AddEnvironmentVariables(); .AddEnvironmentVariables();
Configuration = builder.Build(); Configuration = builder.Build();
_platform = new Platform(runtimeEnvironment); _platform = new Platform();
} }
public IConfiguration Configuration { get; private set; } public IConfiguration Configuration { get; private set; }

View File

@ -1,13 +1,12 @@
using System; using System;
using System.Security.Claims; using System.Security.Claims;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.Net.Http.Server;
using MusicStore.Components; using MusicStore.Components;
using MusicStore.Models; using MusicStore.Models;
@ -32,13 +31,13 @@ namespace MusicStore
/// </summary> /// </summary>
public class StartupNtlmAuthentication public class StartupNtlmAuthentication
{ {
public StartupNtlmAuthentication(IApplicationEnvironment applicationEnvironment) public StartupNtlmAuthentication(IHostingEnvironment hostingEnvironment)
{ {
// Below code demonstrates usage of multiple configuration sources. For instance a setting say 'setting1' // Below code demonstrates usage of multiple configuration sources. For instance a setting say 'setting1'
// is found in both the registered sources, then the later source will win. By this way a Local config // is found in both the registered sources, then the later source will win. By this way a Local config
// can be overridden by a different setting while deployed remotely. // can be overridden by a different setting while deployed remotely.
var builder = new ConfigurationBuilder() var builder = new ConfigurationBuilder()
.SetBasePath(applicationEnvironment.ApplicationBasePath) .SetBasePath(hostingEnvironment.ContentRootPath)
.AddJsonFile("config.json") .AddJsonFile("config.json")
//All environment variables in the process's context flow in as configuration values. //All environment variables in the process's context flow in as configuration values.
.AddEnvironmentVariables(); .AddEnvironmentVariables();

View File

@ -1,10 +1,10 @@
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.IdentityModel.Protocols.OpenIdConnect; using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using MusicStore.Components; using MusicStore.Components;
using MusicStore.Models; using MusicStore.Models;
@ -32,19 +32,19 @@ namespace MusicStore
{ {
private readonly Platform _platform; private readonly Platform _platform;
public StartupOpenIdConnect(IApplicationEnvironment env, IRuntimeEnvironment runtimeEnvironment) public StartupOpenIdConnect(IHostingEnvironment hostingEnvironment)
{ {
// Below code demonstrates usage of multiple configuration sources. For instance a setting say 'setting1' // Below code demonstrates usage of multiple configuration sources. For instance a setting say 'setting1'
// is found in both the registered sources, then the later source will win. By this way a Local config can // is found in both the registered sources, then the later source will win. By this way a Local config can
// be overridden by a different setting while deployed remotely. // be overridden by a different setting while deployed remotely.
var builder = new ConfigurationBuilder() var builder = new ConfigurationBuilder()
.SetBasePath(env.ApplicationBasePath) .SetBasePath(hostingEnvironment.ContentRootPath)
.AddJsonFile("config.json") .AddJsonFile("config.json")
//All environment variables in the process's context flow in as configuration values. //All environment variables in the process's context flow in as configuration values.
.AddEnvironmentVariables(); .AddEnvironmentVariables();
Configuration = builder.Build(); Configuration = builder.Build();
_platform = new Platform(runtimeEnvironment); _platform = new Platform();
} }
public IConfiguration Configuration { get; private set; } public IConfiguration Configuration { get; private set; }
@ -98,7 +98,8 @@ namespace MusicStore
{ {
options.AddPolicy( options.AddPolicy(
"ManageStore", "ManageStore",
authBuilder => { authBuilder =>
{
authBuilder.RequireClaim("ManageStore", "Allowed"); authBuilder.RequireClaim("ManageStore", "Allowed");
}); });
}); });