使用WCF和C#(F#)收集分散操作

时间:2011-10-14 06:20:14

标签: c# .net wcf f#

使用WCF和C#(或F#)实现Gather-Scatter操作的最佳方法是什么?

  1. 假设我有20个节点(计算机)通过WCF连接。
  2. 每个节点计算一个time duration值,并将此值发送给main node
  3. main node获取所有进入的值的最小值,并将此信息发送回所有节点。
  4. 编辑:

    结果代码:

    open System.ServiceModel
    open System.ServiceModel.Channels
    
    let numberOfClients = 10
    
    type IMyContractCallback =
      [<OperationContract>]
      abstract GetDuration: duration: float -> unit
    
    [<ServiceContract(CallbackContract = typeof<IMyContractCallback>)>]
    type IMyContract =
        [<OperationContract>]
        abstract SendDuration: duration: float -> unit
    
    [<ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Reentrant)>]
    type MyService () =
    
      let mutable totDuration = 1.
      let callbacks = System.Collections.Generic.Stack ()
    
      interface IMyContract with
        member o.SendDuration (duration) =
            totDuration <- min totDuration duration
            callbacks.Push (OperationContext.Current.GetCallbackChannel<IMyContractCallback>())
            printfn "N = %d" callbacks.Count
            if callbacks.Count = numberOfClients then
              for c in callbacks do c.GetDuration (totDuration)
    
      interface IMyContractCallback with
        member o.GetDuration (duration) = printfn "Minimum duration = %g" duration
    
    let address = "net.pipe://localhost/aaa"
    
    let service = MyService ()
    let pipe = new NetNamedPipeBinding()
    
    let host = new ServiceHost(service)
    host.AddServiceEndpoint(typeof<IMyContract>, pipe, address) |> ignore
    host.Open()
    
    for i in 0 .. numberOfClients - 1 do
      let client1 = System.Threading.Thread (fun () ->
        let fact = new DuplexChannelFactory<IMyContract>(new InstanceContext(service), pipe, address)
        let ch = fact.CreateChannel()
        ch.SendDuration (0.4 + float i) )
      client1.Start()
    

1 个答案:

答案 0 :(得分:3)

您可以使用WCF轻松完成此操作。 在计算最小值后服务器将触发的客户机节点上实现回调。

请参阅WCF Callbacks; a beginners guide