【问题标题】:F# and WCF consumptionF# 和 WCF 消耗
【发布时间】:2011-07-04 15:30:52
【问题描述】:

我在 C# 中使用标准的“添加服务引用”对话框创建了一个库,但其中实现的方法返回 void,因此我无法在异步工作流中绑定到它们。我不能将接口与 Begin* End* 一起使用,因为每个方法都有不同数量的参数,所以它说“错误的参数计数”(类似的东西)。那么如何异步使用 WCF 服务(异步,因为它旨在用于一切都是异步的 silverlight)?

【问题讨论】:

  • "return void 所以我不能在异步工作流中绑定它们" - 你是什么意思?
  • 我的意思是如何使用 let 获得结果!
  • 什么结果?他们返回无效?
  • 当使用 svcutil 时,它会将所有异步操作生成为 void,并在回调函数中返回结果,所以我不明白如何获得结果,但是早上一切都清楚了。

标签: c# wcf f# asynchronous


【解决方案1】:

我不清楚什么是绊脚石,但过程应该是

  • 让 svcutil 生成异步接口(使用 Begin/End 方法)
  • 将这些方法与FromBeginEnd 一起使用以转换为异步:http://msdn.microsoft.com/en-us/library/ee340508.aspx
  • 请注意,如果该方法返回 void (unit),那么您将在异步工作流中使用 do! 而不是 let!

【讨论】:

  • 如果 Begin* 函数需要一些参数,我该如何使用 FromBeginEnd?
  • 你将不得不发布一些不起作用的代码,我不理解你。
  • 抱歉,已经很晚了,我牙疼得厉害,我没有仔细阅读你提供的链接,错过了我可以将 args 作为第一个参数传递。
【解决方案2】:

回答您问题的一部分:“如果 Begin 函数需要一些参数,我该如何使用 FromBeginEnd?”

您可以通过闭包的魔力来做到这一点。这里有一些例子。如您所见,扩展方法接受参数,但将匿名函数传递给Async.FromBeginEnd,该函数与FromBeginEnd 预期的签名相匹配。额外的参数在闭包中被捕获并传递到匿名函数内部的真实BeginXyz。

您还可以使用 FromBeginEnd 的重载,它首先采用附加参数,然后是指向 Begin/End 函数的指针,就像我在下面的 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)

【讨论】:

    猜你喜欢
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多