Exemple #1
0
        public Task <MessageBridgeResponse> RequestAsync(MessageBridgeRequest request)
        {
            return(Task.Run(() =>
            {
                var pipe = new NamedPipeClientStream(_pipeName);
                try
                {
                    pipe.Connect();
                    var reader = new StreamReader(pipe);
                    var writer = new StreamWriter(pipe);

                    writer.Write(request.ToJson());
                    writer.Flush();
                    pipe.WaitForPipeDrain();

                    var json = reader.ReadToEnd();

                    if (string.IsNullOrEmpty(json))
                    {
                        return MessageBridgeResponse.CreateFailureResponse("No content.");
                    }

                    pipe.Close();
                    pipe.Dispose();

                    return MessageBridgeResponse.CreateSuccessResponse(json);
                }
                catch (Exception ex)
                {
                    return MessageBridgeResponse.CreateFailureResponse(ex.Message);
                }
            }));
        }
        public Task <MessageBridgeResponse> RequestAsync(MessageBridgeRequest request)
        {
            return(Task.Run(() =>
            {
                var pipe = new NamedPipeClientStream(_pipeName);
                try
                {
                    pipe.Connect();
                    pipe.ReadMode = PipeTransmissionMode.Message;

                    var buff = Encoding.UTF8.GetBytes(request.ToJson());

                    pipe.Write(buff, 0, buff.Length);
                    pipe.Flush();
                    pipe.WaitForPipeDrain();

                    buff = new byte[BUFFER_SIZE];
                    var ms = new MemoryStream();

                    do
                    {
                        ms.Write(buff, 0, pipe.Read(buff, 0, buff.Length));
                    }while (!pipe.IsMessageComplete);

                    var json = Encoding.UTF8.GetString(ms.ToArray());
                    ms.Close();
                    ms.Dispose();

                    buff = null;


                    if (string.IsNullOrEmpty(json))
                    {
                        return MessageBridgeResponse.CreateFailureResponse("No content.");
                    }

                    pipe.Close();
                    pipe.Dispose();

                    return MessageBridgeResponse.CreateSuccessResponse(json);
                }
                catch (Exception ex)
                {
                    return MessageBridgeResponse.CreateFailureResponse(ex.Message);
                }
            }));
        }