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.Builder;
using Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@ -21,19 +21,19 @@ namespace MusicStore
{
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,
//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()
.SetBasePath(env.ApplicationBasePath)
.SetBasePath(env.ContentRootPath)
.AddJsonFile("config.json")
.AddEnvironmentVariables(); //All environment variables in the process's context flow in as configuration values.
Configuration = builder.Build();
_runtimeEnvironment = runtimeEnvironment;
_platform = new Platform();
}
public IConfiguration Configuration { get; private set; }
@ -43,7 +43,7 @@ namespace MusicStore
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
//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
if (useInMemoryStore)

View File

@ -5,10 +5,10 @@ using Microsoft.AspNetCore.Authentication.Twitter;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@ -25,20 +25,20 @@ namespace MusicStore
{
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,
//then the later source will win. By this way a Local config can be overridden by a different setting while deployed remotely.
// 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.
var builder = new ConfigurationBuilder()
.SetBasePath(appEnvironment.ApplicationBasePath)
.SetBasePath(hostingEnvironment.ContentRootPath)
.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.
Configuration = builder.Build();
_runtimeEnvironment = runtimeEnvironment;
_platform = new Platform();
}
public IConfiguration Configuration { get; private set; }
@ -47,8 +47,8 @@ namespace MusicStore
{
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
//Sql client not available on mono
var useInMemoryStore = !_runtimeEnvironment.OperatingSystem.Equals("Windows", StringComparison.OrdinalIgnoreCase);
// Sql client not available on mono
var useInMemoryStore = _platform.IsRunningOnMono;
// Add EF services to the services container
if (useInMemoryStore)

View File

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

View File

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

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
@ -14,19 +15,19 @@ namespace MusicStore
{
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'
// 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.
var builder = new ConfigurationBuilder()
.SetBasePath(applicationEnvironment.ApplicationBasePath)
.SetBasePath(hostingEnvironment.ContentRootPath)
.AddJsonFile("config.json")
//All environment variables in the process's context flow in as configuration values.
.AddEnvironmentVariables();
Configuration = builder.Build();
_platform = new Platform(runtimeEnvironment);
_platform = new Platform();
}
public IConfiguration Configuration { get; private set; }

View File

@ -1,13 +1,12 @@
using System;
using System.Security.Claims;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.Net.Http.Server;
using MusicStore.Components;
using MusicStore.Models;
@ -32,13 +31,13 @@ namespace MusicStore
/// </summary>
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'
// 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.
var builder = new ConfigurationBuilder()
.SetBasePath(applicationEnvironment.ApplicationBasePath)
.SetBasePath(hostingEnvironment.ContentRootPath)
.AddJsonFile("config.json")
//All environment variables in the process's context flow in as configuration values.
.AddEnvironmentVariables();

View File

@ -1,10 +1,10 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using MusicStore.Components;
using MusicStore.Models;
@ -32,19 +32,19 @@ namespace MusicStore
{
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'
// 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.
var builder = new ConfigurationBuilder()
.SetBasePath(env.ApplicationBasePath)
.SetBasePath(hostingEnvironment.ContentRootPath)
.AddJsonFile("config.json")
//All environment variables in the process's context flow in as configuration values.
.AddEnvironmentVariables();
Configuration = builder.Build();
_platform = new Platform(runtimeEnvironment);
_platform = new Platform();
}
public IConfiguration Configuration { get; private set; }
@ -98,7 +98,8 @@ namespace MusicStore
{
options.AddPolicy(
"ManageStore",
authBuilder => {
authBuilder =>
{
authBuilder.RequireClaim("ManageStore", "Allowed");
});
});