Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EWS: NetworkCredential not compatible with ExchangeCredentials in F#

I like to use the Microsoft.Exchange.WebService API:

C# works fine

ExchangeService service = new ExchangeService(userData.Version);
service.Credentials = new NetworkCredential(userData.EmailAddress, userData.Password);

F# gives the error: The type 'NetworkCredential' is not compatible with 'ExchangeCredential'

open System
open Microsoft.Exchange.WebServices.Data
open System.Net

[<EntryPoint>]
let main argv =    

    let connectToService userData =
        let service = new ExchangeService(userData.Version)
        do
            service.Credentials <-  new NetworkCredential(userData.EmailAddress, userData.Password)
            service.Url <- userData.AutodicoverUrl
    0

I thought it has something to do with implicit conversion that's defined in the C# API. so I tried up (:>) and downcast (:?>). I tried to make it explicite (new NetworkCredential... :ExchangeCredentials) and I've checked the referenced dlls as I used in C# nuget directly and in F# paket. Both tested in VS 2015. In C# it's .Net 4.5.2 and in F#, too if it's the correct way to look it up in the app.config

 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />

And I guess using WebCredentials is not the correct way. I want to use a SecureString and not a string and if works in C#. So it's more likely that I did something wrong with the F# syntax that I'd like to understand.

like image 639
KCT Avatar asked Jan 23 '16 11:01

KCT


1 Answers

As you have noticed ExchangeCredentials defines an implicit conversion from NetworkCredentials to ExchangeCredentials, which is why your code functions correctly in C#. Note that there is no inheritance relationship between these two things, hence you cannot use the upcast (:>) and downcast (:?>) operators.

Implicit conversions appear in F# as a static member called op_Implicit.

let connectToService userData =
    let service = new ExchangeService(userData.Version)
    service.Credentials <- 
        NetworkCredential(userData.EmailAddress, userData.Password)
        |> ExchangeCredentials.op_Implicit // call implicit conversion
    service.Url <- userData.AutodicoverUrl
like image 73
TheInnerLight Avatar answered Sep 20 '22 18:09

TheInnerLight