Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Figuring out Hosting Environment outside of MVC/ASP.NET 5 specific code

$
0
0

My ASP.NET 5 (RC1) project accesses, through couple of other layers, a Data Access Layer where I have to know connection string to my database. Technically I can read the connection string (this code is executed deep in Data Access Layer):

ConfigurationBuilder builder = new ConfigurationBuilder();
builder.AddJsonFile("appsettings.json");
IConfigurationRoot config = builder.Build();
var connectionString = config["Data:DefaultConnection:ConnectionString"];

But this code is unable to load a settings file corresponding to an environment. So, I have appsettings.json used in Production and appsettingsDevelopment.json which should be used in Developlment environment. What I need is an access to the property HostingEnvironment.EnvironmentName so it tells me what my current environment is (this code is executed deep in Data Access Layer):

HostingEnvironment env = new HostingEnvironment();
string envName = env.EnvironmentName;

The code above always returns environment Production, mostlikely this is just default value for the property. I understand that I am probably misusing the class and ideally its instance should be taken through Dependency Injection (in Startup/Controllers) so the Framework initializes it with right values but I need to know the environment very far from Startup/Controllers.

As an option I could pass a reference to IHostingEnvironment through all my layers down to Data Access Level but this solution does not look clean to me. What are my other options?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>