C# F#和WCF消耗

C# F#和WCF消耗,c#,wcf,f#,asynchronous,C#,Wcf,F#,Asynchronous,我使用标准的“添加服务引用”对话框在C#中创建了一个lib,但在其中实现的方法返回void,因此我无法在异步工作流中绑定它们。我不能使用Begin*End*接口,因为每个方法都有不同数量的参数,所以它会显示“错误的参数计数”(类似于这样)。那么,我如何才能异步使用WCF服务(异步,因为它打算在silverlight中使用,而所有内容都是异步的)?我不清楚障碍是什么,但过程应该是 让svcutil生成异步接口(使用Begin/End方法) 将这些方法与FromBeginEnd一起使用以转换为异步

我使用标准的“添加服务引用”对话框在C#中创建了一个lib,但在其中实现的方法返回void,因此我无法在异步工作流中绑定它们。我不能使用Begin*End*接口,因为每个方法都有不同数量的参数,所以它会显示“错误的参数计数”(类似于这样)。那么,我如何才能异步使用WCF服务(异步,因为它打算在silverlight中使用,而所有内容都是异步的)?

我不清楚障碍是什么,但过程应该是

  • 让svcutil生成异步接口(使用Begin/End方法)
  • 将这些方法与
    FromBeginEnd
    一起使用以转换为异步:
  • 请注意,如果该方法返回
    void
    unit
    ),那么您将使用
    do而不是
    让我们在异步工作流中

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

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

您也可以使用
FromBeginEnd
的重载,它首先获取附加参数,然后是指向开始/结束函数的指针,正如我在下面的
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)
开放系统
开放系统.Data.Services.Client
键入System.Data.Services.Client.DataServiceContext和
成员this.AsyncExecute(uri、cb、state)),
(有趣的iar->this.EndExecute)
成员(this.AsyncExecute)=

Async.FromBeginEnd((fun(cb,state)->this.BeginExecute(iar):?>QueryOperationResponse“返回void,这样我就不能在异步工作流中绑定它们了”-你是什么意思?我的意思是如何使用let获得结果!什么结果?它们返回void?当使用svcutil时,它会将所有异步操作生成为void,并在callback func中返回结果,因此我无法理解如何获得结果,但在早上一切都变清楚了。如果Begin*函数需要一些时间,我如何使用FromBeginEnd参数?你将不得不发布一个不起作用的小代码,我不理解你的意思。抱歉,那是深夜,我牙痛得厉害,我没有仔细阅读你提供的链接,错过了我可以将args作为第一个参数传递的内容。