Exemplo n.º 1
0
        public async override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            try
            {
                // Just make sure api-version is in url
                // Let exception handler handle any exception
                ApiVersionHelper.GetApiVersion(actionContext.Request.RequestUri);
            }
            catch
            {
                var account        = RequestHelper.ParseAccount(actionContext.Request);
                var subscriptionId = await RequestHelper.GetSubscriptionId(account);

                MetricManager.Instance.LogRequestFailed4xx(1, account, subscriptionId, string.Empty);
                throw;
            }

            await base.OnActionExecutingAsync(actionContext, cancellationToken);
        }
Exemplo n.º 2
0
        protected async Task <HttpResponseMessage> OnRequestAsync(string providerType)
        {
            var account        = RequestHelper.ParseAccount(this.Request);
            var subscriptionId = await RequestHelper.GetSubscriptionId(account);

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            try
            {
                // Get provider
                var provider = ProviderManager.GetServiceProvider(providerType);
                Validator.IsTrue <ArgumentException>(provider != null, nameof(providerType), "The provider type '{0}' is invalid.", providerType);

                var path = ParseServicePath(this.Request.RequestUri, providerType);

                // Build request
                var request = new ServiceProviderRequest
                {
                    HttpMethod          = this.Request.Method.Method,
                    Path                = path,
                    Content             = await this.Request.Content.ReadAsStringAsync(),
                    Headers             = this.Request.Headers.ToDictionary(pair => pair.Key, pair => pair.Value),
                    QueryNameValuePairs = this.Request.GetQueryNameValuePairs(),
                    // comment by jin: this is where apiVersion is validated
                    ApiVersion = ApiVersionHelper.GetApiVersion(this.Request.RequestUri)
                };

                // Dispatch
                var result = await provider.OnRequestAsync(request);

                // Build response
                var response = new HttpResponseMessage(result.StatusCode)
                {
                    Content = result.Content != null && result.MediaType != null
                        ? new StringContent(result.Content, Encoding.UTF8, result.MediaType)
                        : null
                };

                if (result.Headers != null && result.Headers.Count >= 0)
                {
                    foreach (var header in result.Headers)
                    {
                        response.Headers.Add(header.Key, header.Value);
                    }
                }

                if (response.IsSuccessStatusCode)
                {
                    MetricManager.Instance.LogRequestSuccess(1, account, subscriptionId, providerType);
                }

                return(response);
            }
            catch
            {
                throw;
            }
            finally
            {
                stopwatch.Stop();
                MetricManager.Instance.LogRequestLatency(stopwatch.ElapsedMilliseconds, account, subscriptionId, providerType);
            }
        }