问题
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));
}
}
回答1:
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
来源:https://stackoverflow.com/questions/33363178/runtime-services-no-longer-get-injected-into-dnx-console-app-rc1