【问题标题】:How to initialize and use Custom / Support classes in MVC 6 with Asp.Net Core 2.0如何使用 Asp.Net Core 2.0 在 MVC 6 中初始化和使用自定义/支持类
【发布时间】:2018-03-04 11:26:07
【问题描述】:
public sealed class SessionContext
{
    private ISession httpContext;
    public  SessionContext(ISession httpContext)
    { 
        this.httpContext = httpContext;
    } 
    public string UserType
    {
        get
        {
            return httpContext.GetString("_UserType");
        }
        set
        {
            httpContext.SetString("_UserType", value);
        }
    }
    ...... More properties ..... 
 }

public class HomeController : Controller
{
    private AppSettings _appSettings; 
    private SessionContext session = null;
    private readonly IHttpContextAccessor _httpContextAccessor;
    private ISession httpContext => _httpContextAccessor.HttpContext.Session;   
//I don't like this constructor as it is getting initialize or every controller call.
    public HomeController(IOptions<AppSettings> myAppSettings, IHttpContextAccessor httpContextAccessor)
    {
        _appSettings = myAppSettings.Value;
        _httpContextAccessor = httpContextAccessor;
        appSettings = new AppSettings(_appSettings);  //Should initialize only once.
        session = new SessionContext(httpContext);  
    }
}

我有关于...的问题

  1. 如何使用 Asp.Net Core 2.0 在 MVC 6 中初始化和使用自定义/支持类 当我初始化这些类时,它们会被初始化或每次控制器调用。这是非常多余的。
  2. 我的 SessionContext 类每次都重新初始化。所以当我从另一个控制器调用这个类时,我失去了这些值。

    我尝试了这种方法,但用处不大。 services.AddSingleton();

【问题讨论】:

  • 终于...解决了问题。感谢博客强类型配置设置在 ASP.NET Core Part II .... 由 Khalid Abuhakmeh 撰写

标签: class asp.net-core-mvc helper


【解决方案1】:

从问题转到答案:

public void ConfigureServices(IServiceCollection services)
{
   services.AddOptions();
   services.AddSingleton<SessionContext, SessionContext>(); 
//calling the extension class to instantiate the classes which we require earlier.
   services.AddMyProjectHelper(Configuration)  
}

创建了一个扩展类...它初始化了支持类

public static class MyProjectHelperExtensions
  {
        public static IServiceCollection AddMyProjectHelper(this IServiceCollection services, IConfiguration configuration)
        {
            var section = configuration.GetSection("AppSettings");

           // we first need to create an instance
            var settings = new AppSettings();

           // then we set the properties 
            new ConfigureFromConfigurationOptions<AppSettings>(section).Configure(settings); 

           var session = services.BuildServiceProvider().GetService<SessionContext>(); 

           // then we register the instance into the services collection
            services.AddSingleton<MyProjectHelper>(new MyProjectHelper(settings, session));

           return services;
        }
}

最后,控制器 ctor 将 DI 用于所需的类。现在我避免了支持类的冗余初始化。

public SecurityController(MyProjectHelper objHelper, SessionContext sessionContext)
{ 
   session = sessionContext;
   projectHelper  = projectHelper  ?? objHelper; 
}

现在,我可以共享我在支持类中设置的会话变量

private SessionContext session = null;
public HomeController(SessionContext sessionContext)
{
   session = sessionContext;
}

[Authorize]
public IActionResult Index()
{
   if (session.CurrEmployee != null)
   {
      ViewBag.Name = (session.CurrEmployee.FirstName + " " + session.CurrEmployee.LastName);
      return View();
   }
}  

【讨论】:

    猜你喜欢
    • 2018-02-18
    • 2017-02-11
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2017-03-30
    相关资源
    最近更新 更多