Search code examples
f#auth0

Casting a dynamic type in C# to F#


I am working through Auth0's documentation here and porting the C# code to F#

In the C# code, there is this line:

var auth0LoginResult = await _auth0Client.LoginAsync(new { audience = AuthenticationConfig.Audience });

Where LoginAsync's parameter is called extraParameters of type object.

Audience is just a string, but when I try and pass in a string like this

let extraParameters = Support.authenticationConfig.Audience
let result = client.LoginAsync(extraParameters).Result

it fails with a

System.Reflection.TargetParameterCountException: Number of parameters specified does not match the expected number.

How can I pass in a type that the method likes? Just cast it to object?


Solution

  • I guess that the library takes an object and uses reflection to look at the members of the object, which is why the C# way of calling it is to give it an anonymous object as an argument.

    In F#, you probably need to define an explicit record type with the parameters you need to pass as fields, although in the near future, you will likely be able to use anonymous records for this.

    I have not tested this, but I would try something like this:

    type LoginParameters = { audience : string }
    
    async { 
      let! result = 
        auth0Client.LoginAsync({ audience = AuthenticationConfig.Audience }) 
        |> Async.AwaitTask
      (...) }