سؤال

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top