Пример #1
0
        /// <summary>
        /// Constructs an uploader object.
        /// </summary>
        /// <param name="eventHubConnectionString">The Azure Stream Analytics connection string.</param>
        /// <param name="batchConfig">Optional; The batching configuration to used when uploading data.</param>
        /// <param name="developmentMode">If true, enables additional logging and disables batching.</param>
        public EventUploaderASA
        (
            string eventHubConnectionString,
            BatchingConfiguration batchConfig = null,
            bool developmentMode = false
        )
            : base(batchConfig, developmentMode)
        {
            this.connectionString = eventHubConnectionString;

            var builder = new ServiceBusConnectionStringBuilder(this.connectionString)
            {
                TransportType = TransportType.Amqp,
            };

            var eventHubInputName = builder.EntityPath;

            builder.EntityPath = null;

            if (this.batchConfig.ReUseTcpConnection)
            {
                this.client = EventHubClient.CreateFromConnectionString(builder.ToString(), eventHubInputName);
            }
            else
            {
                var factory = MessagingFactory.CreateFromConnectionString(builder.ToString());
                this.client = factory.CreateEventHubClient(eventHubInputName);
            }
        }
        static void Main()
        {
            //Build the Service Bus connection string (again, not necessary if using config from the config file)
            var connBuilder = new ServiceBusConnectionStringBuilder();

            connBuilder.ManagementPort = HttpPort;
            connBuilder.RuntimePort    = TcpPort;
            connBuilder.Endpoints.Add(new UriBuilder {
                Scheme = "sb", Host = ServerFqdn, Path = ServiceNamespace
            }.Uri);
            connBuilder.StsEndpoints.Add(new UriBuilder {
                Scheme = "https", Host = ServerFqdn, Port = HttpPort, Path = ServiceNamespace
            }.Uri);


            //Create a NamespaceManager instance (for management operations) and a MessagingFactory instance (for sending and receiving messages)
            //MessagingFactory messageFactory = MessagingFactory.Create();
            var messageFactory = MessagingFactory.CreateFromConnectionString(connBuilder.ToString());
            //NamespaceManager namespaceManager = NamespaceManager.Create();
            var namespaceManager = NamespaceManager.CreateFromConnectionString(connBuilder.ToString());

            //Create a new queue
            const string queueName = "ServiceBusQueueSample";

            if (namespaceManager == null)
            {
                Console.WriteLine("\nUnexpected Error: NamespaceManager is NULL");
                return;
            }
            if (namespaceManager.QueueExists(queueName))
            {
                namespaceManager.DeleteQueue(queueName);
            }
            namespaceManager.CreateQueue(queueName);

            //Create a queue client to send and receive messages to and from the queue
            var myQueueClient = messageFactory.CreateQueueClient(queueName);

            //Create a simple brokered message and send it to the queue
            var sendMessage = new BrokeredMessage("Hello World!");

            myQueueClient.Send(sendMessage);
            Console.WriteLine("Message sent: Body = {0}", sendMessage.GetBody <string>());

            //Receive the message from the queue
            var receivedMessage = myQueueClient.Receive(TimeSpan.FromSeconds(5));

            if (receivedMessage != null)
            {
                Console.WriteLine("Message received: Body = {0}", receivedMessage.GetBody <string>());
                receivedMessage.Complete();
            }

            //Close the connection to the Service Bus
            messageFactory.Close();

            Console.WriteLine("Press Enter to close.");
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            _serverFqdn = System.Net.Dns.GetHostEntry(string.Empty).HostName;

            var connectionStringBuilder = new ServiceBusConnectionStringBuilder { ManagementPort = HttpPort, RuntimePort = TcpPort };

            connectionStringBuilder.Endpoints.Add(new UriBuilder() { Scheme = "sb", Host = _serverFqdn, Path = ServiceNamespace }.Uri);
            connectionStringBuilder.StsEndpoints.Add(new UriBuilder() { Scheme = "https", Host = _serverFqdn, Port = HttpPort, Path = ServiceNamespace }.Uri);

            var messageFactory = MessagingFactory.CreateFromConnectionString(connectionStringBuilder.ToString());
            var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionStringBuilder.ToString());

            if (namespaceManager == null)
            {
                Console.WriteLine("\nUnexpected Error");
                return;
            }

            if (!namespaceManager.QueueExists(QueueName))
            {
                Console.WriteLine(@"Error: TALServiceBusQueue Queue des not Exist... Start your Queue first");
                return;
            }

            QueueClient queueClient = messageFactory.CreateQueueClient(QueueName);

            while (true)
            {
                try
                {
                    // Receive the message from the queue
                    BrokeredMessage receivedMessage = queueClient.Receive();

                    if (receivedMessage != null)
                    {
                        Console.WriteLine(@"Message received: {0}", receivedMessage.GetBody<ServiceBusEventMessage>());
                        receivedMessage.Complete();
                    }
                    else
                    {
                        Console.WriteLine(@"No new messages in the queue");
                        Thread.Sleep(1000);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Unexpected exception {0}", e);
                    throw;
                }
            }

            if (messageFactory != null)
            {
                messageFactory.Close();
            }

            //            Console.WriteLine("Press  ENTER to clean up and exit.");
            //            Console.ReadLine();
        }
        static void Main()
        {
            //Build the Service Bus connection string (again, not necessary if using config from the config file)
            var connBuilder = new ServiceBusConnectionStringBuilder();

            connBuilder.ManagementPort = HttpPort;
            connBuilder.RuntimePort    = TcpPort;
            connBuilder.Endpoints.Add(new UriBuilder {
                Scheme = "sb", Host = ServerFqdn, Path = ServiceNamespace
            }.Uri);
            connBuilder.StsEndpoints.Add(new UriBuilder {
                Scheme = "https", Host = ServerFqdn, Port = HttpPort, Path = ServiceNamespace
            }.Uri);


            var messageFactory   = MessagingFactory.CreateFromConnectionString(connBuilder.ToString());
            var namespaceManager = NamespaceManager.CreateFromConnectionString(connBuilder.ToString());

            const string topicName = "DemoTopic";
            //const string subscriptionName = "status_verifications";
            const string subscriptionName = "Subscription1";

            //Give the publisher some time to create the topic
            System.Threading.Thread.Sleep(5000);

            //Only uncomment to reset the subscription
            //namespaceManager.DeleteSubscription(topicName, subscriptionName);


            //Create the subscription (if it doesn't exist)
            if (!namespaceManager.SubscriptionExists(topicName, subscriptionName))
            {
                //Only subscribe to messages that are Type1
                var filter = new SqlFilter("Type = 'Type1'");

                namespaceManager.CreateSubscription(topicName, subscriptionName, filter);
            }


            // Create the subscription client
            var subscriptionClient = messageFactory.CreateSubscriptionClient(topicName, subscriptionName, ReceiveMode.PeekLock);

            Console.WriteLine(subscriptionName);
            Console.WriteLine("----------------");

            while (true)
            {
                var message = subscriptionClient.Receive(TimeSpan.FromSeconds(5));
                if (message != null)
                {
                    Console.WriteLine("{0} message received: Id = {1}; delivery count = {2}",
                                      subscriptionName, message.MessageId, message.DeliveryCount);
                    message.Abandon();
                }

                System.Threading.Thread.Sleep(10);
            }
        }
Пример #5
0
        static void Main(string[] args)
        {
            _serverFqdn = System.Net.Dns.GetHostEntry(string.Empty).HostName;

            var connectionStringBuilder = new ServiceBusConnectionStringBuilder {ManagementPort = HttpPort, RuntimePort = TcpPort};

            connectionStringBuilder.Endpoints.Add(new UriBuilder() { Scheme = "sb", Host = _serverFqdn, Path = ServiceNamespace }.Uri);
            connectionStringBuilder.StsEndpoints.Add(new UriBuilder() { Scheme = "https", Host = _serverFqdn, Port = HttpPort, Path = ServiceNamespace }.Uri);

            var messageFactory = MessagingFactory.CreateFromConnectionString(connectionStringBuilder.ToString());
            var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionStringBuilder.ToString());

            if (namespaceManager == null)
            {
                Console.WriteLine("\nUnexpected Error");
                return;
            }

            if (namespaceManager.QueueExists(QueueName))
            {
                namespaceManager.DeleteQueue(QueueName);
            }
            namespaceManager.CreateQueue(QueueName);

            QueueClient queueClient = messageFactory.CreateQueueClient(QueueName);
            DisplayHeader();

            var userInput = string.Empty;
            while (!ShouldExit(userInput))
            {
                try
                {
                    userInput = Console.ReadLine();
                    var numberOfMessages = userInput;

                    var messagesList = GetListOfMessages(numberOfMessages);

                    var startTime = DateTime.Now;
                    SendMessages(messagesList, queueClient);

                    var sendingTime = (DateTime.Now - startTime).TotalSeconds;
                    Console.WriteLine(string.Format("\n{0} Messages Sent.. [Total Time: {1} Secs]", messagesList.Count(), sendingTime));

                }
                catch (Exception e)
                {
                    Console.WriteLine("Unexpected exception {0}", e.ToString());
                    throw;
                }
            }

            if (messageFactory != null)
            {
                messageFactory.Close();
            }
        }
