简体   繁体   English

每个请求的ASP.NET Core MVC注入

[英]ASP.NET Core MVC inject per request

I am looking forward to inject RequestContext, per request in .Net Core. 我期待在.Net Core中为每个请求注入RequestContext。 inside the service collection. 在服务集合中。

Someone attempted 8 yrs. 有人尝试了8年。 ago. 前。 ASP.NET MVC inject per request 每个请求的ASP.NET MVC注入

public interface IMvcDepency
    {
        string PathValue { get; set; }
    }

public class FakeMvcDepency : IMvcDepency
{
    public string PathValue { get; set; }
}

public class MvcDepency : IMvcDepency
{
    public string PathValue { get; set; }

    public MvcDepency(HttpRequest req)
    {
        PathValue = req.Path.Value;
    }
}

And inject it somewhere in startup, as follows: 并在启动时将其注入到某处,如下所示:

services.AddTransient<IMvcDepency, MvcDepency>(x => x.???);

or in OnActionExecuting like below: 或如下所示在OnActionExecuting中:

public override void OnActionExecuting(ActionExecutingContext actCtx)
    {
        MvcDepency mvcDepency = actCtx.HttpContext.RequestServices.GetService(typeof(IMvcDepency)) as MvcDepency;
        mvcDepency = new MvcDepency(actCtx.HttpContext.Request);
        actCtx.HttpContext.RequestServices.AddService(mvcDepency);// AddService method doesn't in exist
      }

Current Error: System.InvalidOperationException: 'Unable to resolve service for type 'Microsoft.AspNetCore.Http.HttpRequest' while attempting to activate 'CAWP.Api.Controllers.MvcDepency'.' 当前错误: System.InvalidOperationException:'尝试激活'CAWP.Api.Controllers.MvcDepency'时,无法解析类型为'Microsoft.AspNetCore.Http.HttpRequest'的服务。

Controllers already have access to the HttpRequest object in each of the methods via the base class. 控制器已经可以通过基类访问每个方法中的HttpRequest对象。 But it is only available once a method is called (for obvious reasons!). 但是它仅在调用方法后才可用(出于明显的原因!)。 If you want to wrap it in your own class then you can do it in the OnActionExecuting override. 如果要将其包装在自己的类中,则可以在OnActionExecuting重写中进行。

You can create a new MvcDepency class in OnActionExecuting and reference it in the code. 您可以在OnActionExecuting创建一个新的MvcDepency类,并在代码中引用它。 As controllers are created per request you should be able to use a class variable to store the reference. 由于控制器是根据请求创建的,因此您应该能够使用类变量来存储引用。

public class ValuesController : Controller
{
    private IMvcDepency _depency;

    public ValuesController()
    {
    }

    public override void OnActionExecuting(ActionExecutingContext context)
    {
        _depency = new MvcDepency(context.HttpContext.Request);

        base.OnActionExecuting(context);
    }

    // GET api/values
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        var path = _depency.PathValue;

        return new string[] { "PathValue", path };
    }
}

This should result in the MvcDepency class having access to the HttpRequest object. 这应该导致MvcDepency类可以访问HttpRequest对象。 You should add a factory class for your IMvcDepency interface to avoid the new in OnActionExecuting . 您应该为IMvcDepency接口添加一个工厂类,以避免OnActionExecutingnew

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM