Beispiel #1
0
    public async Task FindByClientIdAsync_Should_Return_Null_If_Not_Found()
    {
        var nonExistingClientId = Guid.NewGuid().ToString();
        var application         = await _applicationStore.FindByClientIdAsync(nonExistingClientId, CancellationToken.None);

        application.ShouldBeNull();
    }
Beispiel #2
0
    /// <inheritdoc/>
    public ValueTask <TApplication?> FindByClientIdAsync(string identifier, CancellationToken cancellationToken)
    {
        if (string.IsNullOrEmpty(identifier))
        {
            throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier));
        }

        var parameters = new
        {
            Method     = nameof(FindByClientIdAsync),
            Identifier = identifier
        };

        if (_cache.TryGetValue(parameters, out TApplication? application))
        {
            return(new ValueTask <TApplication?>(application));
        }

        return(new ValueTask <TApplication?>(ExecuteAsync()));

        async Task <TApplication?> ExecuteAsync()
        {
            if ((application = await _store.FindByClientIdAsync(identifier, cancellationToken)) is not null)
            {
                await AddAsync(application, cancellationToken);
            }

            await CreateEntryAsync(parameters, application, cancellationToken);

            return(application);
        }
    }
        /// <summary>
        /// Retrieves an application using its client identifier.
        /// </summary>
        /// <param name="identifier">The client identifier associated with the application.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>
        /// <returns>
        /// A <see cref="ValueTask{TResult}"/> that can be used to monitor the asynchronous operation,
        /// whose result returns the client application corresponding to the identifier.
        /// </returns>
        public ValueTask <TApplication> FindByClientIdAsync([NotNull] string identifier, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(identifier))
            {
                throw new ArgumentException("The identifier cannot be null or empty.", nameof(identifier));
            }

            var parameters = new
            {
                Method     = nameof(FindByClientIdAsync),
                Identifier = identifier
            };

            if (_cache.TryGetValue(parameters, out TApplication application))
            {
                return(new ValueTask <TApplication>(application));
            }

            return(new ValueTask <TApplication>(ExecuteAsync()));

            async Task <TApplication> ExecuteAsync()
            {
                if ((application = await _store.FindByClientIdAsync(identifier, cancellationToken)) != null)
                {
                    await AddAsync(application, cancellationToken);
                }

                await CreateEntryAsync(parameters, application, cancellationToken);

                return(application);
            }
        }
        /// <summary>
        /// Retrieves an application using its client identifier.
        /// </summary>
        /// <param name="identifier">The client identifier associated with the application.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>
        /// <returns>
        /// A <see cref="ValueTask{TResult}"/> that can be used to monitor the asynchronous operation,
        /// whose result returns the client application corresponding to the identifier.
        /// </returns>
        public ValueTask <TApplication> FindByClientIdAsync([NotNull] string identifier, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(identifier))
            {
                throw new ArgumentException("The identifier cannot be null or empty.", nameof(identifier));
            }

            var parameters = new
            {
                Method     = nameof(FindByClientIdAsync),
                Identifier = identifier
            };

            if (_cache.TryGetValue(parameters, out TApplication application))
            {
                return(new ValueTask <TApplication>(application));
            }

            async Task <TApplication> ExecuteAsync()
            {
                if ((application = await _store.FindByClientIdAsync(identifier, cancellationToken)) != null)
                {
                    await AddAsync(application, cancellationToken);
                }

                using (var entry = _cache.CreateEntry(parameters))
                {
                    if (application != null)
                    {
                        var signal = await CreateExpirationSignalAsync(application, cancellationToken);

                        if (signal == null)
                        {
                            throw new InvalidOperationException("An error occurred while creating an expiration signal.");
                        }

                        entry.AddExpirationToken(signal);
                    }

                    entry.SetSize(1L);
                    entry.SetValue(application);
                }

                return(application);
            }

            return(new ValueTask <TApplication>(ExecuteAsync()));
        }