IPipeContextAgent <ConnectionContext> IPipeContextFactory <ConnectionContext> .CreateContext(ISupervisor supervisor) { var context = Task.Run(() => CreateConnection(supervisor), supervisor.Stopping); IPipeContextAgent <ConnectionContext> contextHandle = supervisor.AddContext(context); void HandleConnectionException(Exception exception) { contextHandle.Stop($"Connection Exception: {exception}"); } context.ContinueWith(task => { task.Result.Connection.ExceptionListener += HandleConnectionException; contextHandle.Completed.ContinueWith(_ => task.Result.Connection.ExceptionListener -= HandleConnectionException); }, TaskContinuationOptions.OnlyOnRanToCompletion); return(contextHandle); }
IPipeContextAgent <ConnectionContext> IPipeContextFactory <ConnectionContext> .CreateContext(ISupervisor supervisor) { Task <ConnectionContext> context = Task.Run(() => CreateConnection(supervisor), supervisor.Stopped); IPipeContextAgent <ConnectionContext> contextHandle = supervisor.AddContext(context); void HandleShutdown(object sender, ShutdownEventArgs args) { if (args.Initiator != ShutdownInitiator.Application) { contextHandle.Stop(args.ReplyText); } } context.ContinueWith(task => { task.Result.Connection.ConnectionShutdown += HandleShutdown; contextHandle.Completed.ContinueWith(_ => task.Result.Connection.ConnectionShutdown -= HandleShutdown); }, TaskContinuationOptions.OnlyOnRanToCompletion); return(contextHandle); }
Task IAgent.Stop(StopContext context) { return(_agent.Stop(context)); }