<tfoot id='lP02u'></tfoot>

    1. <i id='lP02u'><tr id='lP02u'><dt id='lP02u'><q id='lP02u'><span id='lP02u'><b id='lP02u'><form id='lP02u'><ins id='lP02u'></ins><ul id='lP02u'></ul><sub id='lP02u'></sub></form><legend id='lP02u'></legend><bdo id='lP02u'><pre id='lP02u'><center id='lP02u'></center></pre></bdo></b><th id='lP02u'></th></span></q></dt></tr></i><div id='lP02u'><tfoot id='lP02u'></tfoot><dl id='lP02u'><fieldset id='lP02u'></fieldset></dl></div>

    2. <legend id='lP02u'><style id='lP02u'><dir id='lP02u'><q id='lP02u'></q></dir></style></legend>
        <bdo id='lP02u'></bdo><ul id='lP02u'></ul>
    3. <small id='lP02u'></small><noframes id='lP02u'>

      ASP.NET Core 依赖解析器

      时间:2023-06-08

      • <small id='U6dAm'></small><noframes id='U6dAm'>

      • <i id='U6dAm'><tr id='U6dAm'><dt id='U6dAm'><q id='U6dAm'><span id='U6dAm'><b id='U6dAm'><form id='U6dAm'><ins id='U6dAm'></ins><ul id='U6dAm'></ul><sub id='U6dAm'></sub></form><legend id='U6dAm'></legend><bdo id='U6dAm'><pre id='U6dAm'><center id='U6dAm'></center></pre></bdo></b><th id='U6dAm'></th></span></q></dt></tr></i><div id='U6dAm'><tfoot id='U6dAm'></tfoot><dl id='U6dAm'><fieldset id='U6dAm'></fieldset></dl></div>
        <legend id='U6dAm'><style id='U6dAm'><dir id='U6dAm'><q id='U6dAm'></q></dir></style></legend>

            <tbody id='U6dAm'></tbody>
                <bdo id='U6dAm'></bdo><ul id='U6dAm'></ul>
                <tfoot id='U6dAm'></tfoot>

              • 本文介绍了ASP.NET Core 依赖解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在 ASP.NET MVC 5 中,可以通过 DependencyResolver.Current.GetService() 获得一些依赖关系.ASP.NET Core 中是否有类似的东西?

                In ASP.NET MVC 5 is possible to obtain some dependency through DependencyResolver.Current.GetService<T>(). Is there something similar in ASP.NET Core?

                推荐答案

                是的,有.在 ASP.NET Core 1.0.0 中,来自 HttpContext 的请求中可用的服务通过 RequestServices 集合公开[1]:

                Yes, there is. In ASP.NET Core 1.0.0, the services available within a request from HttpContext are exposed through the RequestServices collection[1]:

                this.HttpContext.RequestServices
                

                您可以使用GetService方法通过指定依赖的类型来检索依赖:

                You can use the GetService method to retrieve the dependencies by specifying the type of the dependency:

                this.HttpContext.RequestServices.GetService(typeof(ISomeService));
                

                通常,您不应该直接使用这些属性,而是更愿意通过类的构造函数来请求您的类所需的类型,并让框架注入这些依赖项.这会产生更容易测试且耦合更松散的类.

                Generally, you shouldn’t use these properties directly, preferring instead to request the types your classes you require via your class’s constructor, and letting the framework inject these dependencies. This yields classes that are easier to test and are more loosely coupled.

                [1] https://docs.asp.net/en/latest/fundamentals/dependency-injection.html#request-services

                这篇关于ASP.NET Core 依赖解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 MVC6 中正确注入 HttpContext 下一篇:从 ASP.NET 5 控制器 VS 2015 获取 wwwroot 文件夹路径

                相关文章

                  <bdo id='yX71T'></bdo><ul id='yX71T'></ul>
              • <i id='yX71T'><tr id='yX71T'><dt id='yX71T'><q id='yX71T'><span id='yX71T'><b id='yX71T'><form id='yX71T'><ins id='yX71T'></ins><ul id='yX71T'></ul><sub id='yX71T'></sub></form><legend id='yX71T'></legend><bdo id='yX71T'><pre id='yX71T'><center id='yX71T'></center></pre></bdo></b><th id='yX71T'></th></span></q></dt></tr></i><div id='yX71T'><tfoot id='yX71T'></tfoot><dl id='yX71T'><fieldset id='yX71T'></fieldset></dl></div>
                <legend id='yX71T'><style id='yX71T'><dir id='yX71T'><q id='yX71T'></q></dir></style></legend>

                    <small id='yX71T'></small><noframes id='yX71T'>

                    <tfoot id='yX71T'></tfoot>