From 8610e016b3711ab1c643068ced5f9d1f3d13a8cb Mon Sep 17 00:00:00 2001 From: Praburaj Date: Wed, 9 Apr 2014 10:43:52 -0700 Subject: [PATCH] Setting the full path to load the Config.json. Specifying just the name of the file works fine on selfhost or IISExpress. When app is hosted in IIS, Config.json is being searched in the w3wp3.exe's folder path resulting in an exception. So fixing the path of Config.json by retrieving the application base path from AppDomain. --- src/MusicStore/Startup.cs | 30 ++++++++++++++++++++++++++++-- src/MusicStore/project.json | 5 ++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/MusicStore/Startup.cs b/src/MusicStore/Startup.cs index 57ac262197..5a28290126 100644 --- a/src/MusicStore/Startup.cs +++ b/src/MusicStore/Startup.cs @@ -15,6 +15,8 @@ using MusicStore.Models; using MusicStore.Web.Models; using System; using System.Collections.Generic; +using System.IO; +using System.Reflection; public class Startup { @@ -52,8 +54,8 @@ public class Startup { var configuration = new Configuration(); configuration.AddEnvironmentVariables(); //If configuration flows through environment we should pick that first - configuration.AddJsonFile("Config.json"); - + configuration.AddJsonFile(Path.Combine(GetApplicationBasePath(), "Config.json")); + string _username = configuration.Get("DefaultAdminUsername"); string _password = configuration.Get("DefaultAdminPassword"); string _role = "Administrator"; @@ -76,4 +78,28 @@ 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 754b2a437a..5bff92db79 100644 --- a/src/MusicStore/project.json +++ b/src/MusicStore/project.json @@ -50,7 +50,10 @@ "System.Console": "4.0.0.0", "System.Diagnostics.Debug": "4.0.10.0", "System.Diagnostics.Tools": "4.0.0.0", - "Microsoft.ComponentModel.DataAnnotations": "4.0.10.0" + "Microsoft.ComponentModel.DataAnnotations": "4.0.10.0", + "System.Reflection": "4.0.10.0", + "System.Reflection.Extensions": "4.0.0.0", + "System.IO": "4.0.0.0" } } }