Beispiel #1
0
        public override IWebResponse Get()
        {
            IDependencyTelemetry dependencyTelemetry = null;

            dependencyTelemetry = _telemetryFactory.BuildDependencyTelemetry(
                "Web",
                Address.ToString(),
                _telemetryTag ?? "WebRequest",
                null);

            using (var dependencyClient = _telemetryClient.StartOperation <IDependencyTelemetry>(dependencyTelemetry))
            {
                var response = base.Get();

                dependencyTelemetry.ResultCode = response.StatusCode.ToString();
                dependencyClient.CompleteOperation(response.Success);

                return(response);
            }
        }
Beispiel #2
0
        public virtual IPluginWebResponse Get()
        {
            IDependencyTelemetry dependencyTelemetry = null;
            IOperationalTelemetryClient <IDependencyTelemetry> dependencyClient = null;

            if (_telemetryClient != null && _telemetryFactory != null)
            {
                dependencyTelemetry = _telemetryFactory.BuildDependencyTelemetry(
                    "Web",
                    _address.ToString(),
                    _dependencyName != null ? _dependencyName : "PluginWebRequest",
                    null);
                dependencyClient = _telemetryClient.StartOperation <IDependencyTelemetry>(dependencyTelemetry);
            }

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_address);
                request.Method = "GET";
                if (this.Credentials != null)
                {
                    request.Credentials = this.Credentials;
                }
                request.Headers = this.Headers;
                request.Timeout = (int)this.Timeout.TotalMilliseconds;

                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    var pluginWebResponse = new PluginHttpWebResponse(response);
                    if (dependencyTelemetry != null)
                    {
                        dependencyTelemetry.ResultCode = pluginWebResponse.StatusCode.ToString();
                    }

                    //signals completion of the request operation for telemetry tracking.
                    if (dependencyClient != null)
                    {
                        dependencyClient.CompleteOperation(pluginWebResponse.Success);
                    }

                    return(pluginWebResponse);
                }
            }
            catch (WebException ex)
            {
                string str = string.Empty;
                if (ex.Response != null)
                {
                    using (StreamReader reader = new StreamReader(ex.Response.GetResponseStream()))
                    {
                        str = reader.ReadToEnd();
                    }
                    ex.Response.Close();
                }
                if (ex.Status == WebExceptionStatus.Timeout)
                {
                    throw new Exception("Plugin Web Request Timeout occurred.", ex);
                }
                throw new Exception(String.Format(CultureInfo.InvariantCulture,
                                                  "A Web exception occurred while attempting to issue the request. {0}: {1}",
                                                  ex.Message, str), ex);
            }
            finally
            {
                if (dependencyClient != null)
                {
                    dependencyClient.Dispose();
                }
            }
        }