Exemplo n.º 1
0
        public static async Task <ActionResult <TResult> > ExecuteToActionResultAsync <TResult>(
            IMediatorClient client,
            ActivityId activityId,
            TRequest request,
            Func <TResponse, TResult> extractResult,
            ActivityExecutionConfiguration activityExecutionConfiguration,
            System.Threading.CancellationToken requestAborted)
        {
            try {
                using var connected = await client.ConnectAndSendAsync(
                          activityId,
                          request,
                          activityExecutionConfiguration,
                          requestAborted);

                var response = await connected.WaitForAsync(activityExecutionConfiguration, requestAborted);

                return(response.ConvertResponseToActionResult <TResponse, TResult>(extractResult));
            } catch (System.Exception error) {
                return(new ObjectResult(error.Message)
                {
                    StatusCode = 500
                });
            }
        }
Exemplo n.º 2
0
        public async Task <IMediatorClientConnected <TRequestInner> > ConnectAndSendAsync <TRequestInner>(
            ActivityId activityId,
            TRequestInner request,
            ActivityExecutionConfiguration activityExecutionConfiguration,
            CancellationToken cancellationToken
            )
        {
            var result = await this._MediatorScopeService.ConnectAsync(activityId, request, activityExecutionConfiguration, cancellationToken);

            this._LocalDisposables.Add(result);
            await result.SendAsync(cancellationToken);

            return(result);
        }
Exemplo n.º 3
0
 public Task <IMediatorClientConnected <TRequest> > ConnectAsync <TRequest>(
     ActivityId activityId,
     TRequest request,
     ActivityExecutionConfiguration activityExecutionConfiguration,
     CancellationToken cancellationToken)
 {
     try {
         if (request is null)
         {
             throw new ArgumentNullException(nameof(request));
         }
         //
         if (this._MediatorService.TryRequestRelatedType(typeof(TRequest), out var rrt))
         {
             var mediatorClientConnected
                 = (IMediatorClientConnected <TRequest>)rrt.FactoryClientConnected(
                       this._ServiceProvider,
                       new object[] {
                 new CreateClientConnectedArguments(
                     this._MediatorService,
                     this,
                     activityId,
                     rrt),
                 request
             });
             mediatorClientConnected.Initialize();
             return(Task.FromResult <IMediatorClientConnected <TRequest> >(mediatorClientConnected));
         }
         else
         {
             throw new NotSupportedException($"Unknown RequestType: {typeof(TRequest).FullName}");
         }
     } catch (System.Exception error) {
         return(Task.FromException <IMediatorClientConnected <TRequest> >(error));
     }
 }
Exemplo n.º 4
0
        public async Task <IActivityResponse> WaitForAsync(
            ActivityExecutionConfiguration waitForSpecification,
            CancellationToken cancellationToken
            )
        {
            waitForSpecification ??= ActivityExecutionConfiguration.Default;
            var activityContext = this._ActivityContext;

            if (activityContext is null)
            {
                throw new InvalidOperationException("ActivityContext is null");
            }
            //
            var taskResult = activityContext.GetActivityResponseAsync();

            if (taskResult.IsCompleted)
            {
                return(await taskResult);
            }
            //
            if (waitForSpecification.RespectRequestAborted)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    if (waitForSpecification.SupportAccepted202Redirect)
                    {
                        var redirectUrl = waitForSpecification.RedirectUrl(activityContext.ActivityId.ExecutionId);
                        return(new AcceptedActivityResponse(redirectUrl));
                    }
                    else
                    {
                        return(new CanceledActivityResponse());
                    }
                }
            }
            if (waitForSpecification.WaitTimeSpan == TimeSpan.MaxValue)
            {
                var result = await activityContext.GetActivityResponseAsync();

                return(result);
            }
            else
            {
                var cts = (waitForSpecification.RespectRequestAborted)
                    ? CancellationTokenSource.CreateLinkedTokenSource(cancellationToken)
                    : new CancellationTokenSource();
                var taskTimeout = Task.Delay(waitForSpecification.WaitTimeSpan, cts.Token);
                var taskDone    = await Task.WhenAny(taskResult, taskTimeout);

                if (ReferenceEquals(taskResult, taskDone))
                {
                    // success
                    cts.Cancel();
                    try { await taskTimeout; } catch (TaskCanceledException) { }
                    return(await taskResult);
                }
                else
                {
                    // timeout
                    if (waitForSpecification.SupportAccepted202Redirect)
                    {
                        this._MedaitorService.HandleRequestForAccepted202Redirect(activityContext);
                        var redirectUrl = waitForSpecification.RedirectUrl(activityContext.ActivityId.ExecutionId);
                        return(new AcceptedActivityResponse(redirectUrl));
                    }
                    else
                    {
                        this._MedaitorService.HandleRequestAfterTimeout(activityContext);
                        return(new CanceledActivityResponse());
                    }
                }
            }
        }