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.
This commit is contained in:
Praburaj 2014-04-09 10:43:52 -07:00
parent f4cee29842
commit 8610e016b3
2 changed files with 32 additions and 3 deletions

View File

@ -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;
}
}

View File

@ -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"
}
}
}