如何使用IHttpContextAccessor实现UserFactory

编程入门 行业动态 更新时间:2024-10-16 02:31:11
本文介绍了如何使用IHttpContextAccessor实现UserFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我曾经有一个使用HttpContext.Current的UserFactory(在vNext之前),但是由于现在已经不存在了,所以我在重新创建它时遇到了麻烦.

I used to have a UserFactory (before vNext) that used HttpContext.Current but since that is now gone I am having trouble recreating it.

我希望它是一个静态类,用于设置并让当前用户访问整个应用程序中的用户数据.

I want it to be a static class that sets and gets the current user to access user data throughout the application.

我知道我必须使用DI系统,但不确定如何使用.

I know I must use the DI system but not sure how.

代码:

public class CurrentContext : IHttpContextAccessor { private IHttpContextAccessor ctx; public HttpContext HttpContext { get { return ctx.HttpContext; } set { ctx.HttpContext = value; } } } services.AddTransient<IHttpContextAccessor, CurrentContext>(); public class UserFactory { private IHttpContextAccessor _context; public UserFactory(IHttpContextAccessor Context) { _context = Context; } public void Add(string s) => _context.HttpContext.Session.SetString(s, s); public string Get(string s) => _context.HttpContext.Session.GetString(s); }

如何在具有当前上下文的应用中的任何地方获取UserFactory实例?

How can I get a UserFactory instance anywhere in my app with the current context?

推荐答案

我建议您将UserFactory类设为非静态并将其注册为作用域:

I suggest you make the UserFactory class non-static and register it as scoped:

services.AddScoped<UserFactory>();

这将为每个Web请求创建一个实例.您可以将其注入到其他所有类中,并让UserFactory依赖于IHttpContextAccessor以获得当前的HttpContext.

This will create one instance per web request. You can inject this into every other class and let the UserFactory take a dependency on IHttpContextAccessor to get the current HttpContext.

这符合Microsoft试图在ASP.NET 5中实现的依赖关系反转哲学.静态类并不真正适合于此,应尽可能避免使用.

This adheres to the dependency inversion philosophy Microsoft is trying to implement in ASP.NET 5. Static classes do not really fit into this and should be avoided as much as possible.

示例

UserFactory类:

public class UserFactory { private readonly IHttpContextAccessor _httpContextAccessor; public UserFactory(IHttpContextAccessor httpContextAccessor) { _httpContextAccessor = httpContextAccessor; } // other code... }

类中的

ConfigureServices():

public void ConfigureServices(IServiceCollection services) { // ... services.AddScoped<UserFactory>(); // ... }

现在您可以在控制器中注入UserFactory的实例,例如:

Now you can inject an instance of UserFactory in a controller for example:

public class SomeController : Controller { private readonly UserFactory _userFactory; public SomeController(UserFactory userFactory) { _userFactory = userFactory; } // ... }

现在,当UserFactory开始解析时,UserFactory内部的IHttpContextFactory也将被解析.

Now when UserFactory is begin resolved, IHttpContextFactory inside UserFactory will also be resolved.

更多推荐

如何使用IHttpContextAccessor实现UserFactory

本文发布于:2023-11-17 00:09:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608084.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   IHttpContextAccessor   UserFactory

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!