Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App.config and F# Interactive not working

Tags:

.net

f#

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.

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?

With best regards

like image 691
leen Avatar asked Mar 14 '09 08:03

leen


People also ask

What is app config?

App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.

What is app config and web config?

Web. Config is used for asp.net web projects / web services. App.Config is used for Windows Forms, Windows Services, Console Apps and WPF applications.

Where is app config file?

The application configuration file usually lives in the same directory as your application. For web applications, it is named Web. config. For non-web applications, it starts life with the name of App.

What does app config do in Flask?

When your app is initialized, the variables in config.py are used to configure Flask and its extensions are accessible via the app. config dictionary – e.g. app. config["DEBUG"] . Configuration variables can be used by Flask, extensions or you.


1 Answers

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...

like image 62
Daniel Asher Avatar answered Oct 19 '22 03:10

Daniel Asher