Пример #6
0
        public void Run(MessageIngestor.SetEventActionEventEmbedded setEventActionEventEmbedded)
        {
            try
            {
                SetEventActionEventEmbedded = setEventActionEventEmbedded;

                //Load message ingestor
                MessageIngestor.Init(SetEventActionEventEmbedded);
                // Assign the delegate

                // Load vars
                var eventHubConnectionString = ConfigurationLibrary.AzureNameSpaceConnectionString();
                var eventHubName             = ConfigurationLibrary.GroupEventHubsName();

                LogEngine.TraceInformation(
                    $"Start GrabCaster DownStream - Point Id {ConfigurationLibrary.PointId()} - Point name {ConfigurationLibrary.PointName()} - Channel Id {ConfigurationLibrary.ChannelId()} - Channel name {ConfigurationLibrary.ChannelName()} ");

                var builder = new ServiceBusConnectionStringBuilder(eventHubConnectionString)
                {
                    TransportType =
                        TransportType.Amqp
                };

                //If not exit it create one, drop brachets because Azure rules
                var eventHubConsumerGroup = string.Concat(ConfigurationLibrary.EngineName(), "_",
                                                          ConfigurationLibrary.ChannelId().Replace("{", "").Replace("}", "").Replace("-", ""));

                var nsManager = NamespaceManager.CreateFromConnectionString(builder.ToString());

                LogEngine.TraceInformation(
                    $"Start DirectRegisterEventReceiving. - Initializing Group Name {eventHubConsumerGroup}");

                // Create Event Hubs
                var eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString(), eventHubName);
                // Create consumer
                nsManager.CreateConsumerGroupIfNotExists(eventHubName, eventHubConsumerGroup);

                var namespaceManager = NamespaceManager.CreateFromConnectionString(builder.ToString());
                var ehDescription    = namespaceManager.GetEventHub(eventHubName);
                // Use the default consumer group

                foreach (var partitionId in ehDescription.PartitionIds)
                {
                    var myNewThread =
                        new Thread(() => ReceiveDirectFromPartition(eventHubClient, partitionId, eventHubConsumerGroup));
                    myNewThread.Start();
                }

                LogEngine.TraceInformation("After DirectRegisterEventReceiving Downstream running.");
            }
            catch (Exception ex)
            {
                LogEngine.TraceError(
                    $"Error in {MethodBase.GetCurrentMethod().Name} - Hint: Check if the firewall outbound port 5671 is opened. - Error {ex.Message}");
            }
        }
        static void Main(string[] args)
        {
            timer.Elapsed += TimerElapsed;

            //Build the Service Bus connection string (again, not necessary if using config from the config file)
            var connBuilder = new ServiceBusConnectionStringBuilder();

            connBuilder.ManagementPort = HttpPort;
            connBuilder.RuntimePort    = TcpPort;
            connBuilder.Endpoints.Add(new UriBuilder()
            {
                Scheme = "sb", Host = ServerFQDN, Path = ServiceNamespace
            }.Uri);
            connBuilder.StsEndpoints.Add(new UriBuilder()
            {
                Scheme = "https", Host = ServerFQDN, Port = HttpPort, Path = ServiceNamespace
            }.Uri);


            var messageFactory   = MessagingFactory.CreateFromConnectionString(connBuilder.ToString());
            var namespaceManager = NamespaceManager.CreateFromConnectionString(connBuilder.ToString()); //connBuilder.ToString()

            const string TopicName        = "DemoTopic";
            const string SubscriptionName = "Subscription2";

            //Create the subscription (if it doesn't exist)
            if (!namespaceManager.SubscriptionExists(TopicName, SubscriptionName))
            {
                var inventorySubscription = namespaceManager.CreateSubscription(TopicName, SubscriptionName);
            }

            // Create the subscription client
            var agentSubscriptionClient = messageFactory.CreateSubscriptionClient(TopicName, SubscriptionName, ReceiveMode.PeekLock);

            Console.WriteLine(SubscriptionName);
            Console.WriteLine("----------------");

            while (true)
            {
                var message = agentSubscriptionClient.Receive(TimeSpan.FromSeconds(5));
                if (message != null)
                {
                    Console.WriteLine(string.Format("{0} message received: Id = {1}, Body = {2}", SubscriptionName, message.MessageId, message.GetBody <string>()));
                    message.Complete();

                    numMessages++;

                    //When we receive a message, stop and restart the timer.  If the timer ever ticks, then display how many messages were processed
                    timer.Stop();
                    timer.Start();
                }

                System.Threading.Thread.Sleep(10);
            }
        }
Пример #8
0
        static void Main()
        {
            //Build the Service Bus connection string (again, not necessary if using config from the config file)
            var connBuilder = new ServiceBusConnectionStringBuilder();

            connBuilder.ManagementPort = HttpPort;
            connBuilder.RuntimePort    = TcpPort;
            connBuilder.Endpoints.Add(new UriBuilder {
                Scheme = "sb", Host = ServerFqdn, Path = ServiceNamespace
            }.Uri);
            connBuilder.StsEndpoints.Add(new UriBuilder {
                Scheme = "https", Host = ServerFqdn, Port = HttpPort, Path = ServiceNamespace
            }.Uri);


            var messageFactory   = MessagingFactory.CreateFromConnectionString(connBuilder.ToString());
            var namespaceManager = NamespaceManager.CreateFromConnectionString(connBuilder.ToString());

            const string topicName        = "DemoTopic";
            const string subscriptionName = "Subscription2";

            //Give the publisher some time to create the topic
            System.Threading.Thread.Sleep(5000);

            //Create the subscription (if it doesn't exist)
            if (!namespaceManager.SubscriptionExists(topicName, subscriptionName))
            {
                //Do not filter messages at all, so it will be subscribed to ALL messages, regardless of type

                namespaceManager.CreateSubscription(topicName, subscriptionName);
            }

            // Create the subscription client
            var subscriptionClient = messageFactory.CreateSubscriptionClient(topicName, subscriptionName, ReceiveMode.PeekLock);

            Console.WriteLine(subscriptionName);
            Console.WriteLine("----------------");

            while (true)
            {
                var message = subscriptionClient.Receive(TimeSpan.FromSeconds(5));
                if (message != null)
                {
                    Console.WriteLine("{0} message received: Id = {1}, Body = {2}, Type = {3}",
                                      subscriptionName, message.MessageId, message.GetBody <string>(), message.Properties["Type"]);
                    message.Complete();
                }

                System.Threading.Thread.Sleep(10);
            }
        }
