// This is used by some code in Process 1 public async Task SendMessage(string message) { try { if (ClientPipe != null && //ClientPipe.RawPipeStream.IsConnected == true && ClientPipe.State == PipeState.Connected) { await semaphoreSlim.WaitAsync(); await ClientPipe.InvokeAsync(cmd => cmd.WriteMessage(message)); semaphoreSlim.Release(); } else { Console.WriteLine($"Pipe state is:[{ClientPipe?.State}]"); } } catch (Exception e) { Console.WriteLine($"Message: [{message}]] | Exception: [{e.Message}] | Stack: [{e.StackTrace}] | InnerEx: [{e.InnerException}]"); ClientPipe.Dispose(); ClientPipe = null; } }
public async Task RunAsync() { var pipeClient = new PipeClientWithCallback <IAdder, IConcatenator>("mypipe", () => new Concatenator()); await pipeClient.ConnectAsync(); int result = await pipeClient.InvokeAsync(adder => adder.AddNumbers(4, 7)); }
private static async Task RunClientAsync() { var rawStream = new NamedPipeClientStream(".", "testpipe", PipeDirection.InOut, PipeOptions.Asynchronous); var pipeClientWithCallback = new PipeClientWithCallback <IAdder, IConcatenator>(rawStream, () => new Concatenator()); //var pipeClientWithCallback = new PipeClientWithCallback<IAdder, IConcatenator>("testpipe", () => new Concatenator()); pipeClientWithCallback.SetLogger(message => Console.WriteLine(message)); try { await pipeClientWithCallback.ConnectAsync().ConfigureAwait(false); WrappedInt result = await pipeClientWithCallback.InvokeAsync(adder => adder.AddWrappedNumbers(new WrappedInt { Num = 1 }, new WrappedInt { Num = 3 })).ConfigureAwait(false); Console.WriteLine("Server wrapped add result: " + result.Num); int asyncResult = await pipeClientWithCallback.InvokeAsync(adder => adder.AddAsync(4, 7)).ConfigureAwait(false); Console.WriteLine("Server async add result: " + asyncResult); IList <string> listifyResult = await pipeClientWithCallback.InvokeAsync(adder => adder.Listify("item")).ConfigureAwait(false); Console.WriteLine("Server listify result: " + listifyResult[0]); try { await pipeClientWithCallback.InvokeAsync(adder => adder.AlwaysFails()).ConfigureAwait(false); } catch (PipeInvokeFailedException exception) { Console.WriteLine("Handled invoke exception:" + Environment.NewLine + exception); } try { int refValue = 4; await pipeClientWithCallback.InvokeAsync(adder => adder.HasRefParam(ref refValue)).ConfigureAwait(false); } catch (PipeInvokeFailedException exception) { Console.WriteLine("Handled invoke exception:" + Environment.NewLine + exception); } await pipeClientWithCallback.WaitForRemotePipeCloseAsync(); Console.WriteLine("Server closed pipe."); } catch (Exception exception) { Console.WriteLine("Exception in pipe processing: " + exception); } }
private async Task <bool> ConnectToPipeAsync() { for (int i = 0; i < ConnectionRetries; i++) { try { this.Client = new PipeClientWithCallback <TWork, TCallback>(this.pipeName, () => this.CallbackInstance); // With extended logging, log the pipe messages. if (Config.LogVerbosity >= 2) { this.Client.SetLogger(message => { this.Logger.Log(message); #if DEBUG System.Diagnostics.Debug.WriteLine(message); #endif } ); } await this.Client.ConnectAsync().ConfigureAwait(false); await this.Client.InvokeAsync(x => x.Ping()); return(true); } catch (Exception) { } if (this.worker.HasExited) { List <LogEntry> logs = this.GetWorkerMessages(); int errors = logs.Count(l => l.LogType == LogType.Error); this.Logger.LogError("Worker exited before a connection could be established."); if (errors > 0) { this.Logger.Log(logs); } return(false); } await Task.Delay(ConnectionRetryIntervalMs); } this.Logger.LogError("Connection to worker failed after " + ConnectionRetries + " retries. Unable to find endpoint."); this.LogAndClearWorkerMessages(); return(false); }
// This is used by some code in Process 1 public void SendMessage(string message) { try { if (ClientPipe != null && //ClientPipe.RawPipeStream.IsConnected == true && ClientPipe.State == PipeState.Connected) { ClientPipe.InvokeAsync(cmd => cmd.WriteMessage(message)); } } catch (Exception e) { ClientPipe.Dispose(); ClientPipe = null; Console.WriteLine($"ClientClass Exception message: [{e.Message}] | stack: [{e.StackTrace}] | innerException: [{e.InnerException}]."); } }
// This monitors the client pipe in case it's disconnected. // It's started in Program.Main() public async Task ClientPipeMonitor() { while (true) { if (ClientPipe == null || //ClientPipe.RawPipeStream.IsConnected == false || ClientPipe?.State != PipeState.Connected) { // Now reconnect Console.WriteLine("Connecting..."); ClientPipe?.Dispose(); ClientPipe = null; ClientPipe = new PipeClientWithCallback <IServerMethods, IClientMethods>("Pipename", () => new PipeClientImpl()); await ClientPipe.ConnectAsync(); Console.WriteLine("Connected."); } await Task.Run(() => Thread.Sleep(100)); } }
// This monitors the client pipe in case it's disconnected. // It's started in Program.Main() public async Task ClientPipeMonitor(bool justStartIt = false) { while (true) { if (ClientPipe == null || //ClientPipe.RawPipeStream.IsConnected == false || // I just added this and it works. ClientPipe?.State != PipeState.Connected) { // Now reconnect Console.WriteLine("Connecting..."); ClientPipe?.Dispose(); ClientPipe = null; ClientPipe = new PipeClientWithCallback <IServerMethods, IClientMethods>("Pipename", () => new PipeClientImpl()); await ClientPipe.ConnectAsync(); Console.WriteLine("Connected."); if (justStartIt) { break; // Just let it start and break out of the loop. } } await Task.Run(() => Thread.Sleep(100)); } }