In my ConfigureServices
method I want to read a file (in my case a certificate used for signing tokens, but it could be any file necessary for setting up services). Therefore I need to know the ApplicationBasePath
from IApplicationEnvironment
.
Currently I solve the problem by getting the IApplicationEnvironment
service like this:
public void ConfigureServices(IServiceCollection services)
{
...
string basePath;
var serviceProvider = services.BuildServiceProvider();
try
{
basePath = serviceProvider.GetRequiredService<IApplicationEnvironment>().ApplicationBasePath;
}
finally
{
(serviceProvider as IDisposable)?.Dispose();
}
...
}
This approach works but I am not sure if that's the right approach. So my questions are:
ConfigureServices
?ConfigureServices
?IDisposable
correctly?You can easily access it in ConfigureServices, just persist it to a property during Startup method which is called first and gets it passed in, then you can access the property from ConfigureServices. public Startup(IWebHostEnvironment env, IApplicationEnvironment appEnv) { ... your code here...
Yes, you can access the database! Code that runs in the Configure method can access any services that are added in the ConfigureServices method, including things like database contexts.
The ConfigureServices method is a public method on your Startup class that takes an IServiceCollection instance as a parameter and optionally returns an IServiceProvider . The ConfigureServices method is called before Configure .
How do you check if current environment is development or not? If you need to check whether the application is running in a particular environment, use env. IsEnvironment("environmentname") since it will correctly ignore case (instead of checking if env. EnvironmentName == "Development" for example).
The runtime allows dependency injection in the constructor of the Startup
class:
public class Startup
{
private readonly IApplicationEnvironment _appEnv;
public Startup(IApplicationEnvironment appEnv)
{
_appEnv = appEnv;
}
public void ConfigureServices(IServiceCollection services)
{
string basePath = _appEnv.ApplicationBasePath;
}
}
To obtain a reference to an implementer of the IApplicationEnviroment
interface you can use PlatformServices.Default.Application
.
The following example shows this in the context of a project's Startup.cs file:
using Microsoft.Extensions.PlatformAbstractions;
namespace MyWebApp
{
public class Startup
{
private IApplicationEnvironment _appEnv { get; set; }
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false);
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
// obtain IApplicationEnvironment instance
_appEnv = PlatformServices.Default.Application;
}
...
The following line shows how to use that instance to obtain the application's base path:
string basepath = _appEnv.ApplicationBasePath
References:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With