Пример #9
0
        /// <summary>
        /// Builds the MessagingFactory and NamespaceManager for the ServiceBus
        /// which are used for managing the service bus itself and creating senders and receivers
        /// </summary>
        /// <param name="serviceNamespace"></param>
        /// <param name="serverFullyQualifiedDomainName"></param>
        /// <param name="httpPort"></param>
        /// <param name="tcpPort"></param>
        public async Task <bool> Connect(string serviceNamespace, string serverFullyQualifiedDomainName, int httpPort, int tcpPort)
        {
            Trace($"Namespace: {serviceNamespace}, FQDN: {serverFullyQualifiedDomainName}, httpPort: {httpPort}, tcpPort: {tcpPort}");
            if (MessagingFactory != null && NamespaceManager != null)
            {
                return(true);                                                      //only one connection. could use a disconnect method
            }
            ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder();

            builder.ManagementPort = httpPort;
            builder.RuntimePort    = tcpPort;
            builder.Endpoints.Add(new UriBuilder()
            {
                Scheme = "sb",
                Host   = serverFullyQualifiedDomainName,
                Path   = serviceNamespace
            }.Uri);
            builder.StsEndpoints.Add(new UriBuilder()
            {
                Scheme = "https",
                Host   = serverFullyQualifiedDomainName,
                Port   = httpPort,
                Path   = serviceNamespace
            }.Uri);
            var serverAddress = builder.ToString();

            return(await Connect(serverAddress));
        }
        public void ConnectionStringBuilderShouldTrimTrailingSemicolon()
        {
            var csBuilder = new ServiceBusConnectionStringBuilder
            {
                Endpoint   = " amqps://contoso.servicebus.windows.net",
                SasKeyName = " keyname "
            };

            Assert.Equal("Endpoint=amqps://contoso.servicebus.windows.net;SharedAccessKeyName=keyname", csBuilder.ToString());

            csBuilder.SasKeyName = "";
            Assert.Equal("Endpoint=amqps://contoso.servicebus.windows.net", csBuilder.ToString());

            csBuilder.EntityPath = "myQ";
            Assert.Equal("Endpoint=amqps://contoso.servicebus.windows.net;EntityPath=myQ", csBuilder.ToString());

            csBuilder.EntityPath    = "";
            csBuilder.TransportType = TransportType.AmqpWebSockets;
            Assert.Equal("Endpoint=amqps://contoso.servicebus.windows.net;TransportType=AmqpWebSockets", csBuilder.ToString());

            csBuilder.OperationTimeout = TimeSpan.FromSeconds(42);
            Assert.Equal("Endpoint=amqps://contoso.servicebus.windows.net;TransportType=AmqpWebSockets;OperationTimeout=00:00:42", csBuilder.ToString());

            csBuilder.ConnectionIdleTimeout = TimeSpan.FromSeconds(42);
            Assert.Equal("Endpoint=amqps://contoso.servicebus.windows.net;TransportType=AmqpWebSockets;OperationTimeout=00:00:42;ConnectionIdleTimeout=00:00:42", csBuilder.ToString());
        }
        static string GetEventHubConnectionString()
        {
            var connectionString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"];

            if (string.IsNullOrEmpty(connectionString))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Did not find Service Bus connections string in appsettings (app.config)");
                Console.ResetColor();
                return(string.Empty);
            }

            try
            {
                var builder = new ServiceBusConnectionStringBuilder(connectionString);
                builder.TransportType = TransportType.Amqp;
                return(builder.ToString());
            }
            catch (Exception exception)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(exception.Message);
                Console.ResetColor();
            }

            return(string.Empty);
        }
Пример #12
0
        static void Main(string[] args)
        {
            Console.WriteLine("Receive messages. Ctrl-C to exit.\n");

            var builder = new ServiceBusConnectionStringBuilder(connectionString)
            {
                TransportType = TransportType.Amqp,
            };

            eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString(), iotHubD2cEndpoint);

            var d2cPartitions = eventHubClient.GetRuntimeInformation().PartitionIds;

            CancellationTokenSource cts = new CancellationTokenSource();

            System.Console.CancelKeyPress += (s, e) =>
            {
                e.Cancel = true;
                cts.Cancel();
                Console.WriteLine("Exiting...");
            };

            var tasks = new List <Task>();

            foreach (string partition in d2cPartitions)
            {
                tasks.Add(ReceiveMessagesFromDeviceAsync(partition, cts.Token));
            }
            Task.WaitAll(tasks.ToArray());
        }
Пример #13
0
        EventProcessorHost IEventHubProvider.GetEventProcessorHost(string eventHubName, string consumerGroup)
        {
            ReceiverCreds creds;

            if (this._receiverCreds.TryGetValue(eventHubName, out creds))
            {
                // Common case. Create a new EventProcessorHost instance to listen.
                string eventProcessorHostName = Guid.NewGuid().ToString();

                if (consumerGroup == null)
                {
                    consumerGroup = EventHubConsumerGroup.DefaultGroupName;
                }
                var storageConnectionString = creds.StorageConnectionString;
                if (storageConnectionString == null)
                {
                    storageConnectionString = _defaultStorageString;
                }

                // If the connection string provides a hub name, that takes precedence.
                // Note that connection strings *can't* specify a consumerGroup, so must always be passed in.
                string actualPath = eventHubName;
                ServiceBusConnectionStringBuilder sb = new ServiceBusConnectionStringBuilder(creds.EventHubConnectionString);
                if (sb.EntityPath != null)
                {
                    actualPath    = sb.EntityPath;
                    sb.EntityPath = null; // need to remove to use with EventProcessorHost
                }

                var @namespace = GetServiceBusNamespace(sb);
                var blobPrefix = GetBlobPrefix(actualPath, @namespace);

                // Use blob prefix support available in EPH starting in 2.2.6
                EventProcessorHost host = new EventProcessorHost(
                    hostName: eventProcessorHostName,
                    eventHubPath: actualPath,
                    consumerGroupName: consumerGroup,
                    eventHubConnectionString: sb.ToString(),
                    storageConnectionString: storageConnectionString,
                    leaseContainerName: LeaseContainerName,
                    leaseBlobPrefix: blobPrefix);

                if (_partitionOptions != null)
                {
                    host.PartitionManagerOptions = _partitionOptions;
                }

                return(host);
            }
            else
            {
                // Rare case: a power-user caller specifically provided an event processor host to use.
                EventProcessorHost host;
                if (_explicitlyProvidedHosts.TryGetValue(eventHubName, out host))
                {
                    return(host);
                }
            }
            throw new InvalidOperationException("No event hub receiver named " + eventHubName);
        }
        static void Main(string[] args)
        {
            string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
            string eventHubName     = CloudConfigurationManager.GetSetting("EventHubName");

            if (string.IsNullOrEmpty(connectionString) || string.IsNullOrEmpty(eventHubName))
            {
                Trace.WriteLine("One or more parameters missing in appsettings (app.config)");
                return;
            }

            // Change Culture settings to en-US to ensure formmatting of CSV data is correct
            System.Globalization.CultureInfo.DefaultThreadCurrentCulture = new System.Globalization.CultureInfo("en-US");

            ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connectionString);

            builder.TransportType = TransportType.Amqp;
            var namespaceManager = NamespaceManager.CreateFromConnectionString(builder.ToString());
            int numPartitions    = 16;

            EventHubManager.CreateEventHubIfNotExists(eventHubName, numPartitions, namespaceManager);

            var driver = new StockMarketDriver(eventHubName, numPartitions);
            var token  = tokenSource.Token;

            Task.Factory.StartNew(() => driver.Run(token));
            Task.Factory.StartNew(() => driver.WaitForEnter(tokenSource));
            driver.runCompleteEvent.WaitOne();
        }
