无法在 AsyncController 的构造函数中访问 HttpContext 或 Request 对象

Unable to access the HttpContext or Request object in the constructor of an AsyncController

我正在使用 AsyncController 说 'AbstractAbcContoller'

public class AbstractAbcContoller : AsyncController
{
}

然后我还有 3 个控制器继承了新创建的 AbstractAbcContoller

现在,当我为这 3 个控制器中的任何一个创建构造函数并尝试访问 HttpContext/Request 对象时,我总是将它们设为 null。

我需要初始化我的服务对象,它接受 HttpRequestBase 作为其构造函数中的参数。

我不得不接受这个解决方法。

public class AbstractAbcController : AsyncController
{
    protected IAbcService GetAbcService(AbstractAbcController controller, HttpRequestBase request)
    {
        if (controller.GetType() == typeof (AbcAddressVerificationController))
            return new AbcAddressVerificationService(request);

        if (controller.GetType() == typeof(AbcPhoneVerificationController))
            return new AbcPhoneVerificationService(request);

        throw new ArgumentException();
    }
}

然后在controller/action中使用这样的东西

IAbcService abcService = GetAbcService(this, Request);
abcService.DoSomething();

我有点不喜欢,我不确定哪种方法更好。请指教

请求和响应上下文是每个请求对象,每个请求上下文都有一个单独的副本,因此没有必要通过构造函数注入它们,因此应该向需要它们的每个方法传递一个参数