Esempio n. 1
0
        public void SynchroniseOutstanding()
        {
            var thisServer = db.Get <MyMood.Domain.MoodServer>().Where(s => s.Name == Configuration.WebConfiguration.ServerName).FirstOrDefault();

            if (thisServer != null)
            {
                var otherServers = (from s in db.Get <MyMood.Domain.MoodServer>()
                                    where s.Id != thisServer.Id && (!s.LastSuccessfulSync.HasValue || s.LastSuccessfulSync.Value.AddMinutes(Configuration.WebConfiguration.ServerSyncIntervalMinutes) < DateTime.UtcNow)
                                    select s)
                                   .ToArray();

                var eventsToSync = (from e in db.Get <Event>()
                                    select e)
                                   .ToArray();

                var client = new HttpClient();

                foreach (var otherServer in otherServers)
                {
                    client.BaseAddress = new Uri(otherServer.BaseAddress);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    foreach (var evt in eventsToSync)
                    {
                        try
                        {
                            var outgoingChanges = this.GetChangesNewerThan(evt, otherServer.LastSuccessfulSync ?? DateTime.MinValue);

                            var response = client.PostAsJsonAsync <DataSyncChangeSet>("api/datasync?eventName=" + evt.Name, outgoingChanges).Result;

                            if (response.IsSuccessStatusCode)
                            {
                                var incomingChanges = response.Content.ReadAsAsync <DataSyncChangeSet>().Result;

                                this.SyncEntityChanges(evt, incomingChanges, null);
                            }
                            else
                            {
                                this.logger.Error(this.GetType(), string.Format("Failed during data sync between {0} and {1} for event {2} - HTTP {3} {4}", thisServer.Name, otherServer.Name, (int)response.StatusCode, response.ReasonPhrase));
                            }

                            otherServer.LastSuccessfulSync = DateTime.UtcNow;

                            db.Add(new MoodServerSyncReport(thisServer, otherServer, DateTime.UtcNow));

                            db.SaveChanges();
                        }
                        catch (Exception ex)
                        {
                            this.logger.Error(this.GetType(), ex, string.Format("Failed during data sync between {0} and {1}", thisServer.Name, otherServer.Name));
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        public DataSyncChangeSet EventData(string eventName, DateTime newerThan, DataSyncChangeSet pushChanges)
        {
            var evt = db.Get <Event>().Where(e => e.Name.Equals(eventName, StringComparison.InvariantCultureIgnoreCase)).First();

            var outgoingChanges = syncAgent.GetChangesNewerThan(evt, newerThan);

            syncAgent.SyncEntityChanges(evt, outgoingChanges, pushChanges);

            db.SaveChanges();

            return(outgoingChanges);
        }
Esempio n. 3
0
        private bool TryDeliverMessage(SentMailMessage message)
        {
            using (var mailClient = new Smtp())
            {
                try
                {
                    var senderAccount = FindMailAccountFor(message.From.Address);

                    if (senderAccount == null)
                    {
                        throw new ArgumentException("Could not deliver message - invalid account");
                    }

                    if (message.ReplyTo.Count() == 0 && !string.IsNullOrWhiteSpace(senderAccount.ReplyTo))
                    {
                        message.ReplyTo.Add(new MailAddress()
                        {
                            Address = senderAccount.ReplyTo
                        });
                    }

                    var server = mailClient.SmtpServers.Add(senderAccount.ServerAddress, senderAccount.ServerUsername, senderAccount.ServerPassword);
                    server.Port    = senderAccount.ServerPort;
                    server.Timeout = 60000;

                    var email = new MailBeeMessage()
                    {
                        Charset = "utf-8", ThrowExceptions = true
                    };

                    email.From = new MailBeeEmailAddress(senderAccount.SenderEmailAddress, message.From.DisplayName ?? senderAccount.SenderDisplayName);
                    message.To.ForEach(to => email.To.Add(to.Address, to.DisplayName));
                    message.ReplyTo.ForEach(to => email.ReplyTo.Add(to.Address, to.DisplayName));
                    message.CC.ForEach(cc => email.Cc.Add(cc.Address, cc.DisplayName));
                    message.BCC.ForEach(bcc => email.Bcc.Add(bcc.Address, bcc.DisplayName));

                    email.Subject       = message.Subject;
                    email.BodyPlainText = message.IsBodyHtml ? null : message.Body;
                    email.BodyHtmlText  = message.IsBodyHtml ? message.Body : null;

                    foreach (var attachment in message.Attachments)
                    {
                        if (attachment.Content != null)
                        {
                            email.Attachments.Add(attachment.Content, attachment.Name, string.Empty, attachment.ContentType, null, NewAttachmentOptions.None, MailTransferEncoding.None);
                        }
                        else if (!string.IsNullOrEmpty(attachment.Path))
                        {
                            email.Attachments.Add(attachment.Path, attachment.Name, string.Empty, attachment.ContentType, null, NewAttachmentOptions.None, MailTransferEncoding.None);
                        }
                    }

                    if (message.IsEncrypted)
                    {
                        email = new Smime().SignAndEncrypt(email, new Certificate(senderAccount.CertificateFilePath, CertFileType.Pfx, senderAccount.CertificatePassword), FindAllRecipientCertificatesFor(message));
                    }
                    else if (message.IsSigned)
                    {
                        email = new Smime().Sign(email, new Certificate(senderAccount.CertificateFilePath, CertFileType.Pfx, senderAccount.CertificatePassword));
                    }

                    email.EncodeAllHeaders(System.Text.Encoding.UTF8, HeaderEncodingOptions.None);

                    mailClient.Message = email;
                    mailClient.Send();

                    message.DeliveryStatus = DeliveryStatus.Sent;
                    message.SentOn         = DateTime.UtcNow;
                    return(true);
                }
                catch (Exception ex)
                {
                    message.FailedOn       = DateTime.UtcNow;
                    message.FailureCount  += 1;
                    message.FailureMessage = ex.ToString();
                    message.DeliveryStatus = (message.FailureCount < config.SuspendMailAfterAttempts) ? DeliveryStatus.Failed : DeliveryStatus.Suspended;
                    return(false);
                }
                finally
                {
                    db.SaveChanges();
                }
            }
        }