Пример #15
0
        public AzureSBQSender(string connectionString, string queueName, int numberOfSenders)
        {
            NumberOfSenders = numberOfSenders;

            var connBuilder      = new ServiceBusConnectionStringBuilder(connectionString);
            var namespaceManager = NamespaceManager.CreateFromConnectionString(connBuilder.ToString());

            // Create queue.
            if (!namespaceManager.QueueExists(queueName))
            {
                namespaceManager.CreateQueue(queueName);
            }

            // Create messaging factories, senders and receivers.
            _messagingFactory = new MessagingFactory[NumberOfSenders];
            Sender            = new MessageSender[NumberOfSenders];

            // Create senders.
            var factoryIndex = 0;

            for (var i = 0; i < NumberOfSenders; i++)
            {
                _messagingFactory[factoryIndex] = MessagingFactory.CreateFromConnectionString(connBuilder.ToString());
                Sender[i] = _messagingFactory[factoryIndex++].CreateMessageSender(queueName);
            }
        }
        public async Task<string> OpenAsync(CancellationToken cancellationToken)
        {
            var builder = new ServiceBusConnectionStringBuilder(_connectionString)
            {
                TransportType = TransportType.Amqp
            };
            _messagingFactory = MessagingFactory.CreateFromConnectionString(builder.ToString());
            _eventHubClient = _messagingFactory.CreateEventHubClient(_eventHubName);
            _consumerGroup = !string.IsNullOrEmpty(_consumerGroupName)
                ? _eventHubClient.GetConsumerGroup(_consumerGroupName)
                : _eventHubClient.GetDefaultConsumerGroup();

            _eventProcessorFactory = new EventProcessorFactory();
            _leaseRepository = new ReliableStateLeaseRepository(_reliableStateManager);
            _checkpointManager = new CheckpointManager(_leaseRepository); 

            var allocatedPartitions = await new EventHubPartitionPartitionAllocationStrategy(_serviceName, _partitionId)
                .AllocateAsync(_eventHubClient, new FabricClient());

            foreach (var partition in allocatedPartitions)
            {
                var lease = await _leaseRepository.GetOrCreateAsync(_connectionString, _consumerGroupName, _eventHubName, partition);

                await _consumerGroup.RegisterProcessorFactoryAsync(lease, _checkpointManager, _eventProcessorFactory);
            }

            return string.Concat(_eventHubName, " @ ", _connectionString);
        }
        public static bool CreateEventUpStream()
        {
            try
            {
                Debug.WriteLine("-------------- Engine LogEventUpStream --------------");
                Debug.WriteLine("LogEventUpStream - Get Configuration settings.");
                //Event Hub Configuration
                azureNameSpaceConnectionString = ConfigurationBag.Configuration.AzureNameSpaceConnectionString;
                eventHubName = ConfigurationBag.Configuration.LoggingComponentStorage;
                Debug.WriteLine($"LogEventUpStream - azureNameSpaceConnectionString={azureNameSpaceConnectionString}");
                Debug.WriteLine($"LogEventUpStream - eventHubName={eventHubName}");

                // TODO ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Https;

                var builder = new ServiceBusConnectionStringBuilder(azureNameSpaceConnectionString)
                {
                    TransportType =
                        TransportType
                        .Amqp
                };

                Debug.WriteLine("LogEventUpStream - Create the eventHubClient.");

                eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString(), eventHubName);
                return(true);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("LogEventUpStream - error->{0}", ex.Message);
                EventLog.WriteEntry("Framework.Log.EventHubs", ex.Message);
                return(false);
            }
        }
        private void CreateConnectionData(IConfigurationProvider configurationProvider)
        {
            string serviceBusConnectionString = configurationProvider.GetValue("serviceBusConnectionString");
            if (string.IsNullOrWhiteSpace(serviceBusConnectionString))
            {
                throw new ConfigurationErrorsException(
                    "Configuration parameter 'serviceBusConnectionString' must be set to a valid Service Bus connection string");
            }

            string eventHubName = configurationProvider.GetValue("eventHubName");
            if (string.IsNullOrWhiteSpace(eventHubName))
            {
                throw new ConfigurationErrorsException("Configuration parameter 'eventHubName' must not be empty");
            }

            this.connectionData = new EventHubConnectionData();
            this.connectionData.EventHubName = eventHubName;

            ServiceBusConnectionStringBuilder connStringBuilder = new ServiceBusConnectionStringBuilder(serviceBusConnectionString);
            connStringBuilder.TransportType = TransportType.Amqp;
            this.connectionData.MessagingFactories = new MessagingFactory[ConcurrentConnections];
            for (uint i = 0; i < ConcurrentConnections; i++)
            {
                this.connectionData.MessagingFactories[i] = MessagingFactory.CreateFromConnectionString(connStringBuilder.ToString());
            }
        }
Пример #19
0
        /// <summary>
        /// pull connection string for Event Hub from the Settings.xml file in this project
        /// </summary>
        /// <returns></returns>
        private string GetEventHubConnectionString()
        {
            ConfigurationSettings settingsFile =
                ActorService.Context.CodePackageActivationContext.GetConfigurationPackageObject("Config").Settings;
            ConfigurationSection configSection = settingsFile.Sections["Config"];

            var connectionString = configSection.Parameters["EventHub"].Value;

            if (string.IsNullOrEmpty(connectionString))
            {
                Console.WriteLine("Did not find Service Bus connections string in appsettings (app.config)");
                return(string.Empty);
            }
            try
            {
                var builder = new ServiceBusConnectionStringBuilder(connectionString);
                builder.TransportType = TransportType.Amqp;
                return(builder.ToString());
            }
            catch (Exception exception)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(exception.Message);
                Console.ResetColor();
            }

            return(null);
        }
Пример #20
0
        public bool CreateEventUpStream()
        {
            try
            {
                //EH Configuration
                connectionString = ConfigurationLibrary.AzureNameSpaceConnectionString();
                eventHubName     = ConfigurationLibrary.GroupEventHubsName();

                LogEngine.TraceInformation($"Start GrabCaster UpStream - Point Id {ConfigurationLibrary.PointId()} - Point name {ConfigurationLibrary.PointName()} - Channel Id {ConfigurationLibrary.ChannelId()} - Channel name {ConfigurationLibrary.ChannelName()} ");

                var builder = new ServiceBusConnectionStringBuilder(connectionString)
                {
                    TransportType =
                        TransportType.Amqp
                };

                eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString(), eventHubName);

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Пример #21
0
        public AzureSBQRecevier(string connectionString, string queueName, int numberOfReceivers, int prefetchCount)
        {
            NumberOfReceivers = numberOfReceivers;

            var connBuilder      = new ServiceBusConnectionStringBuilder(connectionString);
            var namespaceManager = NamespaceManager.CreateFromConnectionString(connBuilder.ToString());

            // Create queue.
            if (!namespaceManager.QueueExists(queueName))
            {
                namespaceManager.CreateQueue(queueName);
            }

            // Create messaging factories, senders and receivers.
            _messagingFactory = new MessagingFactory[NumberOfReceivers];
            Receiver          = new MessageReceiver[NumberOfReceivers];

            var factoryIndex = 0;

            // Create receivers.
            for (var i = 0; i < NumberOfReceivers; i++)
            {
                _messagingFactory[factoryIndex] = MessagingFactory.CreateFromConnectionString(connBuilder.ToString());
                Receiver[i] = _messagingFactory[factoryIndex++].CreateMessageReceiver(queueName, ReceiveMode.PeekLock);
                Receiver[i].PrefetchCount = prefetchCount;
            }
        }
Пример #22
0
        static string GetAmqpConnectionString()
        {
            ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connectionString);

            builder.TransportType = TransportType.Amqp;
            return(builder.ToString());
        }
        public bool CreateOffRampStream()
        {
            try
            {
                //EH Configuration
                connectionString = ConfigurationBag.Configuration.AzureNameSpaceConnectionString;
                eventHubName     = ConfigurationBag.Configuration.GroupEventHubsName;

                LogEngine.WriteLog(
                    ConfigurationBag.EngineName,
                    $"Event Hubs transfort Type: {ConfigurationBag.Configuration.ServiceBusConnectivityMode}",
                    Constant.LogLevelError,
                    Constant.TaskCategoriesError,
                    null,
                    Constant.LogLevelInformation);

                var builder = new ServiceBusConnectionStringBuilder(connectionString)
                {
                    TransportType =
                        TransportType.Amqp
                };

                eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString(), eventHubName);

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Пример #24
0
        public void Execute(SetEventActionTrigger setEventActionTrigger, EventActionContext context)
        {
            try
            {
                this.Context = context;
                this.SetEventActionTrigger = setEventActionTrigger;

                // Create the connection string
                var builder = new ServiceBusConnectionStringBuilder(this.EventHubsConnectionString)
                {
                    TransportType =
                        TransportType
                        .Amqp
                };

                // Create the EH Client
                var eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString(), this.EventHubsName);

                // muli partition sample
                var namespaceManager    = NamespaceManager.CreateFromConnectionString(builder.ToString());
                var eventHubDescription = namespaceManager.GetEventHub(this.EventHubsName);

                // Use the default consumer group
                foreach (var partitionId in eventHubDescription.PartitionIds)
                {
                    var myNewThread = new Thread(() => this.ReceiveDirectFromPartition(eventHubClient, partitionId));
                    myNewThread.Start();
                }
            }
            catch (Exception)
            {
                // ignored
            }
        }
        static void Main(string[] args)
        {
            var builder = new ServiceBusConnectionStringBuilder("");
            builder.TransportType = TransportType.Amqp;
            
            var factory = MessagingFactory.CreateFromConnectionString(builder.ToString());
            var client = factory.CreateEventHubClient("iotsample");

            Console.WriteLine("Hit enter to send a batch of messages, x to exit");
            var x = Console.ReadLine();

            while (x != "x")
            {
                var random = new Random();
                var messages = new List<EventData>();
                for (int i = 0; i < 1000; i++)
                {
                    var message = new
                    {
                        deviceId = random.Next(0, 1)
                    };
                    var body = JsonConvert.SerializeObject(message);
                    messages.Add(new EventData(Encoding.UTF8.GetBytes(body)));
                }

                client.SendBatch(messages);

                Console.WriteLine("Batch sent, hit enter to send the next batch, x to exit");
                x = Console.ReadLine();
            }

        }
