监视 app.config 中设置的更改
当我使用类型安全应用程序设置时,我的 app.exe.config 文件中有类似这样的内容:
<setting name="IntervalTimeout" serializeAs="String">
<value>10</value>
</setting>
Settings.Designer.vb 执行如下操作:
<Global.System.Configuration.ApplicationScopedSettingAttribute(), _
Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), _
Global.System.Configuration.DefaultSettingValueAttribute("10")> _
Public ReadOnly Property IntervalTimeout() As Integer
Get
Return CType(Me("IntervalTimeout"),Integer)
End Get
End Property
我可以通过以下方式访问设置:
Settings.IntervalTimeout
什么是最好的从外部监视此设置的更改的做法。 即,该设置由作为服务运行的应用程序使用,当有人打开相应的配置文件并将 IntervalTimeout 更改为 30 时,服务应用程序会收到通知并执行我告诉它执行的任何操作来重新配置自身。
通常,我会在初始化期间使用类似的东西:
AddHandler Settings.PropertyChanged, New PropertyChangedEventHandler(AddressOf Settings_PropertyChanged)
然后实现一个事件处理程序来检查已更改的属性名称并做出适当的反应。
但是,仅当对属性调用 Set 方法时才会触发 PropertyChanged 事件,而当有人直接在文件中更改数据时则不会触发。 当然,这本来就是天堂,但可以理解的是,事情并非如此。 ;-)
最有可能的是,我会实现一个文件系统观察器来监视文件的更改,然后重新加载配置,找出更改的内容,然后执行任何必要的操作来反映这些更改。
有谁知道更好的方法吗? 或者甚至是受支持的一个?
When I use typesafe application settings, I have something like this in my app.exe.config file:
<setting name="IntervalTimeout" serializeAs="String">
<value>10</value>
</setting>
The Settings.Designer.vb does something like this:
<Global.System.Configuration.ApplicationScopedSettingAttribute(), _
Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), _
Global.System.Configuration.DefaultSettingValueAttribute("10")> _
Public ReadOnly Property IntervalTimeout() As Integer
Get
Return CType(Me("IntervalTimeout"),Integer)
End Get
End Property
And I can access the setting with something like this:
Settings.IntervalTimeout
What is the best practice for monitoring changes to this setting from the outside. I.e. the setting is used by an application that is running as a service and when somebody opens the corresponsing config file and changes the IntervalTimeout to let's say 30, the service application gets notified and does whatever I tell it to do to reconfigure itself.
Usually, I would have used something like this during initialization:
AddHandler Settings.PropertyChanged, New PropertyChangedEventHandler(AddressOf Settings_PropertyChanged)
And then implement an Eventhandler that checks the name of the property that has changed and reacts appropriately.
However, the PropertyChanged event only gets fired when the Set method is called on a property, not when somebody changes the data directly within the file. Of course this would have been heaven, but understandable that this is not the way it works. ;-)
Most probably, I'd implement a filesystemwatcher to monitor changes to the file and then reload the configuration, find out what changed and then do whatever is necessary to reflect those changes.
Does anyone know a better way? Or even a supported one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ASP.NET 监视 Web.config 文件的更改。 它使用 System.Web.FileChangesMonitor 类(标记为内部且密封)来执行此操作,该类使用另一个内部类 System.Web.FileMonitor。 在反光镜中看看它们。 对于您的问题来说可能太复杂,但如果我有类似的要求,我肯定会研究它。
这看起来相当复杂。 请记住,一旦检测到更改,ASP.NET 就会关闭旧应用程序并启动新应用程序。 App.config 的架构无法在应用程序运行时检测更改。 也许重新考虑一下为什么你需要这个?
一种更简单的方法是在进行更改后重新启动服务。
ASP.NET monitors the Web.config file for changes. It does this using the System.Web.FileChangesMonitor class (which is marked internal and sealed), which uses another internal class System.Web.FileMonitor. Have a look at them in reflector. It might be too complex for your problem, but I'd certainly look into it if I had a similar requirement.
This looks pretty complex. Bear in mind ASP.NET shuts down the old application and starts a new one once it detects a change. App.config wasn't architectured to detect for changes while the application is running. Perhaps reconsider why you need this?
An easier approach is to restart the service once you've made the change.