[英]F# and WCF consumption
我使用标准的“添加服务引用”对话框在C#中创建了一个库,但是在其中实现的方法返回的无效,因此我无法在异步工作流程中将其绑定到它们。 我不能将接口与Begin * End *一起使用,因为每种方法都有不同数量的参数,所以它说“参数错误计数”(类似这样)。 那么,如何才能异步使用WCF服务(异步,因为它打算在一切都是异步的Silverlight中使用)?
我尚不清楚绊脚石是什么,但是这个过程应该是
FromBeginEnd
一起使用以转换为异步: http : //msdn.microsoft.com/zh-cn/library/ee340508.aspx void
( unit
),那么您将使用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.