Пример #26
0
        static string ReadConnectionString()
        {
            string connectionString = ConnectionString;

            do
            {
                if (connectionString == null)
                {
                    Console.Write("\nEnter Service Bus Connection String or type 'exit': ");
                    connectionString = Console.ReadLine();
                    if (string.Equals(connectionString, "exit", StringComparison.OrdinalIgnoreCase))
                    {
                        Environment.Exit(0);
                    }
                }

                try
                {
                    ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connectionString);
                    connectionString = builder.ToString();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: {0}", ex.Message);
                    connectionString = null;
                }
            } while (connectionString == null);

            return(connectionString);
        }
        static void Main(string[] args)
        {
            var connBuilder = new ServiceBusConnectionStringBuilder();
            connBuilder.ManagementPort = HttpPort;
            connBuilder.RuntimePort = TcpPort;
            connBuilder.Endpoints.Add(new Uri("sb://Niranga/ServiceBusDefaultNamespace"));

            connBuilder.StsEndpoints.Add(new Uri("https://Niranga:9355/ServiceBusDefaultNamespace"));

            var messagingFactory = MessagingFactory.CreateFromConnectionString(connBuilder.ToString());
            var namespaceManager = NamespaceManager.CreateFromConnectionString(connBuilder.ToString());

            if (namespaceManager == null)
            {
                Console.WriteLine("Error");
                return;
            }

            string queueName = "ServiceBusQueueSample";

            if (namespaceManager.QueueExists(queueName))
            {
                namespaceManager.DeleteQueue(queueName);
            }

            namespaceManager.CreateQueue(queueName);

            var queueClient = messagingFactory.CreateQueueClient(queueName);

            // Send
            var brokeredMessage = new BrokeredMessage("Hello");
            queueClient.Send(brokeredMessage);

            // Receive
            var receiveMessage = queueClient.Receive(TimeSpan.FromSeconds(5));

            if (receiveMessage != null)
            {
                Console.WriteLine("Received '{0}'", receiveMessage.GetBody<string>());
                receiveMessage.Complete();
            }

            if (messagingFactory != null)
            {
                messagingFactory.Close();
            }
        }
        private static MessageReceiver CreateDeadLetterQueueClient(string connectionString, string topicName, string subscriptionName)
        {
            var builder             = new ServiceBusConnectionStringBuilder(connectionString);
            var factory             = MessagingFactory.CreateFromConnectionString(builder.ToString());
            var deadLetterQueuePath = SubscriptionClient.FormatDeadLetterPath(topicName, subscriptionName);

            return(factory.CreateMessageReceiver(deadLetterQueuePath));
        }
Пример #29
0
        public EventPublisher(string connectionString, string eventHubName, string partitionKey, ILogger logger)
        {
            var builder = new ServiceBusConnectionStringBuilder(connectionString) { TransportType = TransportType.Amqp };

            _partitionKey = partitionKey;
            _client = EventHubClient.CreateFromConnectionString(builder.ToString(), eventHubName);
            _logger = logger;
        }
        public void InitializeEventHub()
        {
            var builder = new ServiceBusConnectionStringBuilder();

            builder.Endpoints.Add(new Uri("sb://" + this.appConfig.EventHubNamespace + "." + this.appConfig.EventHubFqnAddress));
            builder.EntityPath          = this.appConfig.EventHubEntityPath;
            builder.SharedAccessKeyName = this.appConfig.EventHubUsername;
            builder.SharedAccessKey     = this.appConfig.EventHubPassword;
            builder.TransportType       = TransportType.Amqp;

            Context.Logger.Info("EventHubWriter: ConnectionString = {0} ParitionId = {1}",
                                builder.ToString(), Context.TopologyContext.GetThisTaskIndex());

            eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString());
            //TODO: Implement a distribution strategy of partitions in case number of bolt tasks is less than partitions in EventHub
            eventHubSender = eventHubClient.CreatePartitionedSender(Context.TopologyContext.GetThisTaskIndex().ToString());
        }
Пример #31
0
        private QueueClient CreateClient(string entityName)
        {
            var connectionStringBuilder = new ServiceBusConnectionStringBuilder(configuration.ConnectionString)
            {
                EntityPath = entityName
            };

            return(QueueClient.CreateFromConnectionString(connectionStringBuilder.ToString()));
        }
Пример #32
0
        internal static string GetEntityConnectionString(string entityName)
        {
            // If the entity name is populated in the connection string, it will be overridden.
            var connectionStringBuilder = new ServiceBusConnectionStringBuilder(NamespaceConnectionString)
            {
                EntityPath = entityName
            };

            return(connectionStringBuilder.ToString());
        }
Пример #33
0
        public Task OpenEventHubAsync(string eventHubConnectionString, string hubName)
        {
            return(Task.Factory.StartNew(async() => {
                if (string.IsNullOrWhiteSpace(eventHubConnectionString))
                {
                    throw new ArgumentException("invalid event hub connection string");
                }

                if (string.IsNullOrWhiteSpace(hubName))
                {
                    throw new ArgumentException("invalid hubname");
                }
                this.IsOpen = true;

                var builder = new ServiceBusConnectionStringBuilder(eventHubConnectionString)
                {
                    TransportType = TransportType.Amqp
                };
                var s = builder.ToString();
                _factory = MessagingFactory.CreateFromConnectionString(builder.ToString());

                _client = _factory.CreateEventHubClient(hubName);

                var runtimeInfo = await _client.GetRuntimeInformationAsync();

                _partitions.Clear();
                foreach (var p in runtimeInfo.PartitionIds)
                {
                    var partition = await _client.GetPartitionRuntimeInformationAsync(p);
                    var count = partition.LastEnqueuedSequenceNumber - partition.BeginSequenceNumber;
                    if (count != 0)
                    {
                        count++;
                    }
                    this.MessageCount = count;
                    _partitions.Add(new Partition(p, _messages));
                    _foundPartitions.OnNext(partition);
                }

                _foundPartitions.OnCompleted();
            }));
        }
        /// <summary>
        /// Returns Service Bus connection string to use.
        /// </summary>
        public string BuildConnectionString()
        {
            if (OperationTimeout != null)
            {
                var connectionStringBuilder = new ServiceBusConnectionStringBuilder(ConnectionString);
                connectionStringBuilder.OperationTimeout = OperationTimeout.Value;
                return(connectionStringBuilder.ToString());
            }

            return(ConnectionString);
        }
Пример #35
0
        public EventPublisher(string connectionString, string eventHubName, string partitionKey, ILogger logger)
        {
            var builder = new ServiceBusConnectionStringBuilder(connectionString)
            {
                TransportType = TransportType.Amqp
            };

            _partitionKey = partitionKey;
            _client       = EventHubClient.CreateFromConnectionString(builder.ToString(), eventHubName);
            _logger       = logger;
        }
Пример #36
0
        public void SetUp()
        {
            var serverFQDN = Environment.MachineName;
            const int HttpPort = 9355;
            const int TcpPort = 9354;
            const string ServiceNamespace = "ServiceBusDefaultNamespace";

            var connBuilder = new ServiceBusConnectionStringBuilder { ManagementPort = HttpPort, RuntimePort = TcpPort };
            connBuilder.Endpoints.Add(new UriBuilder { Scheme = "sb", Host = serverFQDN, Path = ServiceNamespace }.Uri);
            connBuilder.StsEndpoints.Add(new UriBuilder { Scheme = "https", Host = serverFQDN, Port = HttpPort, Path = ServiceNamespace }.Uri);
            _testBus = Bus.CreateBus(cfg => cfg.WithConnectionString(connBuilder.ToString()).WithLogger(new TestLogger())).Result;
        }
Пример #37
0
 private static string GetServiceBusConnectionString()
 {
     string connectionString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"];
     if (string.IsNullOrEmpty(connectionString))
     {
         Console.WriteLine("Did not find Service Bus connections string in appsettings (app.config)");
         return string.Empty;
     }
     ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connectionString);
     builder.TransportType = TransportType.Amqp;
     return builder.ToString();
 }
Пример #38
0
        /// <summary>
        ///
        /// </summary>
        public void CreateReceiver()
        {
            if (!isRunning)
            {
                isRunning = true;
                var    connectionString = Constants.GstnServiceBusEventHub;
                string eventhubPath     = Constants.GstnReqEventHub;
                var    nsm = NamespaceManager.CreateFromConnectionString(connectionString);
                //var description = nsm.CreateEventHubIfNotExists(eventhubPath);
                var builder = new ServiceBusConnectionStringBuilder(connectionString)
                {
                    TransportType = TransportType.Amqp
                };
                var factory     = MessagingFactory.CreateFromConnectionString(builder.ToString());
                var client      = factory.CreateEventHubClient(eventhubPath);
                var group       = client.GetDefaultConsumerGroup();
                var requestdata = new WEP.GSP.Data.RequestData();
                try
                {
                    var receiverList       = new List <EventHubReceiver>();
                    var availablePartition = requestdata.GetInstancePartitions(System.Environment.MachineName + ConfigurationManager.AppSettings["MachineName"].ToString());
                    if (availablePartition != null && availablePartition.Any())
                    {
                        foreach (var partitionno in availablePartition)
                        {
                            receiverList.Add(group.CreateReceiver(partitionno, requestdata.RetrievePartitionOffset(partitionno)));
                        }
                        ;

                        var taskFactory = new TaskFactory();

                        var task = (
                            from r in receiverList
                            select taskFactory.StartNew(() =>
                        {
                            Task.Delay(TimeSpan.FromSeconds(1));
                            StartReceiver(Convert.ToInt32(r.PartitionId), r);
                        })).ToList();

                        Task.WaitAll(task.ToArray());
                    }
                    isRunning = false;
                }
                catch (Exception e)
                {
                    isRunning = false;
                    Trace.TraceInformation(e.Message);
                }
            }
            System.Threading.Thread.Sleep(60000);
            CreateReceiver();
        }
