From 1e137c7af9e9264636f5e353baf37bd16c63a61c Mon Sep 17 00:00:00 2001 From: Praburaj Date: Thu, 10 Apr 2014 14:15:29 -0700 Subject: [PATCH] Getting the application base path from the IoC container. --- .../Interfaces/IApplicationEnvironment.cs | 20 +++++++++ src/MusicStore/LKG.json | 44 ++++++++++--------- src/MusicStore/Startup.cs | 33 +++----------- src/MusicStore/project.json | 1 + 4 files changed, 50 insertions(+), 48 deletions(-) create mode 100644 src/MusicStore/Interfaces/IApplicationEnvironment.cs diff --git a/src/MusicStore/Interfaces/IApplicationEnvironment.cs b/src/MusicStore/Interfaces/IApplicationEnvironment.cs new file mode 100644 index 0000000000..466f9687e1 --- /dev/null +++ b/src/MusicStore/Interfaces/IApplicationEnvironment.cs @@ -0,0 +1,20 @@ +using System; +using System.Runtime.Versioning; + +namespace Microsoft.Net.Runtime +{ + [AssemblyNeutral] + public interface IApplicationEnvironment + { + string ApplicationName { get; } + string Version { get; } + string ApplicationBasePath { get; } + FrameworkName TargetFramework { get; } + } + + [AssemblyNeutral] + [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)] + public sealed class AssemblyNeutralAttribute : Attribute + { + } +} \ No newline at end of file diff --git a/src/MusicStore/LKG.json b/src/MusicStore/LKG.json index 91848459e9..811498b480 100644 --- a/src/MusicStore/LKG.json +++ b/src/MusicStore/LKG.json @@ -1,31 +1,32 @@ { "version": "0.1-alpha-*", "dependencies": { - "Helios": "0.1-alpha-231", - "Microsoft.AspNet.Abstractions": "0.1-alpha-234", - "Microsoft.AspNet.Mvc": "0.1-alpha-553", + "Helios": "0.1-alpha-243", + "Microsoft.AspNet.Abstractions": "0.1-alpha-239", + "Microsoft.AspNet.Mvc": "0.1-alpha-573", "Microsoft.AspNet.Razor": "0.1-alpha-181", "Microsoft.AspNet.ConfigurationModel": "0.1-alpha-174", - "Microsoft.AspNet.DependencyInjection": "0.1-alpha-242", - "Microsoft.AspNet.RequestContainer": "0.1-alpha-234", - "Microsoft.AspNet.Routing": "0.1-alpha-214", - "Microsoft.AspNet.Mvc.ModelBinding": "0.1-alpha-553", - "Microsoft.AspNet.Mvc.Core": "0.1-alpha-553", - "Microsoft.AspNet.Mvc.Razor": "0.1-alpha-553", - "Microsoft.AspNet.StaticFiles": "0.1-alpha-175", - "System.Security.Claims": "0.1-alpha-115", + "Microsoft.AspNet.DependencyInjection": "0.1-alpha-243", + "Microsoft.AspNet.RequestContainer": "0.1-alpha-243", + "Microsoft.AspNet.Routing": "0.1-alpha-220", + "Microsoft.AspNet.Mvc.ModelBinding": "0.1-alpha-573", + "Microsoft.AspNet.Mvc.Core": "0.1-alpha-573", + "Microsoft.AspNet.Mvc.Razor": "0.1-alpha-573", + "Microsoft.AspNet.StaticFiles": "0.1-alpha-180", + "System.Security.Claims": "0.1-alpha-120", "System.Security.Principal": "4.0.0.0", "Microsoft.AspNet.Security.DataProtection": "0.1-alpha-142", - "Microsoft.AspNet.Identity": "0.1-alpha-315", - "Microsoft.AspNet.Identity.Entity": "0.1-alpha-315", - "Microsoft.AspNet.Identity.InMemory": "0.1-alpha-315", - "Microsoft.Data.Entity": "0.1-alpha-416", - "Microsoft.Data.Relational": "0.1-alpha-416", - "Microsoft.Data.SqlServer": "0.1-pre-416", - "Microsoft.Data.InMemory": "0.1-alpha-416", - "Microsoft.AspNet.Diagnostics": "0.1-alpha-119", - "Microsoft.AspNet.Hosting": "0.1-alpha-234", - "Microsoft.AspNet.Server.WebListener": "0.1-alpha-168", + "Microsoft.AspNet.Identity": "0.1-alpha-327", + "Microsoft.AspNet.Identity.Entity": "0.1-alpha-327", + "Microsoft.AspNet.Identity.InMemory": "0.1-alpha-327", + "Microsoft.Data.Entity": "0.1-alpha-419", + "Microsoft.Data.Relational": "0.1-alpha-419", + "Microsoft.Data.SqlServer": "0.1-pre-419", + "Microsoft.Data.InMemory": "0.1-alpha-419", + "Microsoft.Data.Migrations": "0.1-alpha-419", + "Microsoft.AspNet.Diagnostics": "0.1-alpha-124", + "Microsoft.AspNet.Hosting": "0.1-alpha-243", + "Microsoft.AspNet.Server.WebListener": "0.1-alpha-180", "Microsoft.AspNet.Configuration.Json": "0.1-alpha-174" }, "configurations": { @@ -41,6 +42,7 @@ "System.Collections": "4.0.0.0", "System.Linq": "4.0.0.0", "System.Runtime": "4.0.20.0", + "System.Runtime.Extensions": "4.0.10.0", "System.Dynamic.Runtime": "4.0.0.0", "System.Threading.Tasks": "4.0.10.0", "System.ComponentModel": "4.0.0.0", diff --git a/src/MusicStore/Startup.cs b/src/MusicStore/Startup.cs index c0b43d2f76..348f1016d5 100644 --- a/src/MusicStore/Startup.cs +++ b/src/MusicStore/Startup.cs @@ -17,12 +17,13 @@ using System; using System.Collections.Generic; using System.IO; using System.Reflection; +using Microsoft.Net.Runtime; public class Startup { public void Configuration(IBuilder app) { - CreateAdminUser(); + CreateAdminUser(app.ServiceProvider); //ErrorPageOptions.ShowAll to be used only at development time. Not recommended for production. app.UseErrorPage(ErrorPageOptions.ShowAll); @@ -50,11 +51,13 @@ public class Startup SampleData.InitializeMusicStoreDatabaseAsync().Wait(); } - private async void CreateAdminUser() + private async void CreateAdminUser(IServiceProvider serviceProvider) { + var applicationEnvironment = serviceProvider.GetService(); + var configuration = new Configuration(); configuration.AddEnvironmentVariables(); //If configuration flows through environment we should pick that first - configuration.AddJsonFile(Path.Combine(GetApplicationBasePath(), "Config.json")); + configuration.AddJsonFile(Path.Combine(applicationEnvironment.ApplicationBasePath, "Config.json")); string _username = configuration.Get("DefaultAdminUsername"); string _password = configuration.Get("DefaultAdminPassword"); @@ -78,28 +81,4 @@ public class Startup await userManager.AddToRoleAsync(user, _role); } } - - //To find the application base path at runtime. - private static string GetApplicationBasePath() - { -#if NET45 - var applicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; -#else // CORECLR_TODO: ApplicationBase - var appDomainType = typeof(object) - .GetTypeInfo() - .Assembly - .GetType("System.AppDomain"); - - var currentAppDomainProperty = appDomainType.GetRuntimeProperty("CurrentDomain"); - - var currentAppDomain = currentAppDomainProperty.GetValue(null); - - var getDataMethod = appDomainType - .GetRuntimeMethod("GetData", new[] { typeof(string) }); - - string applicationBase = (string)getDataMethod.Invoke(currentAppDomain, new object[] { "APPBASE" }); -#endif - - return applicationBase; - } } \ No newline at end of file diff --git a/src/MusicStore/project.json b/src/MusicStore/project.json index 76b3aeb1a9..a62283c080 100644 --- a/src/MusicStore/project.json +++ b/src/MusicStore/project.json @@ -45,6 +45,7 @@ "System.Collections": "4.0.0.0", "System.Linq": "4.0.0.0", "System.Runtime": "4.0.20.0", + "System.Runtime.Extensions": "4.0.10.0", "System.Dynamic.Runtime": "4.0.0.0", "System.Threading.Tasks": "4.0.10.0", "System.ComponentModel": "4.0.0.0",