Use the in memory store on the Nano server

This commit is contained in:
Victor Hurdugaci 2015-05-06 09:55:47 -07:00
parent 8c9969ed3f
commit 63038b7278
3 changed files with 83 additions and 8 deletions

View File

@ -0,0 +1,77 @@
using System;
using System.Runtime.InteropServices;
using Microsoft.Framework.Runtime;
using Microsoft.Framework.Runtime.Infrastructure;
namespace MusicStore
{
internal class Platform
{
// Defined in winnt.h
private const int PRODUCT_NANO_SERVER = 0x0000006D;
[DllImport("api-ms-win-core-sysinfo-l1-2-1.dll", SetLastError = false)]
private static extern bool GetProductInfo(
int dwOSMajorVersion,
int dwOSMinorVersion,
int dwSpMajorVersion,
int dwSpMinorVersion,
out int pdwReturnedProductType);
private readonly IRuntimeEnvironment _runtimeEnvironment;
private bool? _isNano;
private bool? _isMono;
public Platform(IRuntimeEnvironment runtimeEnvironment)
{
_runtimeEnvironment = runtimeEnvironment;
}
public bool IsRunningOnMono
{
get
{
if (_isMono == null)
{
_isMono = _runtimeEnvironment.RuntimeType.Equals("Mono", StringComparison.OrdinalIgnoreCase);
}
return _isMono.Value;
}
}
public bool IsRunningOnNanoServer
{
get
{
if (_isNano == null)
{
var osVersion = new Version(_runtimeEnvironment.OperatingSystemVersion);
try
{
int productType;
if (GetProductInfo(osVersion.Major, osVersion.Minor, 0,0, out productType))
{
_isNano = productType == PRODUCT_NANO_SERVER;
}
else
{
_isNano = false;
}
}
catch
{
// If the API call fails, the API set is not there which means
// that we are definetely not running on Nano
_isNano = false;
}
}
return _isNano.Value;
}
}
}
}

View File

@ -17,7 +17,7 @@ namespace MusicStore
{
public class Startup
{
private readonly IRuntimeEnvironment _runtimeEnvironment;
private readonly Platform _platform;
public Startup(IApplicationEnvironment env, IRuntimeEnvironment runtimeEnvironment)
{
@ -27,7 +27,7 @@ namespace MusicStore
.AddJsonFile("config.json")
.AddEnvironmentVariables(); //All environment variables in the process's context flow in as configuration values.
_runtimeEnvironment = runtimeEnvironment;
_platform = new Platform(runtimeEnvironment);
}
public IConfiguration Configuration { get; private set; }
@ -36,8 +36,7 @@ namespace MusicStore
{
services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));
//Sql client not available on mono
var useInMemoryStore = _runtimeEnvironment.RuntimeType.Equals("Mono", StringComparison.OrdinalIgnoreCase);
var useInMemoryStore = _platform.IsRunningOnMono || _platform.IsRunningOnNanoServer;
// Add EF services to the services container
if (useInMemoryStore)

View File

@ -30,7 +30,7 @@ namespace MusicStore
/// </summary>
public class StartupOpenIdConnect
{
private readonly IRuntimeEnvironment _runtimeEnvironment;
private readonly Platform _platform;
public StartupOpenIdConnect(IApplicationEnvironment env, IRuntimeEnvironment runtimeEnvironment)
{
@ -40,7 +40,7 @@ namespace MusicStore
.AddJsonFile("config.json")
.AddEnvironmentVariables(); //All environment variables in the process's context flow in as configuration values.
_runtimeEnvironment = runtimeEnvironment;
_platform = new Platform(runtimeEnvironment);
}
public IConfiguration Configuration { get; private set; }
@ -49,8 +49,7 @@ namespace MusicStore
{
services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));
//Sql client not available on mono
var useInMemoryStore = _runtimeEnvironment.RuntimeType.Equals("Mono", StringComparison.OrdinalIgnoreCase);
var useInMemoryStore = _platform.IsRunningOnMono || _platform.IsRunningOnNanoServer;
// Add EF services to the services container
if (useInMemoryStore)