public void CatchException() { var testHub = new MessagesHub("test"); Action <bool> throwTestException = _ => { throw new TestException(); }; using (ThrowExceptionMessage.Subscribe(throwTestException, null, testHub)) { Assert.Throws <TestException>(() => { ThrowExceptionMessage.Broadcast(true, testHub); }); } }
public void RecoveryAfterException() { var testHub = new MessagesHub("test"); int counter = 0; Action <bool> neverThrow = needThrow => { ++counter; }; Action <bool> throwAction = needThrow => { if (needThrow) { throw new Exception("test"); } ++counter; }; using (var subscription0 = ThrowExceptionMessage.Subscribe(neverThrow, null, testHub)) using (var subscription1 = ThrowExceptionMessage.Subscribe(throwAction, null, testHub)) using (var subscription2 = ThrowExceptionMessage.Subscribe(neverThrow, null, testHub)) { ThrowExceptionMessage.Broadcast(false, testHub); Assert.AreEqual(counter, 3); Assert.Throws <Exception>(() => { ThrowExceptionMessage.Broadcast(true, testHub); }); Assert.AreEqual(counter, 4); ThrowExceptionMessage.Broadcast(false, testHub); Assert.AreEqual(counter, 7); } }
public string DeleteMessages(int GridId) { //var messages = new List<Messages>(); string messages = string.Empty; using (var connection = new SqlConnection(_connString)) { connection.Open(); using (var command = new SqlCommand("sp_Delete_Messages", connection)) { command.Notification = null; var dependency = new SqlDependency(command); dependency.OnChange += new OnChangeEventHandler(dependency_OnChange); if (connection.State == ConnectionState.Closed) { connection.Open(); } command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@gridId", GridId); //var reader = command.ExecuteReader(); int reader = command.ExecuteNonQuery(); if (reader >= 1) { messages = "Success"; MessagesHub.SendMessages(); } else { messages = "Failed"; } //while (reader.Read()) //{ // messages.Add(item: new Messages { ID = (int)reader["ID"], CampaignName = (string)reader["CampaignName"], MessageDate = Convert.ToDateTime(reader["Date"]), Clicks = (int)reader["Clicks"], Conversions = (int)reader["Conversions"], Impressions = (int)reader["Impressions"], AffiliateName = (string)reader["AffiliateName"] }); //} } } return(messages); }
public void TestAutoSubscription() { var testHub = new MessagesHub(); var instance = new HandlerObject(); var subscriptions = MessagesUtils.AutoSubscribe(instance, testHub); using (Disposable.FromAction(() => { foreach (var s in subscriptions) { s.Subscription.Dispose(); } })) { string str1 = "value1"; string str2 = "value2"; int ival1 = 77; Assert.AreEqual(instance.StrValue, string.Empty); Assert.AreEqual(instance.IntValue, 0); NotificationMessage1.Broadcast(str1, testHub); Assert.AreEqual(instance.StrValue, str1); NotificationMessage2.Broadcast(str2, ival1, testHub); Assert.AreEqual(instance.StrValue, str2); Assert.AreEqual(instance.IntValue, ival1); } Assert.Throws <Exception>(() => { // all subscriptions must gone after using (...) block NotificationMessage1.Broadcast(string.Empty, testHub); }); }
public static void UnsubscribeWithinHandler() { var testHub = new MessagesHub(); int counter = 0; ISubscription subscription2 = null; var subscription1 = DefaultTestMessage.Subscribe(value => { counter += value; subscription2.Dispose(); }, null, testHub); subscription2 = DefaultTestMessage.Subscribe(value => { counter += value; subscription1.Dispose(); }, null, testHub); DefaultTestMessage.Subscribe(value => { counter += value; }, null, testHub); DefaultTestMessage.Broadcast(1, testHub); Assert.AreEqual(counter, 2); DefaultTestMessage.Broadcast(1, testHub); Assert.AreEqual(counter, 4); }
public Messages GetAllMessagesByID(int Id) { var messages = new Messages(); using (var connection = new SqlConnection(_connString)) { connection.Open(); using (var command = new SqlCommand("sp_select_All_By_Id", connection)) { command.Notification = null; var dependency = new SqlDependency(command); dependency.OnChange += new OnChangeEventHandler(dependency_OnChange); if (connection.State == ConnectionState.Closed) { connection.Open(); } command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@Id", Id); var reader = command.ExecuteReader(); //messages.Add(item: new Messages { ID = (int)reader["ID"], CampaignName = (string)reader["CampaignName"], MessageDate = Convert.ToDateTime(reader["Date"]), Clicks = (int)reader["Clicks"], Conversions = (int)reader["Conversions"], Impressions = (int)reader["Impressions"], AffiliateName = (string)reader["AffiliateName"] }); while (reader.Read()) { messages.ID = (int)reader["ID"]; messages.CampaignName = (string)reader["CampaignName"]; messages.MessageDate = Convert.ToDateTime(reader["Date"]); messages.Clicks = (int)reader["Clicks"]; messages.Impressions = (int)reader["Impressions"]; messages.Conversions = (int)reader["Conversions"]; messages.AffiliateName = (string)reader["AffiliateName"]; MessagesHub.UpdateStatus(messages.ID, "Editing"); } } } return(messages); }
public Smtp4devServer(Func <Smtp4devDbContext> dbContextFactory, IOptions <ServerOptions> serverOptions, MessagesHub messagesHub, SessionsHub sessionsHub) { this.dbContextFactory = dbContextFactory; this.smtpServer = new DefaultServer(serverOptions.Value.AllowRemoteConnections, serverOptions.Value.Port); this.smtpServer.MessageReceived += OnMessageReceived; this.smtpServer.SessionCompleted += OnSessionCompleted; this.messagesHub = messagesHub; this.sessionsHub = sessionsHub; }
public MessagesController(Smtp4devDbContext dbContext, MessagesHub messagesHub) { _dbContext = dbContext; _messagesHub = messagesHub; }
public Smtp4devServer(Func <Smtp4devDbContext> dbContextFactory, IOptions <ServerOptions> serverOptions, MessagesHub messagesHub, SessionsHub sessionsHub) { this.messagesHub = messagesHub; this.sessionsHub = sessionsHub; this.serverOptions = serverOptions; this.dbContextFactory = dbContextFactory; System.Security.Cryptography.X509Certificates.X509Certificate2 cert = null; Console.WriteLine($"\nTLS mode: {serverOptions.Value.TlsMode}"); if (serverOptions.Value.TlsMode != TlsMode.None) { if (!string.IsNullOrEmpty(serverOptions.Value.TlsCertificate)) { Console.WriteLine($"Using certificate from {serverOptions.Value.TlsCertificate}"); cert = new X509Certificate2(File.ReadAllBytes(serverOptions.Value.TlsCertificate), "", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable); } else { string pfxPath = Path.GetFullPath("selfsigned-certificate.pfx"); string cerPath = Path.GetFullPath("selfsigned-certificate.cer"); if (File.Exists(pfxPath)) { cert = new X509Certificate2(File.ReadAllBytes(pfxPath), "", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable); if (cert.Subject != $"CN={serverOptions.Value.HostName}" || DateTime.Parse(cert.GetExpirationDateString()) < DateTime.Now.AddDays(30)) { cert = null; } else { Console.WriteLine($"Using existing self-signed certificate with subject name '{serverOptions.Value.HostName} and expiry date {cert.GetExpirationDateString()}"); } } if (cert == null) { cert = CreateSelfSignedCertificate(); File.WriteAllBytes(pfxPath, cert.Export(X509ContentType.Pkcs12)); File.WriteAllBytes(cerPath, cert.Export(X509ContentType.Cert)); Console.WriteLine($"Generated new self-signed certificate with subject name '{serverOptions.Value.HostName} and expiry date {cert.GetExpirationDateString()}"); } Console.WriteLine($"Ensure that the hostname you enter into clients and '{serverOptions.Value.HostName}' from ServerOptions:HostName configuration match exactly"); Console.WriteLine($"and trust the issuer certificate at {cerPath} in your client/OS to avoid certificate validation errors."); } } else { Console.WriteLine("SSL/TLS is now disabled by default"); Console.WriteLine("To enable use set TlsMode option (values 'ImplicitTls' or 'StartTls') on command line or in appsettings.json and follow the instruction about hostname and certificate trust on first startup."); } Console.WriteLine(); ServerOptions serverOptionsValue = serverOptions.Value; this.smtpServer = new DefaultServer(serverOptionsValue.AllowRemoteConnections, serverOptionsValue.Port, serverOptionsValue.TlsMode == TlsMode.ImplicitTls ? cert : null, serverOptionsValue.TlsMode == TlsMode.StartTls ? cert : null ); this.smtpServer.MessageReceivedEventHandler += OnMessageReceived; this.smtpServer.SessionCompletedEventHandler += OnSessionCompleted; this.smtpServer.SessionStartedHandler += OnSessionStarted; this.smtpServer.AuthenticationCredentialsValidationRequiredEventHandler += OnAuthenticationCredentialsValidationRequired; }
public MessagesController(Smtp4devDbContext dbContext, MessagesHub messagesHub) { this.dbContext = dbContext; this.messagesHub = messagesHub; }
private void dependency_Onchange(object sender, SqlNotificationEventArgs e) { MessagesHub messagesHub = new MessagesHub(); messagesHub.SendMessages(); }
public virtual Task OnReconnected(MessagesHub hub) { return(Task.CompletedTask); }
public virtual Task OnDisconnected(MessagesHub hub, bool stopCalled) { return(Task.CompletedTask); }
public virtual async Task OnReconnected(MessagesHub hub) { }
public virtual async Task OnDisconnected(MessagesHub hub, bool stopCalled) { }
public void Test(string text) { MessagesHub.SendMessages(text); }