App.config and F# Interactive not working

前端 未结 6 1297
陌清茗
陌清茗 2020-12-17 10:53

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 w

6条回答
  •  时光取名叫无心
    2020-12-17 11:15

    F# Interactive can work with executables that rely on app.config files.

    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
    

    then in your script file reference the executable and #load the .fs file so:

    #I "../Build/Path

    #r "ConfiguredApp.exe"

    #load "MyConfig.fs"

    On executing these three lines you will see a message similar to the following in the FSI window:

    [Loading C:\Svn\trunk\Source\ConfiguredApp\MyConfig.fs]

    Binding session to 'C:\Svn\Qar\trunk\Build\Path\ConfiguredApp.exe'...

    Notice that you're actually referencing the app.config when in FSI (rather than the generated .exe.config.)

    Best of luck...

提交回复
热议问题