两种不同应用之间的通信

时间:2011-04-15 12:38:35

标签: c# asp.net wcf windows

我们在一台机器上运行两个应用程序,其中一个是通过读取xml文档来响应每个请求的Web应用程序。我们希望添加一个案例,即在创建新的xml文件或替换现有文件时,应用程序在全部更改之前不得读取文件,并且在案例发生时,它必须使用旧文件进行响应。

由于Web应用程序适用于请求/响应周期,我们认为不应该干扰这个周期,因为知道文件更改和请求时间之间的时间在实时运行系统中是模糊的,我们必须分割文件读取过程。为此目的,我们在本地机器上使用带有Windows或控制台应用程序的FileSystemWatcher(或者其他人说使用WCF代替)。

现在我们在上面的案例中提出质疑,说我们如何沟通这两个(或更多)应用程序?

1 个答案:

答案 0 :(得分:12)

看起来您对命名管道感兴趣以启用IPC,请查看this link以获取示例,或this MSDN link

抓取NamedPipeServerStream page of MSDN中的代码说明最为简单(请参阅客户端的NamedPipeClientStream page):

using (NamedPipeServerStream pipeServer =
    new NamedPipeServerStream("testpipe", PipeDirection.Out))
{
    Console.WriteLine("NamedPipeServerStream object created.");

    // Wait for a client to connect
    Console.Write("Waiting for client connection...");
    pipeServer.WaitForConnection();

    Console.WriteLine("Client connected.");
    try
    {
        // Read user input and send that to the client process.
        using (StreamWriter sw = new StreamWriter(pipeServer))
        {
            sw.AutoFlush = true;
            Console.Write("Enter text: ");
            sw.WriteLine(Console.ReadLine());
        }
    }
    // Catch the IOException that is raised if the pipe is broken
    // or disconnected.
    catch (IOException e)
    {
        Console.WriteLine("ERROR: {0}", e.Message);
    }
}
相关问题