Пример #39
0
        private string GetServiceBusConnectionString()
        {
            string connectionString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"];

            if (string.IsNullOrEmpty(connectionString))
            {
                return(string.Empty);
            }
            ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connectionString);

            builder.TransportType = TransportType.Amqp;
            return(builder.ToString());
        }
 private string GetServiceBusConnectionString()
 {
         string connectionString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"];
         if (string.IsNullOrEmpty(connectionString))
         {
             return string.Empty;
         }
         ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connectionString);
         builder.TransportType = TransportType.Amqp;
         return builder.ToString();
     
    
 }
Пример #41
0
        private static string GetServiceBusConnectionString(string activityType)
        {
            string appSettingsKey = string.Format("ServiceBus{0}", activityType);
            string connectionString = ConfigurationManager.AppSettings[appSettingsKey];

            if (string.IsNullOrEmpty(connectionString))
            {
                Console.WriteLine("Did not find Service Bus connections string in appsettings (app.config)");
                return string.Empty;
            }
            ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connectionString);
            builder.TransportType = TransportType.Amqp;
            return builder.ToString();
        }
        public void Build()
        {
            var connectionBuilder = new ServiceBusConnectionStringBuilder();
            connectionBuilder.ManagementPort = HttpPort;
            connectionBuilder.RuntimePort = TcpPort;

            // Service Bus end-point
            connectionBuilder.Endpoints.Add(new Uri(ServiceBusEndPont));

            // STS End-point
            connectionBuilder.StsEndpoints.Add(new Uri(SecureTokenServiceEndPoint));

            ConnectionString = connectionBuilder.ToString();
        }
Пример #43
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LargePublishPerformance"/> class.
        /// </summary>
        public LargePublishPerformance()
        {
            var serverFQDN = Environment.MachineName;
            const int HttpPort = 9355;
            const int TcpPort = 9354;
            const string ServiceNamespace = "ServiceBusDefaultNamespace";

            // Common.Logging.LogManager.Adapter = new Common.Logging.NLog.NLogLoggerFactoryAdapter(new NameValueCollection { { "configType", "FILE" }, { "configFile", "~/NLog.config" } });

            var connBuilder = new ServiceBusConnectionStringBuilder { ManagementPort = HttpPort, RuntimePort = TcpPort };
            connBuilder.Endpoints.Add(new UriBuilder { Scheme = "sb", Host = serverFQDN, Path = ServiceNamespace }.Uri);
            connBuilder.StsEndpoints.Add(new UriBuilder { Scheme = "https", Host = serverFQDN, Port = HttpPort, Path = ServiceNamespace }.Uri);
            _bus = Bus.CreateBus(cfg => cfg.WithConnectionString(connBuilder.ToString())).Result;
        }
Пример #44
0
        public void SetUp()
        {
            var serverFQDN = Environment.MachineName;
            const int HttpPort = 9355;
            const int TcpPort = 9354;
            const string ServiceNamespace = "ServiceBusDefaultNamespace";

            var connBuilder = new ServiceBusConnectionStringBuilder { ManagementPort = HttpPort, RuntimePort = TcpPort };
            connBuilder.OperationTimeout = TimeSpan.FromSeconds(10);
            connBuilder.Endpoints.Add(new UriBuilder { Scheme = "sb", Host = serverFQDN, Path = ServiceNamespace }.Uri);
            connBuilder.StsEndpoints.Add(new UriBuilder { Scheme = "https", Host = serverFQDN, Port = HttpPort, Path = ServiceNamespace }.Uri);
            _testBus = Bus.CreateBus(cfg => cfg.WithConnectionString(connBuilder.ToString()).WithLogger(new TestLogger())).Result;
            // _testBus = Bus.CreateBus(cfg => cfg.WithConnectionString(@"Endpoint=sb://funnelfire-test.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=ENUAYOiS9Lt3tDngyClvjzRyls5UkS8ie7aLAyjBV0s=;OperationTimeout=00:00:10").WithLogger(new TestLogger())).Result;
        }
Пример #45
0
        public  Task OpenEventHubAsync(string eventHubConnectionString, string hubName)
        {
            
            return Task.Factory.StartNew(async () => {
                if (string.IsNullOrWhiteSpace(eventHubConnectionString))
                    throw new ArgumentException("invalid event hub connection string");

                if (string.IsNullOrWhiteSpace(hubName))
                    throw new ArgumentException("invalid hubname");
                this.IsOpen = true;

                var builder = new ServiceBusConnectionStringBuilder(eventHubConnectionString) { TransportType = TransportType.Amqp };
                var s = builder.ToString();
                _factory = MessagingFactory.CreateFromConnectionString(builder.ToString());
       
                _client = _factory.CreateEventHubClient(hubName);
                
                var runtimeInfo = await _client.GetRuntimeInformationAsync();

                _partitions.Clear();
                foreach (var p in runtimeInfo.PartitionIds)
                {
                
                    var partition = await _client.GetPartitionRuntimeInformationAsync(p);
                    var count = partition.LastEnqueuedSequenceNumber - partition.BeginSequenceNumber;
                    if (count != 0)
                        count++;
                    this.MessageCount = count;
                    _partitions.Add(new Partition(p, _messages));
                    _foundPartitions.OnNext(partition);
                }

                _foundPartitions.OnCompleted();
                
            });

        }
 private static string GetAmqpConnectionString(string connectionString)
 {
     if (string.IsNullOrEmpty(connectionString))
     {
         throw new ApplicationException(ConnectionStringCannotBeNull);
     }
     var builder = new ServiceBusConnectionStringBuilder(connectionString) { TransportType = TransportType.Amqp };
     return builder.ToString();
 }
