.NET NamedPipes客户端服务器通信问题Web服务

时间:2010-08-20 21:02:48

标签: c# .net web-services named-pipes

我正在努力让客户/服务器在我正在进行的项目中来回交谈。在两个Windows服务之间进行交谈时似乎工作正常(到目前为止)。但是当我将客户端放在Web服务中时,似乎遇到了一些问题。它一开始工作正常,直到很多请求快速连续发出,此时Web服务挂起,Web服务请求只是超时(并且服务器连接器没有收到任何请求)。但是,如果我从另一个测试应用程序连接,它可以很好地与服务通信。

任何人都知道为什么会这样?

客户端:

protected T SendMessage<T>(ConnectorRequest request)
    {
        ConnectorResponse response = null;//default(T);
        BinaryFormatter formatter = new BinaryFormatter();
        try
        {
            using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", PIPE_NAME, PipeDirection.InOut))
            {
                //TODO: make this configurable
                pipeClient.Connect(Timeout);
                using (StreamWriter sw = new StreamWriter(pipeClient))
                {
                    sw.AutoFlush = true;
                    formatter.Serialize(pipeClient, request);
                    pipeClient.WaitForPipeDrain();
                    response = (ConnectorResponse)formatter.Deserialize(pipeClient);
                }
            }
        }
        catch (Exception ex)
        {
            logger.Error("response: " + response + "Exception thrown: " + ex);
        }
        return (T)response.Response;
    }

服务器:

void ConnectorRun()
    {
        try
        {
            PipeSecurity ps = new PipeSecurity();
            ps.AddAccessRule(new PipeAccessRule("NETWORK SERVICE", PipeAccessRights.ReadWrite | PipeAccessRights.CreateNewInstance, AccessControlType.Allow));
            ps.AddAccessRule(new PipeAccessRule("Users", PipeAccessRights.ReadWrite | PipeAccessRights.CreateNewInstance, AccessControlType.Allow));
            ps.AddAccessRule(new PipeAccessRule("CREATOR OWNER", PipeAccessRights.FullControl, AccessControlType.Allow));
            ps.AddAccessRule(new PipeAccessRule("SYSTEM", PipeAccessRights.FullControl, AccessControlType.Allow));
            //ps.AddAccessRule(pa);

            _connectorIsRunning = true;
            //PipeSecurity security = new PipeSecurity(
            while (!_connectorStopRequested)
            {
                try
                {
                    using (NamedPipeServerStream pipeServer = new NamedPipeServerStream(PIPE_NAME, PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.None, 2048, 2048, ps))
                    {
                        pipeServer.WaitForConnection();
                        BinaryFormatter formatter = new BinaryFormatter();
                        ConnectorRequest request = (ConnectorRequest)formatter.Deserialize(pipeServer);
                        ConnectorResponse response = OnConnectorRequest(request);
                        formatter.Serialize(pipeServer, response);
                        pipeServer.WaitForPipeDrain();
                        pipeServer.Disconnect();
                    }
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                }
            }
        }
        catch (Exception ex)
        {
            logger.Error(ex);
        }
        finally
        {
            _connectorIsRunning = false;
            OnConnectorServerStopped(this, new EventArgs());
        }
    }

0 个答案:

没有答案