【问题标题】:Runtime services no longer get injected into DNX console app (RC1)运行时服务不再注入 DNX 控制台应用程序 (RC1)
【发布时间】:2016-01-26 13:56:56
【问题描述】:
我曾经能够将 IApplicationEnvironment 等运行时服务注入 DNX 控制台应用程序的 Pogram 类的构造函数中。但是,使用 RC1 的最新 CI 版本,服务不再被注入:
public Program(IApplicationEnvironment env)
{
if (env == null)
{
// env is null.
throw new ArgumentNullException(nameof(env));
}
}
【问题讨论】:
标签:
c#
dependency-injection
asp.net-core
dnx
【解决方案1】:
DNX 平台希望与常规的Program.Main 入口点兼容。因此,他们移除了对Program 类的依赖注入。
相反,您可以使用新的PlatformServices 类,它提供对运行时服务的访问:
public Program()
{
var env = PlatformServices.Default.Application;
}
PlatformServices 类位于 Microsoft.Extensions.PlatformAbstractions 命名空间中。
ILibraryExporter 和 ICompilerOptionsProvider 等类型现在通过 Microsoft.Extensions.CompilationAbstractions 命名空间中的 CompilationServices 类公开。
> Reference