如何在启动时初始化应用程序状态并从MVC 6中的控制器访问它? [英] How do you initialize application state at startup and access it from controllers in MVC 6?

查看:74
本文介绍了如何在启动时初始化应用程序状态并从MVC 6中的控制器访问它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有一个名为MySiteConfiguration的类,其中有很多配置数据,您猜对了.加载后,这些数据将不会在应用程序的运行过程中发生变化.

Let's say I have a class called MySiteConfiguration in which I have a bunch of, you guessed it, configuration data. This data will not change over the course of the application's runtime after it has been loaded.

我的目标是在启动时构造此类的实例,并从我的控制器操作中访问它.我不想多次构造该类,因为这不需要.

My goal would be to construct an instance of this class at startup and access it from my controller actions. I do not want to construct the class more than once, as this should not be needed.

例如,要在WebApi 2中进行此操作,

To do this in WebApi 2 for instance, I would:

  1. 在我的应用程序启动方法中实例化该类.
  2. 将实例存储在HttpConfiguration.Properties上
  3. 创建一个从ApiController继承的ControllerBase类.
  4. 覆盖我的ControllerBase类中的Initialize(HttpControllerContext)方法.此替代将从HttpControllerContext.Configuration.Properties中读取配置实例,并将其分配给ControllerBase中的属性/字段.
  1. Instantiate the class in my application start method.
  2. Store the instance on the HttpConfiguration.Properties
  3. Create a ControllerBase class which inherits from ApiController.
  4. Override the Initialize(HttpControllerContext) method in my ControllerBase class. This override would read the configuration instance from HttpControllerContext.Configuration.Properties and assign it to a property / field in ControllerBase.

任何需要访问配置实例的控制器都将继承ControllerBase并引用基本属性.还不错...

Any controller needing access to the configuration instance would inherit ControllerBase and reference the base property. Not so bad...

话虽如此,但据我所知,这种模式在新框架中不起作用.在MVC 6的新Controller类上没有覆盖任何初始化方法.我对新的Startup.cs模式和可用的中间件还不太熟悉,无法知道从哪里开始这个问题.

With that said, this pattern does not work in the new framework from what I can tell. There is no initialize method to override on MVC 6's new Controller class. I'm also not familiar enough with the new Startup.cs patterns and middleware available to know where to start with this problem.

谢谢.

推荐答案

使用依赖注入.注册包含您的数据的单例服务,然后在控制器上使用构造函数注入来获取服务实例.

Use dependency injection. Register a singleton service that has your data, and then use constructor injection on your controllers to acquire the service instance.

首先,定义服务.服务实际上可以是任何类或接口.

First, define a service. A service can really be any class or interface.

public class MyConfigService {
    // declare some properties/methods/whatever on here
}

在您的Startup.cs中执行以下操作:

In your Startup.cs do something like this:

services.AddSingleton<MyConfigService>();

(请注意,根据您的情况,还有AddSingleton的其他重载.)

(Note that there are other overloads of AddSingleton depending on your scenario.)

然后在每个控制器中使用它:

And then consume it in each controller:

public MyController : Controller {
    public MyController(MyConfigService myService) {
        // do something with the service (read some data from it, store it in a private field/property, etc.
    }
}

这篇关于如何在启动时初始化应用程序状态并从MVC 6中的控制器访问它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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