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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With