Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access IApplicationEnvironment in ConfigureServices

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:

  1. Is there a better way to read a file in ConfigureServices?
  2. Is there a better way to get the application base path in ConfigureServices?
  3. If my approach is correct, do I handle IDisposable correctly?
like image 408
Rainer Avatar asked Jun 26 '15 06:06

Rainer


People also ask

How do I get IWebHostEnvironment in ConfigureServices?

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...

Can I access a database during startup in ASP.NET Core?

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.

What ConfigureServices () method does in startup CS?

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?

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).


2 Answers

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;
    }
}
like image 147
Henk Mollema Avatar answered Oct 17 '22 05:10

Henk Mollema


How to obtain an IApplicationEnvironment instance following the release of DNX 1.0.0-rc1-15996

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:

  1. Refactoring DNX not to inject into Program constructors #2990
  2. IApplicationEnvironment no longer injected in DNX command
like image 33
AperioOculus Avatar answered Oct 17 '22 06:10

AperioOculus