簡體   English   中英

F#和WCF消耗

[英]F# and WCF consumption

我使用標准的“添加服務引用”對話框在C#中創建了一個庫,但是在其中實現的方法返回的無效,因此我無法在異步工作流程中將其綁定到它們。 我不能將接口與Begin * End *一起使用,因為每種方法都有不同數量的參數,所以它說“參數錯誤計數”(類似這樣)。 那么,如何才能異步使用WCF服務(異步,因為它打算在一切都是異步的Silverlight中使用)?

我尚不清楚絆腳石是什么,但是這個過程應該是

  • 讓svcutil生成異步接口(使用Begin / End方法)
  • 將這些方法與FromBeginEnd一起使用以轉換為異步: http : //msdn.microsoft.com/zh-cn/library/ee340508.aspx
  • 請注意,如果該方法返回voidunit ),那么您將使用do! 而不是let! 在異步工作流程中

要回答您的問題的一部分: “如果Begin函數接受一些參數,如何使用FromBeginEnd?”

您可以通過關閉魔術來做到這一點。 這是一些例子。 如您所見,擴展方法采用參數,但是將匿名函數傳遞給Async.FromBeginEnd該匿名函數與FromBeginEnd期望的簽名匹配。 額外的參數在閉包中捕獲,並傳遞到匿名函數內部的真實BeginXyz

您還可以使用FromBeginEnd的重載,該重載FromBeginEnd獲取其他參數,然后最后指向Begin / End函數的指針,就像我在下面的AsyncGetReadStream方法中所做的AsyncGetReadStream -但是當BeginXyz有多個重載時,我很難將其保存到工作,所以我大多數都使用閉包。

open System
open System.Data.Services.Client

type System.Data.Services.Client.DataServiceContext with

    member this.AsyncExecute<'a> (uri:Uri) =
        Async.FromBeginEnd((fun (cb, state) -> this.BeginExecute<'a>(uri, cb, state)), 
                           (fun iar -> this.EndExecute<'a>(iar) :?> QueryOperationResponse<'a>))

    member this.AsyncExecute<'a> (continuation:DataServiceQueryContinuation<'a>) =
        Async.FromBeginEnd((fun (cb, state) -> this.BeginExecute<'a>(continuation, cb, state)), 
                           (fun iar -> this.EndExecute<'a>(iar) :?> QueryOperationResponse<'a>))

    member this.AsyncExecuteBatch ([<ParamArray>] queries : DataServiceRequest[]) =
        Async.FromBeginEnd((fun (cb, state) -> this.BeginExecuteBatch(cb, state, queries)), this.EndExecuteBatch)

    member this.AsyncLoadProperty (entity:obj, propertyName:string) =
        Async.FromBeginEnd((fun (cb, state) -> this.BeginLoadProperty(entity, propertyName, cb, state)), 
                           this.EndLoadProperty)

    member this.AsyncLoadProperty (entity:obj, propertyName:string, continuation:DataServiceQueryContinuation) =
        Async.FromBeginEnd((fun (cb, state) -> this.BeginLoadProperty(entity, propertyName, continuation, cb, state)), 
                           this.EndLoadProperty)

    member this.AsyncLoadProperty (entity:obj, propertyName:string, nextLinkUri:Uri) =
        Async.FromBeginEnd((fun (cb, state) -> this.BeginLoadProperty(entity, propertyName, nextLinkUri, cb, state)), 
                           this.EndLoadProperty)

    member this.AsyncSaveChanges () =
        Async.FromBeginEnd(this.BeginSaveChanges, this.EndSaveChanges)

    member this.AsyncSaveChanges (options:SaveChangesOptions) =
        Async.FromBeginEnd((fun (cb, state) -> this.BeginSaveChanges(options, cb, state)), 
                           this.EndSaveChanges)

    member this.AsyncGetReadStream (entity:obj, args:DataServiceRequestArgs) =
        Async.FromBeginEnd(entity, args, this.BeginGetReadStream, this.EndGetReadStream)


type System.Data.Services.Client.DataServiceQuery with

    member this.AsyncExecute () =
        Async.FromBeginEnd(this.BeginExecute, this.EndExecute)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM