检索在应用程序启动期间添加的应用程序设置时出现问题 [英] Problems retrieving appsettings added during application start

查看:109
本文介绍了检索在应用程序启动期间添加的应用程序设置时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎遇到了一个奇怪的问题,即在我的Application_Start()中的global.asax中,有一些东西进入数据库,从名称/值表中获取所有应用程序设置,然后删除通过 Application.Add(name,value)将它们导入应用程序.

I appear to be having an odd issue where, in my global.asax in my Application_Start(), I have something that goes off to my database, gets all of my app settings from a name/value table, and then drops them into the application via Application.Add(name,value).

我在另一个项目中有一个应用程序外观",我的服务层,数据层等使用它来获取设置,我需要做很多零碎的事情.

I have an 'application facade' in another project which is used by my service layers, data layers and so on in order to get settings I need to do various bits and pieces.

在我的数据库中,我有几个条目:

In my database, I have a couple of entries:

ConfigName          |  ConfigValue
WebServiceUsername  |  myUsername
WebServicePassword  |  myPassword

因此,在我的方法中,我从数据库中获取了这些值,并将其放入我的应用程序中:

So in my method, I go off and get these values from the database, and put them into my application:

protected void GetApplicationSettings()
{
  //Get all the config values out of the database, and then put them into the application keys...
  var appConfigAttributes = ApplicationConfigurationService.GetAppConfigNames();

  foreach (var appConfig in appConfigAttributes)
  {
    Application.Add(appConfig.ConfigName,appConfig.ConfigValue);
  }
}

这是我稍后在应用程序中调用值的方式:

This is how I call the value from the application at a later time:

public static string WebServiceUsername
{
  get { return WebConfigurationManager.AppSettings["WebServiceUsername"]; }
}

这是奇怪的地方.

如果我使用以下方法从Web层调用应用程序外观:

If I call the application facade from my web layer with this:

<%= ApplicationFacade.WebServiceUsername %>

我什么也没回来(是的,我已经在get方法中尝试了ConfigurationManager!).

I get nothing back (yes, I have tried just ConfigurationManager in the get method!).

但这是奇怪的事情...

But this is the odd thing...

如果我将应用程序密钥手动放入我的web.config文件中...

If I manually put an application key into my web.config file...

<appSettings>
  <add key="putz" value="mash"/>
</appSettings>

然后在视图中进行调用时,将与Putz相似的属性构建到Putz的ApplicationFacade类中(<%= ApplicationFacade.Putz%> ),我得到' mash"返回.

And then build a similar property into my ApplicationFacade class as Putz, when I do the call in the view (<%= ApplicationFacade.Putz %>) I get 'mash' returned.

所以,我知道我的ApplicationFacade工作正常.所以也许这是我在application_start()中的代码?

So, I know my ApplicationFacade is working correctly. So maybe it's my code in the application_start()?

好吧,如果我将其放在视图中<%= Application ["WebServiceUsername"]%> ,则会返回 myUsername .

Well, if I put this in my view <%=Application["WebServiceUsername"]%>, myUsername is returned.

有什么用?!

答案

ConfigurationManager.AppSettings.Set(appConfig.ConfigName,appConfig.ConfigValue);

推荐答案

Application_Start 中,当您引用 Application 对象时,它实际上是 HttpApplicationState ,用于将应用程序特定的设置保存在内存中,但没有任何内容与存储在web.config中的键/值appSettings有关.

In Application_Start when you refer to the Application object this actually is an instance of HttpApplicationState which is used to hold application specific settings in memory and has nothing to do with key/value appSettings stored in web.config.

  • 当您使用 WebConfigurationManager.AppSettings ["someKey"] 时,这将返回与Web appSettings 部分中的 someKey 相对应的值.配置.
  • 当您使用 Application ["someKey"] 时,这将返回在应用程序实例中缓存的值.
  • When you use WebConfigurationManager.AppSettings["someKey"] this will return the value corresponding to someKey in the appSettings section of web.config.
  • When you use Application["someKey"] this will return a value cached in the application instance.

两者完全无关,您不能期望使用 WebConfigurationManager.AppSettings ["someKey"] 读取存储在 Application ["someKey"] 中的值.

Both are completely unrelated and you can't expect to read values stored in Application["someKey"] with WebConfigurationManager.AppSettings["someKey"].

这篇关于检索在应用程序启动期间添加的应用程序设置时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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