views:

33

answers:

0

I'm trying to make a client/server talk back and forth in a project i'm working on. It seems to work OK when talking between 2 windows services (so far). But it seems to run into some problems when i put the client side in a web service. It works fine at first until lots of requests hit it in quick succession at which point the web service hangs and web service requests just time out (and the server connector doesnt receive any requests). If i connect from another test application, however, it can talk to the service just fine.

anyone have an idea on why this happens?

Client:

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;
    }

Server:

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());
        }
    }