When I use typesafe application settings, I have something like this in my app.exe.config file:
当我使用类型安全的应用程序设置时,我的app.exe.config文件中有类似的东西:
<setting name="IntervalTimeout" serializeAs="String">
<value>10</value>
</setting>
The Settings.Designer.vb does something like this:
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
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.
从外部监控此设置的更改的最佳做法是什么。即该设置由作为服务运行的应用程序使用,当某人打开相应的配置文件并将IntervalTimeout更改为30时,服务应用程序会收到通知并执行我告诉它要重新配置的任何内容。
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.
然后实现一个Eventhandler,它检查已更改的属性的名称并做出适当的反应。
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. ;-)
但是,PropertyChanged事件仅在属性上调用Set方法时触发,而不是在有人直接在文件中更改数据时触发。当然这可能是天堂,但可以理解的是,这不是它的工作方式。 ;-)
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.
最有可能的是,我实现了一个filesystemwatcher来监视文件的变化,然后重新加载配置,找出发生了什么变化,然后做任何必要的事情来反映这些变化。
Does anyone know a better way? Or even a supported one?
有谁知道更好的方法?或者甚至是受支持的人?
1 个解决方案
#1
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.
ASP.NET监视Web.config文件以查找更改。它使用System.Web.FileChangesMonitor类(标记为内部和密封),使用另一个内部类System.Web.FileMonitor。在反射器中看看它们。对你的问题来说可能太复杂了,但如果我有类似的要求,我肯定会调查它。
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?
这看起来很复杂。请记住,ASP.NET会在检测到更改后关闭旧应用程序并启动新应用程序。 App.config未构建为在应用程序运行时检测更改。也许重新考虑为什么需要这个?
An easier approach is to restart the service once you've made the change.
更简单的方法是在进行更改后重新启动服务。
#1
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.
ASP.NET监视Web.config文件以查找更改。它使用System.Web.FileChangesMonitor类(标记为内部和密封),使用另一个内部类System.Web.FileMonitor。在反射器中看看它们。对你的问题来说可能太复杂了,但如果我有类似的要求,我肯定会调查它。
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?
这看起来很复杂。请记住,ASP.NET会在检测到更改后关闭旧应用程序并启动新应用程序。 App.config未构建为在应用程序运行时检测更改。也许重新考虑为什么需要这个?
An easier approach is to restart the service once you've made the change.
更简单的方法是在进行更改后重新启动服务。