App.config 和 F# Interactive 不起作用 [英] App.config and F# Interactive not working
问题描述
当我完善我的小宠物项目时,我试图将所有常量字符串存储在我的 app.config 文件(键、XpathExpressions 等)中.当我运行编译的 exe 时,这很好用.在交互式外壳中,情况并非如此.
As I'm polishing my little pet project, I'm trying to store all the constant strings in my app.config file (Keys, XpathExpressions etc). When I run the compiled exe this works great. In the Interactive Shell this isn't the case.
我试图将 .config 文件从我的 bin/Release 目录复制到 obj/Debug &obj/Release 目录,但对 ConfigurationManager.AppSettings.Item("key")
的调用总是返回 null.
I tried to copy the .config file from my bin/Release directory to the obj/Debug & obj/Release dirs, but the Call to ConfigurationManager.AppSettings.Item("key")
always returns null.
有什么建议可以解决这个问题吗?
Any suggestions how to fix this?
致以最好的问候
推荐答案
F# Interactive 可以处理依赖于 app.config
文件的可执行文件.
F# Interactive can work with executables that rely on app.config
files.
这样做的方法是在你的项目中有一个 .fs
文件,它以 COMPILED
定义为条件加载你的 .config
:
The way to do this is to have an .fs
file in your project that loads your .config
conditional on the COMPILED
define so:
let GetMyConfig() =
let config =
#if COMPILED
ConfigurationManager.GetSection("MyConfig") :?> MyConfig
#else
let path = __SOURCE_DIRECTORY__ + "/app.config"
let fileMap = ConfigurationFileMap(path)
let config = ConfigurationManager.OpenMappedMachineConfiguration(fileMap)
config.GetSection("MyConfig") :?> MyConfig
#endif
然后在您的脚本文件中引用可执行文件和 #load
.fs
文件,以便:
then in your script file reference the executable and #load
the .fs
file so:
#I "../Build/Path
#r "ConfiguredApp.exe"
#load "MyConfig.fs"
执行这三行后,您将在 FSI 窗口中看到类似于以下内容的消息:
On executing these three lines you will see a message similar to the following in the FSI window:
[加载 C:Svn runkSourceConfiguredAppMyConfig.fs]
将会话绑定到'C:SvnQar runkBuildPathConfiguredApp.exe'...
请注意,您实际上在 FSI 中引用了 app.config
(而不是生成的 .exe.config
.)
Notice that you're actually referencing the app.config
when in FSI (rather than the generated .exe.config
.)
祝你好运...
这篇关于App.config 和 F# Interactive 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!