对 Windows 服务和 GUI 使用相同的 app.config
我已经运行了 Windows 服务,它使用它的 app.config 来获取一些重要的值。现在我想提供一个不同的 GUI 应用程序,它提供了一种更改这些重要值并将其保存到相同的 app.config 的方法。
我的问题是:是否可以通过使用“添加为链接”在项目之间共享此 app.config,如果我随后使用 GUI 应用程序并更改一些值,这是否会反映在 Windows 服务中?
编辑: 如果这有效,那么也许有人也知道从更技术的角度来看链接如何工作的细节?
I have have running windows service that is using it's app.config for getting some important values. Now I would like to provide a different GUI application that provide a way to change these important values and save it to the same app.config.
My question is this: will it be possible to share this app.config between the projects by using "Add as a link" and if I then use my GUI application and change some values, will this be reflected in the windows service?
EDIT:
If this works, then perhaps someone also know the details of how the linking works in a more technical view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这两个项目可以共享相同的
app.config
。不确定链接,但您可以在两个不同的配置中具有相同的结构和值,但在部署它时,将其部署在与 Windows 服务相同的文件夹中。现在,关于更新
app.config
的值,如果win服务正在运行,它们将不会反映在Windows服务中。为此,您需要重新启动 Windows 服务。因为应用程序设置缓存在内存中,并在应用程序启动时加载到内存中。您可以使用ConfigurationManager.RefreshSection
刷新内存中加载的配置。您可以在 MSDN 上阅读相关内容。您必须在 Windows 服务中执行此操作。希望这些信息对您有帮助。
Both the projects can share the same
app.config
. Not sure about linking, but you can have same structure and values in two different config, but when deploying it, deploy it in same folder as windows service.Now regarding updating the values of
app.config
, they will not get reflected in windows service, if win service is running. You will need to restart the windows service for this. Because app settings are cached in memory, and loaded in memory when app starts. You can useConfigurationManager.RefreshSection
to refresh the loaded config in memory. You can read about it on MSDN. You will have have to do this in your windows service.Hope this info helps you.
如果您的 GUI 应用程序不依赖于 app.config 中的任何值(即只需编辑它),并且您计划从与服务相同的文件夹启动 GUI 应用程序,您可以像打开任何(文本/xml)文件一样打开它(甚至可能包括文件打开对话框)。
根据您的 VCS,您可以将 app.config 从 Windows 服务项目“链接”到 GUI 应用程序项目 - 我个人使用 SubVersion 会使用 外部。
If your GUI application doesn't rely on any values from the app.config (i.e. just edit it) and you plan to launch your GUI application from the same folder as your service you could just open it like any (text/xml)file (maybe even include a file open dialogue).
Depending on your VCS you can "link" the app.config from your windows service project to your GUI application project - I, personally using SubVersion would do this with Externals.
一般来说,从“Windows 服务”调用的应用程序中有三分之一使用 Windows 应用程序项目部署的配置文件。因此,当前配置取决于启动项目(可能是 wf、wpf、web...)。
附加配置文件即可
只需记住设置要使用应用程序“属性”->“部署”的 。复制到输出文件夹 ->始终复制
如果您查看已部署的应用程序目录,这种行为就很容易理解。它包含所有项目的 dll 以及配置为复制到最终文件夹的所有其他文件。默认情况下,每个项目对这些文件都具有相同的访问权限,因为它们都位于同一位置。
但请注意 Amar Palsapure 关于 appconfig 数据如何缓存的说法。
generally speaking, every third called app from the 'windows service' uses configuration files deployed by windows application project. Thanks to that, current configuration depends on the starting project (could be wf, wpf, web...).
just remember to set the additional config files to be deployed with the application
Properties -> Copy to output folder -> Copy Always
This behavior is quite easy to understand if you look into the deployed application directory. It contains all the projects' as dlls and all the other files configured to be copied to the final folder. By default each project has the same access to these files as they are all in the same location.
But be carreful about what Amar Palsapure says about how appconfig data are caching.