检索应用程序启动期间添加的应用程序设置时出现问题
我似乎遇到了一个奇怪的问题,在我的 Application_Start() 的 global.asax 中,我有一些东西会进入我的数据库,从名称/值表中获取我的所有应用程序设置,然后将它们放入通过 Application.Add(name,value)
应用程序。
我在另一个项目中有一个“应用程序外观”,由我的服务层、数据层等使用,以便获取我需要执行各种操作的设置。
在我的数据库中,我有几个条目:
ConfigName | ConfigValue
WebServiceUsername | myUsername
WebServicePassword | myPassword
因此,在我的方法中,我从数据库中获取这些值,并将它们放入我的应用程序中:
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);
}
}
这就是我稍后从应用程序中调用该值的方式:
public static string WebServiceUsername
{
get { return WebConfigurationManager.AppSettings["WebServiceUsername"]; }
}
这这就是事情变得奇怪的地方。
如果我通过以下方式从 Web 层调用应用程序外观:
<%= ApplicationFacade.WebServiceUsername %>
我什么也得不到(是的,我在 get 方法中只尝试了 ConfigurationManager!)。
但这是奇怪的事情...
如果我手动将应用程序密钥放入我的 web.config 文件...
<appSettings>
<add key="putz" value="mash"/>
</appSettings>
然后当我在视图中进行调用时,将类似的属性构建到我的 ApplicationFacade 类中(<%= ApplicationFacade.Putz %>
) 我得到“mash
”返回。
所以,我知道我的 ApplicationFacade 工作正常。那么也许这是我在 application_start() 中的代码?
好吧,如果我将其放入视图 <%=Application["WebServiceUsername"]%>
中,则会返回 myUsername
。
什么给?!
回答
ConfigurationManager.AppSettings.Set(appConfig.ConfigName,appConfig.ConfigValue);
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"]; }
}
This is where things get weird.
If I call the application facade from my web layer with this:
<%= ApplicationFacade.WebServiceUsername %>
I get nothing back (yes, I have tried just ConfigurationManager in the get method!).
But this is the odd thing...
If I manually put an application key into my web.config file...
<appSettings>
<add key="putz" value="mash"/>
</appSettings>
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.
So, I know my ApplicationFacade is working correctly. So maybe it's my code in the application_start()?
Well, if I put this in my view <%=Application["WebServiceUsername"]%>
, myUsername
is returned.
What gives?!
Answer
ConfigurationManager.AppSettings.Set(appConfig.ConfigName,appConfig.ConfigValue);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
Application_Start
中,当您引用Application
对象时,这实际上是 HttpApplicationState 用于在内存中保存应用程序特定的设置,与 web.config 中存储的键/值 appSettings 无关。WebConfigurationManager.AppSettings["someKey"]
时,这将返回与 web.config 的appSettings
部分中的someKey
相对应的值。Application["someKey"]
时,这将返回一个缓存在应用程序实例中的值。两者完全无关,您不能指望使用
WebConfigurationManager.AppSettings["someKey"]
读取存储在Application["someKey"]
中的值。In
Application_Start
when you refer to theApplication
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"]
this will return the value corresponding tosomeKey
in theappSettings
section of web.config.Application["someKey"]
this will return a value cached in the application instance.Both are completely unrelated and you can't expect to read values stored in
Application["someKey"]
withWebConfigurationManager.AppSettings["someKey"]
.