private static void AddPlayerClient(this IServiceCollection services, ClientOptions clientOptions, ILoggerFactory loggerFactory)
        {
            var policy = GetPolicy(clientOptions.MaxRetryDelaySeconds, "Player Vm Api", loggerFactory);

            services.AddHttpClient("player", client =>
            {
                // Workaround to avoid TaskCanceledException after several retries. TODO: find a better way to handle this.
                client.Timeout = Timeout.InfiniteTimeSpan;
            })
            .AddHttpMessageHandler <AuthenticatingHandler>()
            .AddPolicyHandler(policy);

            services.AddScoped <IS3VmApiClient, S3VmApiClient>(p =>
            {
                var httpClientFactory = p.GetRequiredService <IHttpClientFactory>();
                var playerOptions     = p.GetRequiredService <PlayerOptions>();

                var uri = new Uri(playerOptions.VmApiUrl);

                var httpClient         = httpClientFactory.CreateClient("player");
                httpClient.BaseAddress = uri;

                var s3VmApiClient     = new S3VmApiClient(httpClient, true);
                s3VmApiClient.BaseUri = uri;

                return(s3VmApiClient);
            });
        }
예제 #2
0
        public PlayerVmService(IHttpContextAccessor httpContextAccessor, ClientOptions clientSettings)
        {
            _userId = httpContextAccessor.HttpContext.User.GetId();

            // Workaround for token bug introduced in .NET Core 2.1.  Remove in 2.2
            string authHeader = httpContextAccessor.HttpContext.Request.Headers["Authorization"];
            string token      = null;

            if (!string.IsNullOrEmpty(authHeader))
            {
                token = authHeader.Replace("bearer ", string.Empty).Replace("Bearer ", string.Empty);
            }

            // Go back to this when bug is fixed in .NET Core 2.2
            //var token = httpContextAccessor.HttpContext.GetTokenAsync("access_token").Result;

            _s3VmApiClient = new S3VmApiClient(new Uri(clientSettings.urls.vmApi), new TokenCredentials(token, "Bearer"));
        }