1. <tfoot id='mqyXh'></tfoot>

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

        <legend id='mqyXh'><style id='mqyXh'><dir id='mqyXh'><q id='mqyXh'></q></dir></style></legend>

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

        • <bdo id='mqyXh'></bdo><ul id='mqyXh'></ul>

        如何使用 DI 在类构造函数中获取 Microsoft.AspNet.Http.HttpContext 实例

        How to get Microsoft.AspNet.Http.HttpContext instance in Class Constructor using DI(如何使用 DI 在类构造函数中获取 Microsoft.AspNet.Http.HttpContext 实例)
          <tbody id='jSJ3c'></tbody>
        <tfoot id='jSJ3c'></tfoot><legend id='jSJ3c'><style id='jSJ3c'><dir id='jSJ3c'><q id='jSJ3c'></q></dir></style></legend>
        <i id='jSJ3c'><tr id='jSJ3c'><dt id='jSJ3c'><q id='jSJ3c'><span id='jSJ3c'><b id='jSJ3c'><form id='jSJ3c'><ins id='jSJ3c'></ins><ul id='jSJ3c'></ul><sub id='jSJ3c'></sub></form><legend id='jSJ3c'></legend><bdo id='jSJ3c'><pre id='jSJ3c'><center id='jSJ3c'></center></pre></bdo></b><th id='jSJ3c'></th></span></q></dt></tr></i><div id='jSJ3c'><tfoot id='jSJ3c'></tfoot><dl id='jSJ3c'><fieldset id='jSJ3c'></fieldset></dl></div>

            <bdo id='jSJ3c'></bdo><ul id='jSJ3c'></ul>
            • <small id='jSJ3c'></small><noframes id='jSJ3c'>

                • 本文介绍了如何使用 DI 在类构造函数中获取 Microsoft.AspNet.Http.HttpContext 实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在 MVC 6 中构建一个一次性应用程序,并尝试使用不同的依赖关系架构.

                  I am building a throwaway application in MVC 6 and experimenting with different architectures for dependencies.

                  我面临的问题是如何创建特定于应用程序的自定义MyAppContext"对象.这将需要来自 HttpContext 的一些信息和来自数据库的一些信息,并且将是应用程序特定属性的请求范围存储库.我想将 HttpContext 的实例传递给 'MyAppContext' 的构造函数.

                  The problem I am facing is how to create a custom 'MyAppContext' object specific to the Application. This would require some information from the HttpContext and some information from the database, and will be a request-scoped repository for application specific attributes. I want to pass the instance of the HttpContext into the constructor of the 'MyAppContext'.

                  我已经使用 DI 成功创建了一个带有 IDataService 接口的 'DataService' 对象,这可以正常工作.与MyAppContext"类的不同之处在于它在构造函数中有两个参数——DataService"和Microsoft.AspNet.Http.HttpContext.这是 MyAppContext 类:

                  I have successfully created a 'DataService' object with an IDataService interface using DI and this works Ok. The difference with the 'MyAppContext' class is that it has two parameters in the constructor - the 'DataService' and the Microsoft.AspNet.Http.HttpContext. Here is the MyAppContext class:

                  public class MyAppContext : IMyAppContext
                  {
                      public MyAppContext(IDataService dataService, HttpContext httpContext)
                      {
                         //do stuff here with the httpContext
                      }
                  }
                  

                  在启动代码中,我注册了DataService实例和MyAppContext实例:

                  In the startup code, I register the DataService instance and the MyAppContext instance:

                      public void ConfigureServices(IServiceCollection services)
                      {
                          services.AddMvc();
                          //adds a singleton instance of the DataService using DI
                          services.AddSingleton<IDataService, DataService>();
                          services.AddScoped<IMyAppContext, MyAppContext>();    
                  
                      }
                  
                      public void Configure(IApplicationBuilder app)
                      {
                          app.UseErrorPage();
                          app.UseRequestServices();
                          app.UseMvc(routes => /* routes stuff */);
                      }
                  

                  我希望构造函数中的 HttpContext 参数能够被 DI 解析.运行代码时,这是我返回的异常:

                  I am expecting the HttpContext parameter in the constructor to get resolved by DI. When running the code, this is the exception I get returned:

                  InvalidOperationException:尝试激活MyAppContext"时无法解析Microsoft.AspNet.Http.HttpContext"类型的服务

                  InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNet.Http.HttpContext' while attempting to activate 'MyAppContext'

                  我认为这是因为没有发生此错误的 HttpContext 的特定实例,但我不知道如何在 DI 中注册 HttpContext 实例.我添加了行 'app.UseRequestServices();' 但这没有任何区别.我还尝试了以下变体:

                  I figure this is because there is no specific instance of HttpContext that this error is occurring, but I don't know how to register the HttpContext instance in DI. I added the line 'app.UseRequestServices();' but this hasn't made any difference. I also tried a variant of:

                  services.AddScoped<HttpContext, HttpContext>();
                  

                  但这失败了,因为第二个 HttpContext 应该是一个实例 - 我知道它不正确但无法弄清楚是什么.

                  But this fails because the second HttpContext is supposed to be an instance - I know it's not correct but haven't been able to work out what is.

                  所以,总而言之 - 我如何将 HttpContext 对象传入 MyAppContext 的构造函数?

                  So, in summary - how can I pass in the HttpContext object into the constructor of MyAppContext?

                  推荐答案

                  在构造函数中注入IHttpContextAccessor

                  这篇关于如何使用 DI 在类构造函数中获取 Microsoft.AspNet.Http.HttpContext 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding DbContextOptions in Startup.cs not registering data store(在 Startup.cs 中添加 DbContextOptions 未注册数据存储)
                  Migrate html helpers to ASP.NET Core(将 html 帮助程序迁移到 ASP.NET Core)
                  Conditional validation in MVC.NET Core (RequiredIf)(MVC.NET Core 中的条件验证(RequiredIf))
                  Is it possible to serve static files from outside the wwwroot folder?(是否可以从 wwwroot 文件夹外部提供静态文件?)
                  Working with multiple resultset in .net core(在 .net 核心中使用多个结果集)
                  Where all types for http headers gone in ASP.NET 5?(ASP.NET 5 中所有类型的 http 标头都去了哪里?)

                • <legend id='7rOV3'><style id='7rOV3'><dir id='7rOV3'><q id='7rOV3'></q></dir></style></legend>
                      <tfoot id='7rOV3'></tfoot>

                      1. <small id='7rOV3'></small><noframes id='7rOV3'>

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