예제 #1
0
        public static async Task <TAgent> CreateAgent <T, TAgent>(this ISupervisor <T> supervisor, IAsyncPipeContextAgent <TAgent> asyncContext,
                                                                  Func <T, CancellationToken, Task <TAgent> > agentFactory, CancellationToken cancellationToken)
            where T : class, PipeContext
            where TAgent : class, PipeContext
        {
            var createAgentPipe = new CreateAgentPipe <T, TAgent>(asyncContext, agentFactory, cancellationToken);

            var supervisorTask = supervisor.Send(createAgentPipe, cancellationToken);

            await Task.WhenAny(supervisorTask, asyncContext.Context).ConfigureAwait(false);

            async Task HandleSupervisorTask()
            {
                try
                {
                    await supervisorTask.ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    await asyncContext.CreateCanceled().ConfigureAwait(false);
                }
                catch (Exception exception)
                {
                    await asyncContext.CreateFaulted(exception).ConfigureAwait(false);
                }
            }

        #pragma warning disable 4014
            HandleSupervisorTask();
        #pragma warning restore 4014

            return(await asyncContext.Context.ConfigureAwait(false));
        }
예제 #2
0
        public static async Task <TAgent> CreateAgent <T, TAgent>(this ISupervisor <T> supervisor, IAsyncPipeContextAgent <TAgent> asyncContext,
                                                                  Func <T, CancellationToken, Task <TAgent> > agentFactory, CancellationToken cancellationToken)
            where T : class, PipeContext
            where TAgent : class, PipeContext
        {
            var createAgentPipe = new CreateAgentPipe <T, TAgent>(asyncContext, agentFactory, cancellationToken);

            var supervisorTask = supervisor.Send(createAgentPipe, cancellationToken);

            await Task.WhenAny(supervisorTask, asyncContext.Context).ConfigureAwait(false);

            if (supervisorTask.IsFaulted)
            {
                await asyncContext.CreateFaulted(supervisorTask.Exception).ConfigureAwait(false);
            }
            else if (supervisorTask.IsCanceled)
            {
                await asyncContext.CreateCanceled().ConfigureAwait(false);
            }

            return(await asyncContext.Context.ConfigureAwait(false));
        }