Пример #47
0
        public async Task EventHubConnectSend(int sendCount)
        {
            var cs = @"Endpoint=sb://yournamespace.servicebus.windows.net/;SharedAccessKeyName=YourPublisher;SharedAccessKey=12EHa5NNllnZBz6gksogvNfhketIokdTdjx4ZrTfu9U=";

            var builder = new ServiceBusConnectionStringBuilder(cs)
                        {
                            TransportType = TransportType.Amqp
                        };

            var client = EventHubClient.CreateFromConnectionString(builder.ToString(), "swiftsoftware-eh");

            int sentCount = sendCount;
            string infoText;

            for (int i = 0; i < sendCount; i++)
            {
                try
                {
                    if (MessageType.Text == "Sensor")
                    {
                        infoText = "Operating Within Expected Parameters";
                    }
                    else
                    {
                        infoText = "Temperature Sensor Requires Calibration";
                    }

                    Random rnd = new Random();
                    int intTemp = rnd.Next(15, 25);
                    int intHum = rnd.Next(50, 80);
                 
                    var e = new Event
                    {
                        MessageType = MessageType.Text,
                        Temp = intTemp,
                        Humidity = intHum,
                        Location = Location.Text,
                        Room = Room.Text,
                        Info = infoText
                    };

                    var serializedString = JsonConvert.SerializeObject(e);
                    var data = new EventData(Encoding.UTF8.GetBytes(serializedString))
                    {
                        PartitionKey = rnd.Next(6).ToString()
                    };

                    // Set user properties if needed
                    data.Properties.Add("Type", "Telemetry_" + DateTime.Now.ToLongTimeString());
                    
                    await client.SendAsync(data).ConfigureAwait(false);
                }
                catch (Exception exp)
                {
                    Console.WriteLine("Error on send: " + exp.Message);
                    sentCount--;
                }

                await Task.Delay(Convert.ToInt32(DelayTime.Text));
               
            }

            MessageResult.Text = " " + sentCount.ToString() + " " + MessageType.Text + " Messages were successfully sent to Event Hub";
            return;
        }
        /// <summary>
        /// Creates an EventHubReceiver from the given connection sting and partition key.
        /// The Reliable Dictionaries are used to create a receiver from wherever the service last left off,
        /// or from the current date/time if it's the first time the service is coming up.
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="servicePartitionKey"></param>
        /// <param name="epochDictionary"></param>
        /// <param name="offsetDictionary"></param>
        /// <returns></returns>
        private async Task<Tuple<EventHubReceiver, MessagingFactory>> ConnectToIoTHubAsync(
            string connectionString,
            long servicePartitionKey,
            IReliableDictionary<string, long> epochDictionary,
            IReliableDictionary<string, string> offsetDictionary)
        {

            // EventHubs doesn't support NetMessaging, so ensure the transport type is AMQP.
            ServiceBusConnectionStringBuilder connectionStringBuilder = new ServiceBusConnectionStringBuilder(connectionString);
            connectionStringBuilder.TransportType = TransportType.Amqp;

            ServiceEventSource.Current.ServiceMessage(
                      this.Context,
                      "RouterService connecting to IoT Hub at {0}",
                      String.Join(",", connectionStringBuilder.Endpoints.Select(x => x.ToString())));

            // A new MessagingFactory is created here so that each partition of this service will have its own MessagingFactory.
            // This gives each partition its own dedicated TCP connection to IoT Hub.
            MessagingFactory messagingFactory = MessagingFactory.CreateFromConnectionString(connectionStringBuilder.ToString());
            EventHubClient eventHubClient = messagingFactory.CreateEventHubClient("messages/events");
            EventHubRuntimeInformation eventHubRuntimeInfo = await eventHubClient.GetRuntimeInformationAsync();
            EventHubReceiver eventHubReceiver;

            // Get an IoT Hub partition ID that corresponds to this partition's low key.
            // This assumes that this service has a partition count 'n' that is equal to the IoT Hub partition count and a partition range of 0..n-1.
            // For example, given an IoT Hub with 32 partitions, this service should be created with:
            // partition count = 32
            // partition range = 0..31
            string eventHubPartitionId = eventHubRuntimeInfo.PartitionIds[servicePartitionKey];

            using (ITransaction tx = this.StateManager.CreateTransaction())
            {
                ConditionalValue<string> offsetResult = await offsetDictionary.TryGetValueAsync(tx, "offset", LockMode.Default);
                ConditionalValue<long> epochResult = await epochDictionary.TryGetValueAsync(tx, "epoch", LockMode.Update);

                long newEpoch = epochResult.HasValue
                    ? epochResult.Value + 1
                    : 0;

                if (offsetResult.HasValue)
                {
                    // continue where the service left off before the last failover or restart.
                    ServiceEventSource.Current.ServiceMessage(
                        this.Context,
                        "Creating EventHub listener on partition {0} with offset {1}",
                        eventHubPartitionId,
                        offsetResult.Value);

                    eventHubReceiver = await eventHubClient.GetDefaultConsumerGroup().CreateReceiverAsync(eventHubPartitionId, offsetResult.Value, newEpoch);
                }
                else
                {
                    // first time this service is running so there is no offset value yet.
                    // start with the current time.
                    ServiceEventSource.Current.ServiceMessage(
                        this.Context,
                        "Creating EventHub listener on partition {0} with offset {1}",
                        eventHubPartitionId,
                        DateTime.UtcNow);

                    eventHubReceiver =
                        await
                            eventHubClient.GetDefaultConsumerGroup()
                                .CreateReceiverAsync(eventHubPartitionId, DateTime.UtcNow, newEpoch);
                }

                // epoch is recorded each time the service fails over or restarts.
                await epochDictionary.SetAsync(tx, "epoch", newEpoch);
                await tx.CommitAsync();
            }

            return new Tuple<EventHubReceiver, MessagingFactory>(eventHubReceiver, messagingFactory);
        }
Пример #49
0
 static string GetAmqpConnectionString()
 {
     ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connectionString);
     builder.TransportType = TransportType.Amqp;
     return builder.ToString();
 }
Пример #50
0
        static void Main(string[] args)
        {
            // Get the executing server's FQDN...this will be used for the Host paroperty of the sb endpoint.
            // I initially neglected to populate this value which resulted in errors resolving the namespace at
            // runtime when an attempt was made to connect to the sb instance.
            ServerFQDN = System.Net.Dns.GetHostEntry(string.Empty).HostName;

            // Build the Service Bus connection string
            ServiceBusConnectionStringBuilder connBuilder = new ServiceBusConnectionStringBuilder();
            connBuilder.ManagementPort = HttpPort;
            connBuilder.RuntimePort = TcpPort;
            UriBuilder runtimeAddress = new UriBuilder()
            {
                Scheme = "sb", // this scheme is for TCP
                Host = ServerFQDN,
                Path = ServiceNamespace
            };
            connBuilder.Endpoints.Add(runtimeAddress.Uri);

            UriBuilder mgmtAddress = new UriBuilder()
            {
                Scheme = "https",
                Host = ServerFQDN,
                Port = HttpPort,
                Path = ServiceNamespace
            };
            connBuilder.StsEndpoints.Add(mgmtAddress.Uri);

            //TODO: Identify specifics of purpose of
            // NamespaceManager instance...
            NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(connBuilder.ToString());
            MessagingFactory messageFactory = MessagingFactory.CreateFromConnectionString(connBuilder.ToString());

            // Now create queue on the fly
            if (namespaceManager == null)
            {
                Console.WriteLine("\nUnexpected Error: NamespaceManager is NULL");
                return;
            }
            if (namespaceManager.QueueExists(QueueName))
            {
                namespaceManager.DeleteQueue(QueueName);
            }
            namespaceManager.CreateQueue(QueueName);

            // Create a queue client to send and receive messages to and from the queue
            QueueClient queueClient = messageFactory.CreateQueueClient(QueueName);

            // Create a simple brokered message and send it to the queue.
            BrokeredMessage messageToSend = new BrokeredMessage("Hello World! I used GitHub with this solution!");
            queueClient.Send(messageToSend);

            // Receive the message back from the queue.
            BrokeredMessage receivedMessage = queueClient.Receive(TimeSpan.FromSeconds(5));
            if (receivedMessage != null)
            {
                Console.WriteLine(string.Format("Message rceived: Body = {0}",
                  receivedMessage.GetBody<string>()));
                receivedMessage.Complete();
            }

            // Clean up.
            if (messageFactory != null)
            {
                messageFactory.Close();
            }
            Console.ReadLine();
        }
        /// <summary>
        /// Returns Service Bus connection string to use.
        /// </summary>
        public string BuildConnectionString()
        {
            if (OperationTimeout != null)
            {
                var connectionStringBuilder = new ServiceBusConnectionStringBuilder(ConnectionString);
                connectionStringBuilder.OperationTimeout = OperationTimeout.Value;
                return connectionStringBuilder.ToString();
            }

            return ConnectionString;
        }
