如何在ASP.NET应用程序中使用Autofac注册HttpContext和HttpContextBase类?

时间:2014-09-16 06:42:10

标签: dependency-injection autofac

我有一个SessionWebAppService类,它在构造函数中使用HttpContext和HttpContextBase类:

public class SessionWebAppService : WebAppServiceBase, ISessionWebAppService
{
    public SessionWebAppService(HttpContext httpContext)
        : this(new ConcertDb(), new WebFormContext(httpContext))
    {
    }

    public SessionWebAppService(HttpContextBase httpContext)
        : this(new ConcertDb(), new MvcContext(httpContext))
    {
    }

    public SessionWebAppService(IConcertDb concertDb, IConcertHttpContext httpContext)
        : base(concertDb, httpContext)
    {
    }

我应该使用Autofac在Global.asax.cs文件中的ASP.NET应用程序中注册HttpContext和HttpContextBase类,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

如果您正在使用MVC,则可以注册Autofac.Integration.Mvc.AutofacWebTypesModule以获取已注册的网络抽象(例如HttpContextBase)。

如果你没有使用MVC,你可以look at the source for AutofacWebTypesModule查看它是如何完成的。