public void Subscribe(string channel, Action <string, string> handler) { if (_messageBroker == null) { return; } _messageBroker.Subscribe(channel, handler); Logger.Debug("{0} subscribed to {1}", _hostNameProvider.GetHostName(), channel); }
public void Subscribe(string channel, Action <string, string> handler) { if (_connection == null) { return; } try { var channelHandlers = _handlers.GetOrAdd(channel, c => { return(new ConcurrentBag <Action <string, string> >()); }); channelHandlers.Add(handler); Model.ExchangeDeclare(ExchangeName, ExchangeType.Direct, false, true); var queueName = Model.QueueDeclare().QueueName; Model.QueueBind(queueName, ExchangeName, channel); var consumer = new EventingBasicConsumer(Model); consumer.Received += (model, ea) => { var body = Encoding.UTF8.GetString(ea.Body); // the message contains the publisher before the first '/' var messageTokens = body.ToString().Split('/'); var publisher = messageTokens.FirstOrDefault(); var message = messageTokens.Skip(1).FirstOrDefault(); if (String.IsNullOrWhiteSpace(publisher)) { return; } // ignore self sent messages if (_hostNameProvider.GetHostName().Equals(publisher, StringComparison.OrdinalIgnoreCase)) { return; } Logger.Debug("Processing {0}", message); handler(ea.RoutingKey, message); }; Model.BasicConsume(queueName, true, consumer); } catch (Exception e) { Logger.Error(e, "An error occurred while subscribing to " + channel); } }
public ConnectionStringSettings GetConnectionString(string connectionName) { var hostName = _hostNameProvider.GetHostName(); var tenantName = GetTenantName(hostName); var connectionString = _connectionStringProvider.Get(tenantName); return(new ConnectionStringSettings(tenantName, connectionString)); }
private void ProcessMessages(IEnumerable <MessageRecord> messages) { if (!messages.Any()) { return; } // if this is the first time it's executed we just need to get the highest Id if (lastMessageId == 0) { lastMessageId = messages.Max(x => x.Id); return; } // process the messages synchronously and in order of publication foreach (var message in messages.OrderBy(x => x.Id)) { // save the latest message id so that next time the table is monitored // we get notified for new messages lastMessageId = message.Id; // only process handlers registered for the specific channel List <Action <string, string> > channelHandlers; if (_handlers.TryGetValue(message.Channel, out channelHandlers)) { var hostName = _hostNameProvider.GetHostName(); // execute subscription foreach (var handler in channelHandlers) { // ignore messages sent by the current host if (!message.Publisher.Equals(hostName, StringComparison.OrdinalIgnoreCase)) { handler(message.Channel, message.Message); } // stop processing other messages if stop has been required if (_stopped) { return; } } } } }
public string GetIdFor <T>() { var typeName = typeof(T).ToString(); var domain = _configDomainProvider.GetHostName(); var getIdUrl = GetUrlForDomain(domain); getIdUrl = getIdUrl + @"EntityId/GetId/" + typeName; var webRequest = (HttpWebRequest)WebRequest.Create(getIdUrl); string entityId; using (var response = webRequest.GetResponse()) { using (var stream = response.GetResponseStream()) { var streamReader = new StreamReader(stream); entityId = streamReader.ReadToEnd(); } } return(entityId); }
/// <summary> /// Verify /// </summary> /// <param name="a_smtpHost">Smtp host</param> /// <param name="a_emailAddress">Email address to veryfi</param> /// <returns>Status and message optional</returns> public Tuple <SmtpVerifyStatus, string> Verify(string a_smtpHost, string a_emailAddress) { try { string hostName = m_hostNameProvider.GetHostName(); TcpClient smtpTest = new TcpClient(); smtpTest.Connect(a_smtpHost, 25); if (!smtpTest.Connected) { return(new Tuple <SmtpVerifyStatus, string>(SmtpVerifyStatus.ServerNotExist, Resource.UnableToConnectToSmtpServer)); } using (NetworkStream ns = smtpTest.GetStream()) { using (StreamReader clearTextReader = new StreamReader(ns)) { using (StreamWriter clearTextWriter = new StreamWriter(ns) { AutoFlush = true }) { var responseConnected = clearTextReader.ReadLine(); if (GetResponseCode(responseConnected) != 220) { return(new Tuple <SmtpVerifyStatus, string>(SmtpVerifyStatus.UnableToConnect, responseConnected)); } clearTextWriter.WriteLine(string.Format("HELO {0}", hostName)); var responseHello = clearTextReader.ReadLine(); if (GetResponseCode(responseHello) != 250) { return(new Tuple <SmtpVerifyStatus, string>(SmtpVerifyStatus.UnableToConnect, responseHello)); } clearTextWriter.WriteLine(string.Format("MAIL FROM: <check@{0}>", hostName)); var responseMailFrom = clearTextReader.ReadLine(); if (GetResponseCode(responseMailFrom) != 250) { return(new Tuple <SmtpVerifyStatus, string>(SmtpVerifyStatus.UnableToConnect, responseMailFrom)); } clearTextWriter.WriteLine(string.Format("RCPT TO: <{0}>", a_emailAddress)); var responseRcptTo = clearTextReader.ReadLine(); int statusCode = GetResponseCode(responseRcptTo); clearTextWriter.WriteLine(string.Format("QUITE")); smtpTest.Close(); switch (statusCode) { case 250: return(new Tuple <SmtpVerifyStatus, string>(SmtpVerifyStatus.Ok, responseRcptTo)); case 550: return(new Tuple <SmtpVerifyStatus, string>(SmtpVerifyStatus.UserNotExist, responseRcptTo)); default: return(new Tuple <SmtpVerifyStatus, string>(SmtpVerifyStatus.ServerUnreliable, responseRcptTo)); } } } } } catch (Exception exception) { return(new Tuple <SmtpVerifyStatus, string>(SmtpVerifyStatus.ServerNotExist, exception.ToString())); } }
public S3StorageProvider(IHostNameProvider hostNameProvider) { BucketName = S3Utilities.GetBucketName(hostNameProvider.GetHostName()); ClientHelper = new S3ClientHelper(Config.AwsKey, Config.AwsSecretKey); }