3

I used to be able to inject runtime services like IApplicationEnvironment into the constructor of the Pogram class of a DNX console application. However, using the latest CI build of RC1, the services no longer get injected:

public Program(IApplicationEnvironment env)
{
    if (env == null)
    {
        // env is null.
        throw new ArgumentNullException(nameof(env));
    }
}
Henk Mollema
  • 44,194
  • 12
  • 93
  • 104

1 Answers1

5

The DNX platform wants to be compatible with regular Program.Main entry points. Therefore they removed dependency injection into the Program class.

In stead, you can use the new PlatformServices class which provides access to runtime services:

public Program()
{
    var env = PlatformServices.Default.Application;
}

The PlatformServices class lives in the Microsoft.Extensions.PlatformAbstractions namespace.

Types like ILibraryExporter and ICompilerOptionsProvider are now exposed through the CompilationServices class in the Microsoft.Extensions.CompilationAbstractions namespace.

> Reference

Henk Mollema
  • 44,194
  • 12
  • 93
  • 104