将MVC Application_Start中的一些值分配给静态类

时间:2016-09-15 21:24:09

标签: c# asp.net-mvc application-start

我需要在我的MVC应用程序的布局视图中显示一些数据库信息。

我想如果我在Application_Start()方法中执行此操作,并将其分配给具有静态值的静态类,则视图将能够访问这些变量并显示它们......但是他们显得空白。

以下是Application_Start()

NWatchEntityApplication nWatchApp;

protected void Application_Start()
{
    var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>();
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);

    nWatchApp = new NWatchEntityApplication(GetNWatchConfig());
    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(nWatchApp.Configuration.NWatchDatabase);
    Infrastructure.ApplicationInfo.NWatchDatabaseCatalog = builder.InitialCatalog;
    Infrastructure.ApplicationInfo.NWatchDatabaseServer = builder.DataSource;

    var context = nWatchApp.GetDbContext();
    builder = new SqlConnectionStringBuilder(context.DatabaseConnectionString);
    Infrastructure.ApplicationInfo.EntityDatabaseCatalog = builder.InitialCatalog;
    Infrastructure.ApplicationInfo.EntityDatabaseServer = builder.DataSource;

    var webApiContainer = new Container();
    webApiContainer.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();
    RegisterTypes(webApiContainer);
    webApiContainer.RegisterWebApiControllers(GlobalConfiguration.Configuration);
    webApiContainer.Verify();

    GlobalConfiguration.Configuration.DependencyResolver =
        new SimpleInjectorWebApiDependencyResolver(webApiContainer);
}

这是保存信息的静态类:

public static class ApplicationInfo
{
    public static string NWatchDatabaseServer { get; set; }

    public static string NWatchDatabaseCatalog { get; set; }

    public static string EntityDatabaseServer { get; set; }

    public static string EntityDatabaseCatalog { get; set; }
}

以下是应该能够访问这些变量的视图部分:

<div class="footer">
    <div class="footer-inner">
        <div class="footer-content">
            <p>@BranchCircuits_Web.Infrastructure.ApplicationInfo.EntityDatabaseServer
            \@BranchCircuits_Web.Infrastructure.ApplicationInfo.EntityDatabaseCatalog</p>
        </div>
    </div>
</div>

是否有人知道为什么这些视图呈现时会显示空白?

只有在应用程序启动时才能在此处执行此类操作的适当位置?

2 个答案:

答案 0 :(得分:1)

Application_Start方法中填充静态类可能不是最好的主意。首先,它会减慢应用程序的初始启动时间。此外,使用此方法,您无法进行延迟实例化,并等到创建该对象的第一个请求发生时。这意味着您可以创建实例,并且您的站点的任何实例的第一个请求或该值将在很长一段时间后才会发生。

这是静态类的Lazy<T>实现。该类及其相应的代码可以 in 项目的任何部分,并从项目的任何部分引用。使用Lazy<T>将确保仅在调用代码中的某处使用对象时才对其进行实例化。

public class ApplicationInfo
{
    private static readonly Lazy<ApplicationInfo> _instance = new Lazy<ApplicationInfo>(() => new ApplicationInfo());

    public static ApplicationInfo Instance
    {
        get { return _instance.Value; }
    }

    public string NWatchDatabaseServer { get; set; }

    public string NWatchDatabaseCatalog { get; set; }

    public string EntityDatabaseServer { get; set; }

    public string EntityDatabaseCatalog { get; set; }

    private ApplicationInfo()
    {
        //ASSIGN VALUES HERE
        NWatchEntityApplication nWatchApp = new NWatchEntityApplication(GetNWatchConfig());
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(nWatchApp.Configuration.NWatchDatabase);
        Infrastructure.ApplicationInfo.NWatchDatabaseCatalog = builder.InitialCatalog;
        Infrastructure.ApplicationInfo.NWatchDatabaseServer = builder.DataSource;

        var context = nWatchApp.GetDbContext();
        builder = new SqlConnectionStringBuilder(context.DatabaseConnectionString);
        Infrastructure.ApplicationInfo.EntityDatabaseCatalog = builder.InitialCatalog;
        Infrastructure.ApplicationInfo.EntityDatabaseServer = builder.DataSource;

    }
}

假设类存在于项目的根目录中,那就是没有名称空间,您可以从这样的视图中访问该对象:

@ApplicationInfo.Instance.EntityDatabaseServer

如果您有任何疑问,请与我们联系。

答案 1 :(得分:0)

如果您不介意,则应使用普通(可实例化)ApplicationInfo类,如同删除static一样。然后在(静态)实用程序类中,例如Utility,添加类似于以下内容的静态属性:

public static ApplicationInfo AppInfo
{
    get
    {
        return HttpRuntime.Cache["ApplicationInfo"] as ApplicationInfo; //using System.Web;
        //Or, if you prefer this: 
        //MemoryCache.Default["ApplicationInfo"] as ApplicationInfo; //using System.Runtime.Caching;
    }
    set
    {
        HttpRuntime.Cache["ApplicationInfo"] = value; //using System.Web;
        //Or, if you prefer this: 
        //MemoryCache.Default["ApplicationInfo"] = value; //using System.Runtime.Caching;
    }
}

将其设置为Application_Start

var appInfo = new Infrastructure.ApplicationInfo();
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(nWatchApp.Configuration.NWatchDatabase);
appInfo.NWatchDatabaseCatalog = builder.InitialCatalog;
appInfo.NWatchDatabaseServer = builder.DataSource;

var context = nWatchApp.GetDbContext();
builder = new SqlConnectionStringBuilder(context.DatabaseConnectionString);
appInfo.EntityDatabaseCatalog = builder.InitialCatalog;
appInfo.EntityDatabaseServer = builder.DataSource;

// Store 'appInfo' in RAM
Infrastructure.Utility.AppInfo = appInfo;

然后您的div将成为

<div class="footer">
    <div class="footer-inner">
        <div class="footer-content">
            <p>@BranchCircuits_Web.Infrastructure.Utility.AppInfo.EntityDatabaseServer
        \@BranchCircuits_Web.Infrastructure.Utility.AppInfo.EntityDatabaseCatalog</p>
        </div>
    </div>
</div>