コード例 #1
0
        ///<inheritdoc cref="IProjectInfoSender"/>
        public override async Task <ProjectImportResult> Send(ProjectInfo info)
        {
            var importResult = new ProjectImportResult(info);

            try
            {
                Persistence.Models.ProjectInfo importedProject = await this.service.Upsert(info).ConfigureAwait(false);

                this.telemetryClient.TrackTrace($"Inserted project with ID [{importedProject.Id}]");
                importResult.Success  = true;
                importResult.Response = importedProject.Id.ToString();
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                this.telemetryClient.TrackException(ex);
                importResult.Exception = ex;
            }

            return(importResult);
        }
コード例 #2
0
        /// <summary>
        /// Sends the specified information.
        /// </summary>
        /// <param name="info">The information.</param>
        /// <returns>Task.</returns>
        public override async Task <ProjectImportResult> Send(ProjectInfo info)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }
            var projectImportResult = new ProjectImportResult(info);

            this.ProgressLog.Report(new ProjectImportProgressData(ProjectImportProgressData.VerbosityLevel.Debug, $"Serializing {info.ProjectName} project info"));

            string serialized;

            try
            {
                serialized = JsonConvert.SerializeObject(info);
            }
            catch (Exception ex)
            {
                this.ProgressLog.Report(new ProjectImportProgressData($"Error while serializing project info: {info.ProjectName}", ex));
                projectImportResult.Exception = ex;
                return(projectImportResult);
            }
            this.ProgressLog.Report(new ProjectImportProgressData(ProjectImportProgressData.VerbosityLevel.Info, $"Sending {info.ProjectName} project info"));
            Stopwatch sw = Stopwatch.StartNew();

            try
            {
                using (StringContent content = new StringContent(serialized, Encoding.UTF8, "application/json"))
                {
                    HttpResponseMessage result = await this.client.PostAsync("api/manifest", content).ConfigureAwait(false);

                    sw.Stop();
                    if (result.IsSuccessStatusCode)
                    {
                        this.ProgressLog.Report(new ProjectImportProgressData(ProjectImportProgressData.VerbosityLevel.Info,
                                                                              $"Sent [{info.ProjectName}]. StatusCode: [{result.StatusCode}]. Elapsed: [{sw.ElapsedMilliseconds}ms] Response: [{result.Headers?.Location}]"));
                        string response = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

                        this.ProgressLog.Report(new ProjectImportProgressData(ProjectImportProgressData.VerbosityLevel.Debug, $"Response [{response}]."));
                        projectImportResult.Success  = true;
                        projectImportResult.Response = $"Location: [{result.Headers?.Location}]";
                        return(projectImportResult);
                    }
                    else
                    {
                        string response;
                        try
                        {
                            response = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            response = "Error while reading http response content: " + ex;
                        }

                        this.ProgressLog.Report(new ProjectImportProgressData(ProjectImportProgressData.VerbosityLevel.Error,
                                                                              $"Error - [{result.StatusCode}] - [{result.ReasonPhrase}] - while sending [{info.ProjectName}]. Elapsed: [{sw.ElapsedMilliseconds}ms]. {response}"));
                        projectImportResult.Exception = new InvalidOperationException($"Error - [{result.StatusCode}] - [{result.ReasonPhrase}] - while sending [{info.ProjectName}]. {response}");
                        projectImportResult.Response  = response;
                        return(projectImportResult);
                    }
                }
            }
            catch (Exception ex)
            {
                sw.Stop();
                this.ProgressLog.Report(new ProjectImportProgressData($"Unexpected error while sending project info: {info.ProjectName}. Elapsed: [{sw.ElapsedMilliseconds}ms]. {serialized}", ex));
                projectImportResult.Exception = ex;
                return(projectImportResult);
            }
        }