简体   繁体   中英

WCF Duplex clients with different data from the service

I am trying to write a web service that communicates output from a process to all listening clients. Here is what seems to be the malfunctioning code. The application is supposed to start a process then listen to the output. There is also a case where a client calls StartWebServer and the process is already running, in which case the function should not start the process again, but instead subscribe that client to the list of clients receiving output(broken).

The problem seems to be that when the second client does the call to StartWebServer , the list of Processes on the specified port is different than the other client's list

This line is returning false on the second call, by a different client, using the same port as a parameter: !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);
}

Figured it out, I just needed the variable to be static:

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

and the tag:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM