gpt4 book ai didi

c# - 使用 WCF 和 C# (F#) 收集类似散点的操作

转载 作者:行者123 更新时间:2023-11-30 22:34:34 25 4
gpt4 key购买 nike

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

  1. 假设我有 20 个节点(计算机)通过 WCF 连接。
  2. 每个节点计算一个持续时间值并将该值发送到主节点
  3. 主节点取所有传入节点的最小值并将此信息发送回所有节点。

编辑:

结果代码:

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()

最佳答案

您可以使用 WCF 轻松地做到这一点。在服务器将在计算最小值后触发的客户端节点上实现回调。

参见 WCF Callbacks; a beginners guide

关于c# - 使用 WCF 和 C# (F#) 收集类似散点的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7763790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com