How to Per-Request caching in ASP.net core

后端 未结 2 1116
礼貌的吻别
礼貌的吻别 2021-02-19 05:04

My old code looks like this:

    public static class DbHelper {
        // One conection per request
        public static         


        
2条回答
  •  臣服心动
    2021-02-19 05:23

    Will not the database or connection string would be same across the requests?

    If so then you could do it by a static variable and middleware.

    The middleware would check and set the info on each request start and static variable would store the value then your method could read it from the static variable.

    Other simpler approach would be to inject/pass the IHttpContextAccessor as parameter. With this you could do with minimal changes but you have the pass the IHttpContextAccessor service from each calling method.

提交回复
热议问题