【问题标题】:WCF Duplex clients with different data from the service具有来自服务的不同数据的 WCF 双工客户端
【发布时间】:2014-01-15 17:09:07
【问题描述】:

我正在尝试编写一个 Web 服务,将进程的输出传送到所有监听客户端。这似乎是故障代码。应用程序应该启动一个进程,然后监听输出。还有一种情况是客户端调用StartWebServer,并且进程已经在运行,在这种情况下,该函数不应再次启动该进程,而是将该客户端订阅到接收输出的客户端列表(已损坏)。

问题似乎是当第二个客户端调用StartWebServer时,指定端口上的进程列表与其他客户端的列表不同

此行在第二次调用时返回 false,由不同的客户端使用相同的端口作为参数: !Processes.ContainsKey(port)

Dictionary<int, List<IBatchServiceCallback>> Processes = 
        new Dictionary<int, List<IBatchServiceCallback>>();

public void StartWebServer(int port)
{
    //Start the webserver (if it's not already running) and listen to the output

    //Start process if not already running
    if (!Processes.ContainsKey(port)) // returns false on second call
    {
        int pid = catalina("run"); // Start process
        if (pid < 0) return;

        Processes.Add(port, new List<IBatchServiceCallback>());
    }

    //Listen to process whether or not I started it
    ListenTo(port);
}

【问题讨论】:

    标签: c# wcf web-services webservice-client duplex


    【解决方案1】:

    想通了,我只需要变量是静态的:

    private static Dictionary<int, List<IBatchServiceCallback>> Processes = 
                        new Dictionary<int, List<IBatchServiceCallback>>();
    

    和标签:

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class BatchService : IBatchService
    

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多