当我正在抛弃我的小宠物项目时,我正在尝试将所有常量字符串存储在我的app.config文件中(Keys,XpathExpressions等).当我运行编译的exe时,这很有效.在Interactive Shell中并非如此.
我试图将.config文件从我的bin/Release目录复制到obj/Debug&obj/Release目录,但Call to ConfigurationManager.AppSettings.Item("key")
always总是返回null.
有任何建议如何解决这个问题?
最诚挚的问候
Dan*_*her 13
F#Interactive 可以处理依赖app.config
文件的可执行文件.
这样做的方法是.fs
在项目中有一个文件,它.config
在COMPILED
define 上加载条件,所以:
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
Run Code Online (Sandbox Code Playgroud)
然后在脚本文件中引用的可执行文件,并#load
在.fs
文件中,以便:
#I "../Build/Path
#r "ConfiguredApp.exe"
#load "MyConfig.fs"
在执行这三行时,您将在FSI窗口中看到类似于以下内容的消息:
[Loading C:\Svn\trunk\Source\ConfiguredApp\MyConfig.fs]
Binding session to 'C:\Svn\Qar\trunk\Build\Path\ConfiguredApp.exe'...
请注意,您实际上是app.config
在FSI中引用了when(而不是生成的).exe.config
.
祝你好运......
Mic*_*lGG 12
虽然FSI动态生成输入代码,但使用fsi.exe.config可以正常工作.
我创建了这个文件:
<configuration>
<appSettings>
<add key="test" value="bar"/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
并将其保存为"fsi.exe.config"(程序文件\ fsharp-version\bin).
然后开始FSI:
> #r "System.configuration";;
--> Referenced 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.configuration.dll'
> System.Configuration.ConfigurationManager.AppSettings.["test"];;
val it : string = "bar"
Run Code Online (Sandbox Code Playgroud)
它也适用于Visual Studio.(但请注意,您需要将会话重置为拾取更改.)
问题在于,FSI 是一个在幕后运行的不同 exe,它通过动态编译和生成二进制文件执行一些疯狂的操作。检查FSI 认为哪个程序集正在运行。你可能会对你的发现感到惊讶:)
它会抛出一个错误:
System.NotSupportedException:动态程序集中不支持调用的成员。在 System.Reflection.Emit.AssemblyBuilder.get_Location()
您需要研究如何将 app.config 设置放入动态程序集中。这可能会很痛苦,而且可能不值得。如果它作为编译的二进制文件工作,我会测试那些依赖于 FSI 之外的配置设置的东西。
祝你好运。