默认值用于 My.Settings,而不是 app.config 中的默认值(对于 Windows 服务)

发布于 2024-10-03 21:19:53 字数 652 浏览 5 评论 0原文

为了简单起见,用 VB.NET 编写的 Windows 服务使用 My.Settings 命名空间。只有三个设置需要读取,并且这些设置是在 ServiceLauncher 的构造函数中读取的。

我正在尝试这样安装服务:

installutil GID.ServiceLauncher.exe

这是成功的,但是它使用的配置设置不是 GID.ServiceLauncher.exe.config 文件中的设置,而是使用应用程序中烘焙的设置作为默认设置在 Settings.Designer.vb 中(用 DefaultSettingValueAttribute 标记)。 [微软不允许开发人员忽略默认设置的可疑智慧完全是另一个问题]

我如何进一步诊断此问题,并可能强制重新加载设置?我尝试调用 My.Settings.Default.Reload,但这没有任何作用。所有设置都是应用程序设置,与自动生成的文件中的设置仅“值”不同。

我已使用 System.Diagnostics.Debugger.Launch() 成功附加调试器,并且确实如此,这些设置仍然是默认设置。

预期问题,背景:之所以需要配置设置是因为这是一个非常简单的服务,只需执行一个exe;并且此 exe 位于可配置位置。还有其他原因,例如我希望无需重新编译即可配置服务名称。

A Windows service written in VB.NET is using the My.Settings namespace for simplicity. There are only three settings to read, and these are read within the constructor of the ServiceLauncher.

I am attempting to install the service as such:

installutil GID.ServiceLauncher.exe

And this is successful, however the config settings it is using are not the ones within the GID.ServiceLauncher.exe.config file, instead it is using the ones baked into the app as Default Settings within Settings.Designer.vb (marked with DefaultSettingValueAttribute). [The questionable wisdom of Microsoft not allowing a developer to ignore default settings is another question entirely].

How can I further diagnose this issue, and maybe force a reload of settings? I tried calling My.Settings.Default.Reload, however this did nothing. All settings are application settings, and only differ by "value" from those in the auto generated file.

I have successfully attached the debugger using System.Diagnostics.Debugger.Launch() and true enough, the settings are still the default settings.

In anticipation of the question, the background: The reason for requiring configuration settings is because this is a very straightforward service that simply executes an exe; and this exe is in configurable location. There are other reasons also, such as I wish to have the service name configurable without recompiling.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧话新听 2024-10-10 21:19:53

我发现,由于安装程序与 InstallUtil.exe 在同一进程中运行,因此永远找不到 Windows 服务的配置文件。
类似的文章 msdn 上

因此,我受 this。请参阅下面的新代码:

Friend Function GetConfigurationValue(ByVal key As String) As String
    Dim service = System.Reflection.Assembly.GetAssembly(GetType(ProjectInstaller))
    Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(service.Location)
    Dim keyValue As String = config.AppSettings.Settings(key).Value

    If String.IsNullOrEmpty(keyValue) Then
        Throw New IndexOutOfRangeException(String.Format("Settings collection does not contain the requested key:[{0}]", key))
    End If

    Return keyValue
End Function

替代解决方案:

将参数传递到 InstallUtil
Daenibuq(包装 System.ServiceProcess 的另一个抽象)

I discovered that because the installer runs in the same process as InstallUtil.exe the config file is never found for a Windows Service.
Similar article here on msdn

Therefore I rolled my own simple solution inspired by this. See below for new code:

Friend Function GetConfigurationValue(ByVal key As String) As String
    Dim service = System.Reflection.Assembly.GetAssembly(GetType(ProjectInstaller))
    Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(service.Location)
    Dim keyValue As String = config.AppSettings.Settings(key).Value

    If String.IsNullOrEmpty(keyValue) Then
        Throw New IndexOutOfRangeException(String.Format("Settings collection does not contain the requested key:[{0}]", key))
    End If

    Return keyValue
End Function

Alternative solutions:

Passing parameters in to InstallUtil
Daenibuq (another abstraction on wrapping System.ServiceProcess)

为人所爱 2024-10-10 21:19:53

当您访问设置时,您是否编写My.Settings.Default.MySetting?如果是这样,请尝试将其更改为 My.Settings.MySetting

When you're accessing the settings do you write My.Settings.Default.MySetting? If so, try changing that to My.Settings.MySetting.

梦幻之岛 2024-10-10 21:19:53

您是否手动更改了 app.config 文件中的设置。如果是这样,只有当这些更改是应用程序设置时才会生效,而不是用户设置。

因此,简单的解决方法是将您的设置范围更改为 Application

更新

检查您的文件访问权限。运行服务的帐户是否有权访问配置文件?请参阅此相关答案

Have you manually changed the settings in the app.config file. If so, these changes will only be picked up if they are Application settings - not User settings.

So the easy fix is to change your settings scope to Application.

Update

Check your file access permissions. Does the account that the service is running under have permission to access the config file? See this related SO answer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文