Ejemplo n.º 1
0
        private async Task <RepositoryDescription> GetRepositoryDescription(ServiceInvocationContext context)
        {
            // We use ConfigureAwait(false) to prevent .NET from trying to synchronize back to our current Sync Context (UI Thread/HttpContext/etc)
            // when continuing from await. We don't care about Sync Context, so this saves the perf hit of syncing back. Our caller will still
            // sync back to their current Sync Context when they await us, so this doesn't affect them.
            using (context.Trace.EnterExit())
            {
                try
                {
                    // Invoke the request
                    var url      = new Uri("/", UriKind.Relative);
                    var response = await context.GetAsync(url, context.CancellationToken)
                                   .ConfigureAwait(continueOnCapturedContext: false);

                    context.CancellationToken.ThrowIfCancellationRequested();
                    response.EnsureSuccessStatusCode();

                    // Parse the response as JSON
                    var json = JObject.Parse(await response.Content.ReadAsStringAsync()
                                             .ConfigureAwait(continueOnCapturedContext: false));

                    // Load the json in to a result
                    return(RepositoryDescription.FromJson(json, context.Trace, context.ResolveUrl(url)));
                }
                catch (Exception ex)
                {
                    context.Trace.Error(ex);
                    throw;
                }
            }
        }