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

查看:87
本文介绍了如何使用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 */);
    }

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

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天全站免登陆