# Preferred way of load application settings in f#

**URL:** <https://forums.fsharp.org/t/preferred-way-of-load-application-settings-in-f/1497>\
**Category:** General\
**Tags:** beginners, cloud\
**Created:** [October 22, 2020, 8:13am UTC](https://forums.fsharp.org/t/preferred-way-of-load-application-settings-in-f/1497 "2020-10-22T08:13:05Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![RicoSaupe](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ricosaupe/32/1092_2.png) [@RicoSaupe](https://forums.fsharp.org/u/RicoSaupe)\
**Post date:** [October 22, 2020, 8:13am UTC](https://forums.fsharp.org/t/preferred-way-of-load-application-settings-in-f/1497/1 "2020-10-22T08:13:05Z")

</div>

Hi

I have library using netstandard 2 and consuming it from a dotnet core 3.1 app and want to get some settings from settings.json or app.config. How do i do that in F#? What is the preferred way here?

I heard about

1. using using the configuration builder (like c#), it needs the package FsConfig

> type Config = {  
> processId:int  
> }  
> let configurationRoot =  
> ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())  
> .AddJsonFile(“settings.json”).Build()  
> let result = appConfig.Get ()

this is somehow giving me this error

System.IO.FileLoadException: ‘Could not load file or assembly ‘TypeShape, Version=8.0.1.0, Culture=neutral, PublicKeyToken=null’. The located assembly’s manifest definition does not match the assembly reference. (0x80131040)’

1. using a typeprovider Fsharp.Configuration [FSharp.Configuration](https://fsprojects.github.io/FSharp.Configuration/) this looks promising but in my dotnet core app it wouldnt work.

> open FSharp.Configuration  
> type Settings = AppSettings\<“app.config”\>  
> Settings.FlagsFileSource

Thats what i am getting when i want to access the settings, last line above

System.IO.FileNotFoundException: ‘Could not load file or assembly ‘System.Configuration.ConfigurationManager, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51’. Das System kann die angegebene Datei nicht finden.’

I added this package “System.Configuration.ConfigurationManager,”

then i got this?

System.TypeLoadException: ‘Could not load type ‘System.Web.Hosting.HostingEnvironment’ from assembly ‘System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’.’

Any hints?

Looks like with the new alpha2 version of FSharp.Configuration its working.

Anyway, is this the way to go in F#?

---

<div class="post-metadata">

**Author:** ![pblasucci](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/pblasucci/32/788_2.png) [@pblasucci](https://forums.fsharp.org/u/pblasucci)\
**Post date:** [October 23, 2020, 7:22am UTC](https://forums.fsharp.org/t/preferred-way-of-load-application-settings-in-f/1497/2 "2020-10-23T07:22:35Z")

</div>

Do you want to consume the settings in the _library_ or in the _application_?

If the latter, I would just use the classes in Microsoft.Extensions.Configuration (i.e. the standard configuration and options classes documented here: [https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1) … don’t let the “[ASP.NET](http://ASP.NET)” fool you – works perfectly well for non-web applications, too).

if the former, it’s somewhat harder to make a recommendation without some more context (e.g. into what envirnments will it be deployed, and so on).

---

<div class="post-metadata">

**Author:** ![RicoSaupe](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ricosaupe/32/1092_2.png) [@RicoSaupe](https://forums.fsharp.org/u/RicoSaupe)\
**Post date:** [October 23, 2020, 7:43am UTC](https://forums.fsharp.org/t/preferred-way-of-load-application-settings-in-f/1497/3 "2020-10-23T07:43:12Z")

</div>

Hi @pblasucci

I guess I have some settings in my library that i definetely need but wouldnt want to hard code.  
My current solution is to use FSharp.Configuration in the latest alpha version. That works now. I have an app.config in my library project to satisfy the type provider and an app.config in my f# application to get the actual values from.

So for now that works. What i dont know is how i refresh the type provider once that has loaded some values. I posted this question here too.

Though I’ll give that Microsoft.Extensions.Configuration option another shot. Since I know that from my c# web project past.

Would you say that the type provider is the f#est way? Dont mind my asking I am a newbie in f#. 🙂

---

<div class="post-metadata">

**Author:** ![pblasucci](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/pblasucci/32/788_2.png) [@pblasucci](https://forums.fsharp.org/u/pblasucci)\
**Post date:** [October 23, 2020, 7:55am UTC](https://forums.fsharp.org/t/preferred-way-of-load-application-settings-in-f/1497/4 "2020-10-23T07:55:16Z")

</div>

Opinions will, of course, vary, but…

I’d say the “F#est way” is whatever gets the job done in a way that you (or your team) feel comfortable maintaining. Could be a type provider. Could be a 3rd-party library. All depends. But absent any other criteria to help “narrow the field”, just using whatever is in the .NET Standard Library is a _safe default_.

Hope this helps. 😊

---

<div class="post-metadata">

**Author:** ![rbauduin](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/rbauduin/32/314_2.png) [@rbauduin](https://forums.fsharp.org/u/rbauduin)\
**Post date:** [October 23, 2020, 1:25pm UTC](https://forums.fsharp.org/t/preferred-way-of-load-application-settings-in-f/1497/5 "2020-10-23T13:25:52Z")

</div>

In case this might help you, I’m currently loading JSON config with [FSharp.Data](https://fsharp.github.io/FSharp.Data/)’s JSON provider. [Here](https://gitlab.com/myowndb/myowndb/-/blob/1e38ca07fa838fabe293cd98c34ca593b3cafc77/lib/AppConfig.fs) is the code.

---

<div class="post-metadata">

**Author:** ![ploeh](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ploeh/32/39_2.png) [@ploeh](https://forums.fsharp.org/u/ploeh)\
**Post date:** [October 28, 2020, 8:31am UTC](https://forums.fsharp.org/t/preferred-way-of-load-application-settings-in-f/1497/6 "2020-10-28T08:31:09Z")

</div>

The following has nothing to do with F#, but is just a general observation regarding good API design.

If you read configuration values from a configuration system from within a library, you’ve now coupled the library to the configuration system. That coupling is implicit, because you can compile calling code which will fail at run time (if the configuration file isn’t present).

It makes it harder to use the library. It makes it harder to unit test the library.

A better option is to design a library’s API so that it takes a set of options as input. Then the caller can supply the configuration options read from whichever source it wants.

With this principle, you can’t compile the calling code if you don’t supply the configuration values.

---

<div class="post-metadata">

**Author:** ![RicoSaupe](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ricosaupe/32/1092_2.png) [@RicoSaupe](https://forums.fsharp.org/u/RicoSaupe)\
**Post date:** [October 28, 2020, 8:47am UTC](https://forums.fsharp.org/t/preferred-way-of-load-application-settings-in-f/1497/7 "2020-10-28T08:47:20Z")

</div>

Hello Mark.

Thanks for the hint. Thats actually what I’d like to do in the end. The thing is that I am still learning the concepts and coming from C# the config approach is more natural in the first place.  
Can you point to me to a good example of your approach? Greatly appriciate that.

---

<div class="post-metadata">

**Author:** ![ploeh](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ploeh/32/39_2.png) [@ploeh](https://forums.fsharp.org/u/ploeh)\
**Post date:** [October 28, 2020, 9:04am UTC](https://forums.fsharp.org/t/preferred-way-of-load-application-settings-in-f/1497/8 "2020-10-28T09:04:13Z")

</div>

What I wrote also applies to C#, and most other languages that I’m aware of.

If, for example, you need an integer configuration value, design your functions so that they take an integer value as a parameter. In F#, it often pays to make the configuration values the leftmost parameters, because that means that callers can partially apply them.

If you need to pass around many configuration values, you can declare a type that encapsulates them.

If you can provide some example code that illustrates what you’re trying to do, I’ll be happy to give an example.

---

<div class="post-metadata">

**Author:** ![rbauduin](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/rbauduin/32/314_2.png) [@rbauduin](https://forums.fsharp.org/u/rbauduin)\
**Post date:** [October 31, 2020, 8:55am UTC](https://forums.fsharp.org/t/preferred-way-of-load-application-settings-in-f/1497/9 "2020-10-31T08:55:36Z")

</div>

I’d like to know how what I did can be improved in regard of your comment @ploeh.  
In my library I have an [AppConfig module](https://gitlab.com/myowndb/myowndb/-/blob/master/lib/AppConfig.fs) using the JSON type provider and defining functions to access the config values because the type is [hidden in AppConfig.fsi](https://gitlab.com/myowndb/myowndb/-/blob/master/lib/AppConfig.fsi). The application can then [load the file](https://gitlab.com/myowndb/myowndb/-/blob/master/program/Program.fs#L67) and subsequently [pass the object loaded](https://gitlab.com/myowndb/myowndb/-/blob/master/program/Program.fs#L75) by the AppConfig module to [functions and types](https://gitlab.com/myowndb/myowndb/-/blob/master/lib/DB.fs#L26).  
My library code is clearly coupled in the way you described. I experience the downside of the coupling because for running my tests I am now required to have a json configuration file. How would you fix this?

---

<div class="post-metadata">

**Author:** ![ploeh](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ploeh/32/39_2.png) [@ploeh](https://forums.fsharp.org/u/ploeh)\
**Post date:** [October 31, 2020, 1:32pm UTC](https://forums.fsharp.org/t/preferred-way-of-load-application-settings-in-f/1497/10 "2020-10-31T13:32:30Z")

</div>

I don’t know that particular code base, but from glancing over it, it looks to me as though the part that’s causing you grief is a symptom of what’s almost always a fundamental problem in FP: impurity.

While you can load the configuration from disk with the `load` action, that’s the _only_ option for creating a value of the type `t` (why is it called `t`, by the way?). That action is impure.

I would suggest that providing a pure function that can create a value of the the type `t` would make things much simpler.

---

<div class="post-metadata">

**Author:** ![rbauduin](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/rbauduin/32/314_2.png) [@rbauduin](https://forums.fsharp.org/u/rbauduin)\
**Post date:** [October 31, 2020, 5:40pm UTC](https://forums.fsharp.org/t/preferred-way-of-load-application-settings-in-f/1497/11 "2020-10-31T17:40:28Z")

</div>

thanks for your advice, I’ll take a shot at it.  
This is a project I’ve been learning F# with, and (I hope) my FP approach improves over time. I think the `t` naming comes from OCaml code I’ve seen where the hidden type was called `t`.

---

<div class="post-metadata">

**Author:** ![ratsclub](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ratsclub/32/1191_2.png) [@ratsclub](https://forums.fsharp.org/u/ratsclub)\
**Post date:** [April 19, 2023, 10:55pm UTC](https://forums.fsharp.org/t/preferred-way-of-load-application-settings-in-f/1497/13 "2023-04-19T22:55:38Z")

</div>

I’m not sure if this forum is against necrobumping, but I came to the following conclusion after some time researching how to do it on `ASP.NET` (this code is using Giraffe, however it should be mostly the same for other _frameworks_):

_Note that this is using the `FsConfig` package._

1. Declare the `Configuration` type and module somewhere.

```plaintext
open System.IO
open FsConfig
open Microsoft.Extensions.Configuration

type Configuration =
    { SuperSecret: string }

module Configuration =
    let init =
        let configurationRoot =
            ConfigurationBuilder()
                .AddEnvironmentVariables()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("config.json")
                .Build()

        let appConfig = AppConfig(configurationRoot)
        appConfig.Get<Configuration>()

```

1. Inject the configuration on your `IServiceCollection`

```plaintext
let configureServices (services: IServiceCollection) =
    let config =
        match Configuration.init with
        | Ok config -> config
        | Error e -> failwithf "Error reading configuration: %A" e

    services.AddSingleton<Configuration>(config) |> ignore
    services.AddGiraffe() |> ignore

```

1. Consume the configuration

```plaintext
let handler : HttpHandler =
    fun (next: HttpFunc) (ctx: HttpContext) ->
        let config = ctx.GetService<Configuration>()
        let msg = sprintf "config: %s" config.SuperSecret
        text msg next ctx

```

---

<div class="post-metadata">

**Author:** ![RicoSaupe](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ricosaupe/32/1092_2.png) [@RicoSaupe](https://forums.fsharp.org/u/RicoSaupe)\
**Post date:** [April 20, 2023, 6:16am UTC](https://forums.fsharp.org/t/preferred-way-of-load-application-settings-in-f/1497/14 "2023-04-20T06:16:58Z")

</div>

Thanks @ratsclub I like this clean approach.
