如何使用IHttpContextAccessor实现UserFactory [英] How to implement a UserFactory using IHttpContextAccessor

查看:118
本文介绍了如何使用IHttpContextAccessor实现UserFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经有一个使用HttpContext.CurrentUserFactory(在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