/// <summary>
            /// Retrieves the resource with the specified moniker.
            /// </summary>
            /// <param name="resourceMoniker">The identifier for the desired resource.</param>
            /// <param name="cancellationToken">The token whose cancellation signals lost interest in this resource.</param>
            /// <returns>A task whose result is the desired resource.</returns>
            internal async Task <TResource> GetResourceAsync(TMoniker resourceMoniker, CancellationToken cancellationToken)
            {
                using (AsyncReaderWriterResourceLock <TMoniker, TResource> .ResourceReleaser resourceLock = this.AcquirePreexistingLockOrThrow())
                {
                    TResource?resource = await this.service.GetResourceAsync(resourceMoniker, cancellationToken).ConfigureAwait(false);

                    Task preparationTask;

                    lock (this.service.SyncObject)
                    {
                        this.SetResourceAsAccessed(resource);

                        preparationTask = this.PrepareResourceAsync(resource, cancellationToken);
                    }

                    await preparationTask.ConfigureAwait(false);

                    return(resource);
                }
            }
Example #2
0
            /// <summary>
            /// Retrieves the resource with the specified moniker.
            /// </summary>
            /// <param name="resourceMoniker">The identifier for the desired resource.</param>
            /// <param name="cancellationToken">The token whose cancellation signals lost interest in this resource.</param>
            /// <returns>A task whose result is the desired resource.</returns>
            internal async Task <TResource> GetResourceAsync(TMoniker resourceMoniker, CancellationToken cancellationToken)
            {
                using (var resourceLock = this.AcquirePreexistingLockOrThrow())
                {
                    var resource = await this.service.GetResourceAsync(resourceMoniker, cancellationToken).ConfigureAwait(false);

                    Task preparationTask;

                    lock (this.service.SyncObject)
                    {
                        this.SetResourceAsAccessed(resource);

                        // We can't currently use the caller's cancellation token for this task because
                        // this task may be shared with others or call this method later, and we wouldn't
                        // want their requests to be cancelled as a result of this first caller cancelling.
                        preparationTask = this.PrepareResourceAsync(resource, cancellationToken);
                    }

                    await preparationTask.ConfigureAwait(false);

                    return(resource);
                }
            }
Example #3
0
 /// <summary>
 /// Gets the lock protected resource.
 /// </summary>
 /// <param name="resourceMoniker">The identifier for the protected resource.</param>
 /// <param name="cancellationToken">A token whose cancellation signals lost interest in the protected resource.</param>
 /// <returns>A task whose result is the resource.</returns>
 public Task <TResource> GetResourceAsync(TMoniker resourceMoniker, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(this.helper.GetResourceAsync(resourceMoniker, cancellationToken));
 }