Runtime services no longer get injected into DNX console app (RC1)

自闭症网瘾萝莉.ら 提交于 2019-12-13 14:27:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!