Access IApplicationEnvironment in ConfigureServices

In my ConfigureServices method, I want to read a file (in my case, the certificate used to sign the tokens, but it can be any file needed to configure services). Therefore, I need to know ApplicationBasePath from IApplicationEnvironment .

I am currently IApplicationEnvironment problem by getting the IApplicationEnvironment service as follows:

 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'm not sure if this is the right approach. So my questions are:

  • Is there a better way to read a file in ConfigureServices ?
  • Is there a better way to get the base path of an application in ConfigureServices ?
  • If my approach is correct, am I handling IDisposable correctly?
+5
source share
3 answers

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; } } 
+12
source

How to get an instance of IApplicationEnvironment after the release of DNX 1.0.0-rc1-15996

To get a reference to the implementation of the IApplicationEnviroment interface, you can use PlatformServices.Default.Application .

The following example shows this in the context of the Startup.cs project:

 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 this instance to get the base path of the application:

  string basepath = _appEnv.ApplicationBasePath 

Literature:

+1
source

Why not just assign a path directly from PlatformServices.Default.Application.ApplicationBasePath ? As the name implies, Default (not entirely correct, since Current also used here and there) is a static member of the PlatformServices type.

More at dept ...

In addition to Default , Application , which is of type ApplicationEnvironment , is the only property open class. And this is reasonable, because the ApplicationEnvironment simply encapsulates some information about the running application (or applications, no matter how much they may be). Importantly, the default ctor class is private . It smells like Singleton, huh ??

Obviously, the developers decided to change the view in the application environment: it does not serve anything, this is general information. The only way to use it is above.

And something else ...

IApplicationEnvironment magically disappeared, again, because it turned out that ApplicationEnvironment not a service that is expected to be modified or re-implemented for different operating systems and, possibly, from that moment, ApplicationEnvironment implements nothing. Very nice!

0
source

All Articles