EWS:NetworkCredential与F#中的ExchangeCredentials不兼容

时间:2016-01-23 11:58:31

标签: f# exchange-server exchangewebservices type-systems

我喜欢使用Microsoft.Exchange.WebService API:

C#工作正常

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

F#给出错误:“NetworkCredential”类型与“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

我认为它与C#API中定义的隐式转换有关。所以我尝试了(:&gt;)和downcast(:?&gt;)。我尝试使它明确(新的NetworkCredential ...:ExchangeCredentials),我已经检查了我在C#nuget中直接使用的引用dll和F#paket。 两者都在VS 2015中进行了测试。在C#中它是.Net 4.5.2和F#,如果它是在app.config中查找它的正确方法

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

我猜使用WebCredentials不是正确的方法。我想使用SecureString而不是字符串,如果在C#中工作。因此,我更有可能对我想要理解的F#语法做错了。

1 个答案:

答案 0 :(得分:7)

正如您所注意到的,ExchangeCredentials定义了从NetworkCredentialsExchangeCredentials的隐式转换,这就是您的代码在C#中正常运行的原因。请注意,这两件事之间没有继承关系,因此您无法使用upcast(:>)和downcast(:?>)运算符。

隐式转换在F#中显示为名为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