public async Task StartMailMergeAsync(RunMailMergeOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (string.IsNullOrWhiteSpace(options.MergeTemplateId))
            {
                throw new ArgumentNullException("options.MergeTemplateId", "MergeTempalteId cannot be null or empty");
            }

            await WithWorker(RUN_MAIL_MERGE_SCOPE, httpClient =>
            {
                // start the task but don't wait for it to finish
                var task = httpClient.PostAsJsonAsync(
                    "api/MailMerge/run",
                    new RunMailMergeOptions()
                {
                    MergeTemplateId = options.MergeTemplateId,
                    ConnectionId    = options.ConnectionId
                },
                    cancellationToken);
                task.Start();
                return(Task.CompletedTask);
            });
        }
Beispiel #2
0
        public async Task StartMailMergeAsync(
            RunMailMergeOptions options,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var mergeTemplate = await _mergeTemplateRepository.GetMergeTemplate(options.MergeTemplateId);

            if (mergeTemplate == null)
            {
                throw new KeyNotFoundException($"MergeTemplate with Id {options.MergeTemplateId} not found");
            }

            Task notifyProgressUpdatedTask = Task.CompletedTask;

            var task = _mergeTemplateService.RunMergeTemplateAsync(mergeTemplate,
                                                                   progress =>
            {
                // only notify is the last one is done already.
                if (!string.IsNullOrWhiteSpace(options.ConnectionId) &&
                    notifyProgressUpdatedTask.IsCompleted)
                {
                    notifyProgressUpdatedTask =
                        NotifyMailMergeUpdatedAsync(
                            options.MergeTemplateId,
                            options.ConnectionId,
                            progress,
                            cancellationToken);
                }
            }
                                                                   /* NO CANCELLATION TOKEN */);

            // start the task but don't wait for it to finish
        }
        public async Task <RunMergeTemplateProgress> RunMailMergeAsync(RunMailMergeOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (string.IsNullOrWhiteSpace(options.MergeTemplateId))
            {
                throw new ArgumentNullException("options.MergeTemplateId", "MergeTempalteId cannot be null or empty");
            }

            RunMergeTemplateProgress returnValue = null;

            await WithWorker(RUN_MAIL_MERGE_SCOPE, async httpClient =>
            {
                try
                {
                    var response = await httpClient.PostAsJsonAsync(
                        "api/MailMerge/run",
                        new RunMailMergeOptions()
                    {
                        MergeTemplateId = options.MergeTemplateId,
                        ConnectionId    = options.ConnectionId
                    },
                        cancellationToken);
                    if (response.IsSuccessStatusCode)
                    {
                        string responseString = await response.Content.ReadAsStringAsync();
                        returnValue           = JsonConvert.DeserializeObject <RunMergeTemplateProgress>(responseString);
                    }
                    else
                    {
                        throw new InvalidOperationException($"Remote server did not return success ({response.StatusCode})");
                    }
                }
                catch (Exception err)
                {
                    _logger.Error(err, "Unable to communicate with worker process");
                    throw;
                }
            });

            return(returnValue);
        }
Beispiel #4
0
        public async Task <RunMergeTemplateProgress> RunMailMergeAsync(
            RunMailMergeOptions options,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var mergeTemplate = await _mergeTemplateRepository.GetMergeTemplate(options.MergeTemplateId);

            if (mergeTemplate == null)
            {
                throw new KeyNotFoundException($"MergeTemplate with Id {options.MergeTemplateId} not found");
            }

            Task notifyProgressUpdatedTask = Task.CompletedTask;

            var result = await _mergeTemplateService.RunMergeTemplateAsync(mergeTemplate,
                                                                           progress =>
            {
                // only notify is the last one is done already.
                if (!string.IsNullOrWhiteSpace(options.ConnectionId) &&
                    notifyProgressUpdatedTask.IsCompleted)
                {
                    notifyProgressUpdatedTask =
                        NotifyMailMergeUpdatedAsync(
                            options.MergeTemplateId,
                            options.ConnectionId,
                            progress,
                            cancellationToken);
                }
            },
                                                                           cancellationToken);

            if (!string.IsNullOrWhiteSpace(options.ConnectionId))
            {
                await NotifyMailMergeCompletedAsync(options.MergeTemplateId,
                                                    options.ConnectionId,
                                                    result,
                                                    cancellationToken);
            }
            return(result);
        }