Ejemplo n.º 1
0
        public static byte[] GetMessage(
            this MessageRepository messageRepository,
            MessageEntry entry)
        {
            if (messageRepository == null)
            {
                throw new ArgumentNullException(nameof(messageRepository));
            }
            if (entry == null)
            {
                throw new ArgumentNullException(nameof(entry));
            }

            return(messageRepository.GetMessage(entry.File));
        }
Ejemplo n.º 2
0
        public IObservable <MimeMessage> Get(MessageEntry messageEntry)
        {
            _logger.Verbose("Loading Message Entry {@MessageEntry}", messageEntry);

            return(Observable.Create <MimeMessage>(
                       o =>
            {
                // in case of multiple subscriptions...
                var observer = Observer.Synchronize(o);

                var disposable = new CancellationDisposable();

                try
                {
                    var message = MimeMessageCache.GetOrSet(
                        messageEntry.File,
                        () =>
                    {
                        _logger.Verbose(
                            "Getting Message Data from Cached Message Repository",
                            messageEntry);
                        var messageData = _messageRepository.GetMessage(messageEntry);
                        MimeMessage mimeMessage;

                        // wrap in a memorystream...
                        using (var ms = new MemoryStream(messageData))
                        {
                            _logger.Verbose(
                                "MimeMessage Load for {@MessageEntry}",
                                messageEntry);

                            mimeMessage = MimeMessage.Load(
                                ParserOptions.Default,
                                ms,
                                disposable.Token);
                        }

                        return mimeMessage;
                    },
                        m =>
                    {
                        var policy = new CacheItemPolicy
                        {
                            SlidingExpiration = TimeSpan.FromSeconds(300)
                        };

                        MimeMessageCache.Add(messageEntry.File, m, policy);
                    });

                    observer.OnNext(message);
                    observer.OnCompleted();
                }
                catch (OperationCanceledException)
                {
                    // no need to respond...
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, "Exception Loading {@MessageEntry}", messageEntry);
                    observer.OnError(ex);
                }

                return disposable;
            }).SubscribeOn(TaskPoolScheduler.Default));
        }