From 63038b7278edbef928159b8977ff4adccf731f43 Mon Sep 17 00:00:00 2001 From: Victor Hurdugaci Date: Wed, 6 May 2015 09:55:47 -0700 Subject: [PATCH] Use the in memory store on the Nano server --- src/MusicStore/Platform.cs | 77 ++++++++++++++++++++++++++ src/MusicStore/Startup.cs | 7 +-- src/MusicStore/StartupOpenIdConnect.cs | 7 +-- 3 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 src/MusicStore/Platform.cs diff --git a/src/MusicStore/Platform.cs b/src/MusicStore/Platform.cs new file mode 100644 index 0000000000..ca0f0cb32d --- /dev/null +++ b/src/MusicStore/Platform.cs @@ -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; + } + } + + } +} diff --git a/src/MusicStore/Startup.cs b/src/MusicStore/Startup.cs index c32edbd08e..051c5589d1 100644 --- a/src/MusicStore/Startup.cs +++ b/src/MusicStore/Startup.cs @@ -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(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) diff --git a/src/MusicStore/StartupOpenIdConnect.cs b/src/MusicStore/StartupOpenIdConnect.cs index 6c10b3f9a4..07281c7f9b 100644 --- a/src/MusicStore/StartupOpenIdConnect.cs +++ b/src/MusicStore/StartupOpenIdConnect.cs @@ -30,7 +30,7 @@ namespace MusicStore /// 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(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)