如何使用 DI 在类构造函数中获取 Microsoft.AspNet.Http.HttpContext 实例 [英] How to get Microsoft.AspNet.Http.HttpContext instance in Class Constructor using DI

查看:9
本文介绍了如何使用 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 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