Пример #52
0
        string GetEventHubConnectionString()
        {
            var connectionString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"];
            if (string.IsNullOrEmpty(connectionString))
            {
                Console.WriteLine("Did not find Service Bus connections string in appsettings (app.config)");
                return string.Empty;
            }
            try
            {
                var builder = new ServiceBusConnectionStringBuilder(connectionString);
                builder.TransportType = Microsoft.ServiceBus.Messaging.TransportType.Amqp;
                return builder.ToString();
            }
            catch (Exception exception)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(exception.Message);
                Console.ResetColor();
            }

            return null;
        }
        public void InitializeEventHub()
        {
            Context.Logger.Info("Current AppConfig File: " + ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None));
            Context.Logger.Info("Current AppSettings: " + String.Join(Environment.NewLine, ConfigurationManager.AppSettings.AllKeys));

            this.EventHubNamespace = ConfigurationManager.AppSettings["EventHubNamespace"];
            if (String.IsNullOrWhiteSpace(this.EventHubNamespace))
            {
                throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubNamespace");
            }

            this.EventHubEntityPath = ConfigurationManager.AppSettings["EventHubEntityPath"];
            if (String.IsNullOrWhiteSpace(this.EventHubEntityPath))
            {
                throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubEntityPath");
            }

            this.EventHubSharedAccessKeyName = ConfigurationManager.AppSettings["EventHubSharedAccessKeyName"];
            if (String.IsNullOrWhiteSpace(this.EventHubSharedAccessKeyName))
            {
                throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubSharedAccessKeyName");
            }

            this.EventHubSharedAccessKey = ConfigurationManager.AppSettings["EventHubSharedAccessKey"];
            if (String.IsNullOrWhiteSpace(this.EventHubSharedAccessKey))
            {
                throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubSharedAccessKey");
            }

            this.EventHubPartitions = ConfigurationManager.AppSettings["EventHubPartitions"];
            if (String.IsNullOrWhiteSpace(this.EventHubPartitions))
            {
                throw new ArgumentException("A required AppSetting cannot be null or empty", "EventHubPartitions");
            }

            var builder = new ServiceBusConnectionStringBuilder();
            builder.Endpoints.Add(new Uri("sb://" + this.EventHubNamespace + "." + EventHubFqnAddress));
            builder.EntityPath = this.EventHubEntityPath;
            builder.SharedAccessKeyName = this.EventHubSharedAccessKeyName;
            builder.SharedAccessKey = this.EventHubSharedAccessKey;
            builder.TransportType = TransportType.Amqp;

            var partitionCount = int.Parse(this.EventHubPartitions);

            TopologyContext topologyContext = Context.TopologyContext;
            Context.Logger.Info(this.GetType().Name + " TopologyContext info:");
            Context.Logger.Info("TaskId: {0}", topologyContext.GetThisTaskId());
            var taskIndex = topologyContext.GetThisTaskIndex();
            Context.Logger.Info("TaskIndex: {0}", taskIndex);
            string componentId = topologyContext.GetThisComponentId();
            Context.Logger.Info("ComponentId: {0}", componentId);
            List<int> componentTasks = topologyContext.GetComponentTasks(componentId);
            Context.Logger.Info("ComponentTasks: {0}", componentTasks.Count);

            if (partitionCount != componentTasks.Count)
            {
                throw new Exception(
                    String.Format("Component task count does not match partition count. Component: {0}, Tasks: {1}, Partition: {2}",
                    componentId, componentTasks.Count, partitionCount));
            }

            partitionId = taskIndex.ToString();

            Context.Logger.Info(this.GetType().Name + " ConnectionString = {0}, ParitionId = {1}",
                builder.ToString(), partitionId);

            eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString());
            eventHubSender = eventHubClient.CreatePartitionedSender(partitionId);
        }
        public void InitializeEventHub()
        {
            Context.Logger.Info("Initializing EventHubClient and EventHubSender...");
            var builder = new ServiceBusConnectionStringBuilder();
            builder.Endpoints.Add(new Uri("sb://" + this.appConfig.EventHubNamespace + "." + this.appConfig.EventHubFqnAddress));
            builder.EntityPath = this.appConfig.EventHubEntityPath;
            builder.SharedAccessKeyName = this.appConfig.EventHubSharedAccessKeyName;
            builder.SharedAccessKey = this.appConfig.EventHubSharedAccessKey;
            builder.TransportType = TransportType.Amqp;

            Context.Logger.Info("EventHubWriter: ConnectionString = {0} ParitionId = {1}",
                builder.ToString(), Context.TopologyContext.GetThisTaskIndex());

            eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString());
            //TODO: Implement a distribution strategy of partitions in case number of bolt tasks is less than partitions in EventHub
            eventHubSender = eventHubClient.CreatePartitionedSender(Context.TopologyContext.GetThisTaskIndex().ToString());
        }
        public async void DoesntIgnoreDefinedTimeoutWhenReceiving(AzureServiceBusMode mode, int operationTimeoutInSeconds)
        {
            var operationTimeout = TimeSpan.FromSeconds(operationTimeoutInSeconds);

            var connString = StandardAzureServiceBusTransportFactory.ConnectionString;
            var builder = new ServiceBusConnectionStringBuilder(connString)
            {
                OperationTimeout = operationTimeout
            };
            var newConnString = builder.ToString();

            var consoleLoggerFactory = new ConsoleLoggerFactory(false);
            var transport = new AzureServiceBusTransport(newConnString, QueueName, consoleLoggerFactory, new TplAsyncTaskFactory(consoleLoggerFactory), new BusLifetimeEvents());

            Using(transport);

            transport.PurgeInputQueue();
            //Create the queue for the receiver since it cannot create it self beacuse of lacking rights on the namespace
            transport.CreateQueue(QueueName);

            var senderActivator = new BuiltinHandlerActivator();

            var senderBus = Configure.With(senderActivator)
                .Transport(t => t.UseAzureServiceBus(newConnString, "sender", mode))
                .Start();

            Using(senderBus);

            // queue 3 messages
            await senderBus.Advanced.Routing.Send(QueueName, "message to receiver");
            await senderBus.Advanced.Routing.Send(QueueName, "message to receiver2");
            await senderBus.Advanced.Routing.Send(QueueName, "message to receiver3");

            await Task.Delay(TimeSpan.FromSeconds(2)); // wait a bit to make sure the messages are queued.

            // receive 1
            using (var transactionContext = new DefaultTransactionContext())
            {
                var sw = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(transactionContext);
                sw.Stop();
                await transactionContext.Complete();

                msg.Should().NotBeNull();
                sw.Elapsed.Should().BeLessThan(TimeSpan.FromMilliseconds(1500));
            }

            // receive 2
            using (var transactionContext = new DefaultTransactionContext())
            {
                var sw = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(transactionContext);
                sw.Stop();
                await transactionContext.Complete();

                msg.Should().NotBeNull();
                sw.Elapsed.Should().BeLessThan(TimeSpan.FromMilliseconds(1500));
            }

            // receive 3
            using (var transactionContext = new DefaultTransactionContext())
            {
                var sw = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(transactionContext);
                sw.Stop();
                await transactionContext.Complete();

                msg.Should().NotBeNull();
                sw.Elapsed.Should().BeLessThan(TimeSpan.FromMilliseconds(1500));
            }

            // receive 4 - NOTHING
            using (var transactionContext = new DefaultTransactionContext())
            {
                var sw = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(transactionContext);
                sw.Stop();
                await transactionContext.Complete();

                msg.Should().BeNull();
                sw.Elapsed.Should().BeCloseTo(operationTimeout, 2000);
            }

            // put 1 more message 
            await senderBus.Advanced.Routing.Send(QueueName, "message to receiver5");

            await Task.Delay(TimeSpan.FromSeconds(2)); // wait a bit to make sure the messages are queued.

            // receive 5
            using (var transactionContext = new DefaultTransactionContext())
            {
                var sw = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(transactionContext);
                sw.Stop();
                await transactionContext.Complete();

                msg.Should().NotBeNull();
                sw.Elapsed.Should().BeLessThan(TimeSpan.FromMilliseconds(1500));
            }

            // receive 6 - NOTHING
            using (var transactionContext = new DefaultTransactionContext())
            {
                var sw = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(transactionContext);
                sw.Stop();
                await transactionContext.Complete();

                msg.Should().BeNull();
                sw.Elapsed.Should().BeCloseTo(operationTimeout, 2000);
            }
        }
        private async Task StartInternalAsync(OnlineTrainerSettingsInternal settings, OnlineTrainerState state = null, byte[] model = null)
        {
            this.LastStartDateTimeUtc = DateTime.UtcNow;
            this.perfCounters = new PerformanceCounters(settings.Metadata.ApplicationID);

            // setup trainer
            this.trainer = new Learner(settings, this.DelayedExampleCallback, this.perfCounters);

            if (settings.ForceFreshStart || model != null)
                this.trainer.FreshStart(state, model);
            else
                await this.trainer.FindAndResumeFromState();

            // setup factory
            this.trainProcessorFactory = new TrainEventProcessorFactory(settings, this.trainer, this.perfCounters);

            // setup host
            var serviceBusConnectionStringBuilder = new ServiceBusConnectionStringBuilder(settings.JoinedEventHubConnectionString);
            var joinedEventhubName = serviceBusConnectionStringBuilder.EntityPath;
            serviceBusConnectionStringBuilder.EntityPath = string.Empty;

            this.eventProcessorHost = new EventProcessorHost(settings.Metadata.ApplicationID, joinedEventhubName,
                EventHubConsumerGroup.DefaultGroupName, serviceBusConnectionStringBuilder.ToString(), settings.StorageConnectionString);

            await this.eventProcessorHost.RegisterEventProcessorFactoryAsync(
                this.trainProcessorFactory,
                new EventProcessorOptions { InitialOffsetProvider = this.InitialOffsetProvider });

            // don't perform too often
            this.perfUpdater = new SafeTimer(
                TimeSpan.FromMilliseconds(500),
                this.UpdatePerformanceCounters);

            this.telemetry.TrackTrace(
                "OnlineTrainer started",
                SeverityLevel.Information,
                new Dictionary<string, string>
                {
                { "CheckpointPolicy", settings.CheckpointPolicy.ToString() },
                { "VowpalWabbit", settings.Metadata.TrainArguments },
                { "ExampleTracing", settings.EnableExampleTracing.ToString() }
                });
        }