/// <summary>
        /// Create a new host to process events from an Event Hub.
        ///
        /// <para>This overload of the constructor allows maximum flexibility.
        /// This one allows the caller to specify the name of the processor host as well.
        /// The overload also allows the caller to provide their own lease and checkpoint managers to replace the built-in
        /// ones based on Azure Storage.</para>
        /// </summary>
        /// <param name="hostName">Name of the processor host. MUST BE UNIQUE. Strongly recommend including a Guid to ensure uniqueness.</param>
        /// <param name="eventHubPath">The name of the EventHub.</param>
        /// <param name="consumerGroupName">The name of the consumer group within the Event Hub.</param>
        /// <param name="eventHubConnectionString">Connection string for the Event Hub to receive from.</param>
        /// <param name="checkpointManager">Object implementing ICheckpointManager which handles partition checkpointing.</param>
        /// <param name="leaseManager">Object implementing ILeaseManager which handles leases for partitions.</param>
        public EventProcessorHost(
            string hostName,
            string eventHubPath,
            string consumerGroupName,
            string eventHubConnectionString,
            ICheckpointManager checkpointManager,
            ILeaseManager leaseManager)
        {
            if (string.IsNullOrEmpty(consumerGroupName))
            {
                throw new ArgumentNullException(nameof(consumerGroupName));
            }
            else if (checkpointManager == null || leaseManager == null)
            {
                throw new ArgumentNullException(checkpointManager == null ? nameof(checkpointManager) : nameof(leaseManager));
            }

            var csb = new EventHubsConnectionStringBuilder(eventHubConnectionString);

            if (string.IsNullOrEmpty(eventHubPath))
            {
                // Entity path is expected in the connection string if not provided with eventHubPath parameter.
                if (string.IsNullOrEmpty(csb.EntityPath))
                {
                    throw new ArgumentException(nameof(eventHubConnectionString),
                                                "Provide EventHub entity path either in eventHubPath parameter or in eventHubConnectionString.");
                }
            }
            else
            {
                // Entity path should not conflict with connection string.
                if (!string.IsNullOrEmpty(csb.EntityPath) &&
                    string.Compare(csb.EntityPath, eventHubPath, StringComparison.OrdinalIgnoreCase) != 0)
                {
                    throw new ArgumentException(nameof(eventHubConnectionString),
                                                "Provided EventHub path in eventHubPath parameter conflicts with the path in provided EventHubs connection string.");
                }

                csb.EntityPath = eventHubPath;
            }

            this.HostName                 = hostName;
            this.EventHubPath             = csb.EntityPath;
            this.ConsumerGroupName        = consumerGroupName;
            this.EventHubConnectionString = csb.ToString();
            this.CheckpointManager        = checkpointManager;
            this.LeaseManager             = leaseManager;
            this.Id = $"EventProcessorHost({hostName.Substring(0, Math.Min(hostName.Length, 20))}...)";
            this.PartitionManager = new PartitionManager(this);
            ProcessorEventSource.Log.EventProcessorHostCreated(this.Id, this.EventHubPath);
        }
Esempio n. 2
0
        public async Task SendDataAsync(string data, string eventHubName, string eventHubConnectionString)
        {
            var eventData = new EventData(Encoding.UTF8.GetBytes(data));

            var connectionStringBuilder = new EventHubsConnectionStringBuilder(eventHubConnectionString)
            {
                EntityPath = eventHubName
            };

            var eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
            await eventHubClient.SendAsync(eventData);

            _logger.LogInformation("Data sent to event hub");
        }
        /// <summary>
        /// Constructor. Arguments break down into three groups: (1) Service Fabric objects so this library can access
        /// Service Fabric facilities, (2) Event Hub-related arguments which indicate what event hub to receive from and
        /// how to process the events, and (3) advanced, which right now consists only of the ability to replace the default
        /// reliable dictionary-based checkpoint manager with a user-provided implementation.
        /// </summary>
        /// <param name="serviceFabricServiceName">Service Fabric Uri found in StatefulServiceContext</param>
        /// <param name="serviceFabricPartitionId">Service Fabric partition id found in StatefulServiceContext</param>
        /// <param name="stateManager">Service Fabric-provided state manager, provides access to reliable dictionaries</param>
        /// <param name="partition">Service Fabric-provided partition information</param>
        /// <param name="userEventProcessor">User's event processor implementation</param>
        /// <param name="eventHubConnectionString">Connection string for user's event hub</param>
        /// <param name="eventHubConsumerGroup">Name of event hub consumer group to receive from</param>
        /// <param name="options">Optional: Options structure for ServiceFabricProcessor library</param>
        /// <param name="checkpointManager">Very advanced/optional: user-provided checkpoint manager implementation</param>
        public ServiceFabricProcessor(Uri serviceFabricServiceName, Guid serviceFabricPartitionId, IReliableStateManager stateManager, IStatefulServicePartition partition, IEventProcessor userEventProcessor,
                                      string eventHubConnectionString, string eventHubConsumerGroup,
                                      EventProcessorOptions options = null, ICheckpointMananger checkpointManager = null)
        {
            if (serviceFabricServiceName == null)
            {
                throw new ArgumentNullException("serviceFabricServiceName is null");
            }
            if (serviceFabricPartitionId == null)
            {
                throw new ArgumentNullException("serviceFabricPartitionId is null");
            }
            if (stateManager == null)
            {
                throw new ArgumentNullException("stateManager is null");
            }
            if (partition == null)
            {
                throw new ArgumentNullException("partition is null");
            }
            if (userEventProcessor == null)
            {
                throw new ArgumentNullException("userEventProcessor is null");
            }
            if (string.IsNullOrEmpty(eventHubConnectionString))
            {
                throw new ArgumentException("eventHubConnectionString is null or empty");
            }
            if (string.IsNullOrEmpty(eventHubConsumerGroup))
            {
                throw new ArgumentException("eventHubConsumerGroup is null or empty");
            }

            this.serviceFabricServiceName = serviceFabricServiceName;
            this.serviceFabricPartitionId = serviceFabricPartitionId;
            this.serviceStateManager      = stateManager;
            this.servicePartition         = partition;

            this.userEventProcessor = userEventProcessor;

            this.ehConnectionString = new EventHubsConnectionStringBuilder(eventHubConnectionString);
            this.consumerGroupName  = eventHubConsumerGroup;

            this.options           = options ?? new EventProcessorOptions();
            this.checkpointManager = checkpointManager ?? new ReliableDictionaryCheckpointMananger(this.serviceStateManager);

            this.EventHubClientFactory = new EventHubWrappers.EventHubClientFactory();
            this.TestMode = false;
            this.MockMode = null;
        }
Esempio n. 4
0
        public EventHubSenderWrapper()
        {
            EventHubConfig.EventHubName          = EventHubName;
            EventHubConfig.WriteConnectionString = EventHubConnectionString;

            var eventHubConfigProvider = EventHubConfig.EventHubConfigProvider;

            var connectionStringBuilder = new EventHubsConnectionStringBuilder(EventHubConfig.WriteConnectionString)
            {
                EntityPath = EventHubConfig.EventHubName
            };

            _sender = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
        }
Esempio n. 5
0
        protected async Task VerifyDataOnIoTHub(string moduleId)
        {
            var builder = new EventHubsConnectionStringBuilder(this.eventhubCompatibleEndpointWithEntityPath);

            builder.TransportType = this.eventHubClientTransportType;

            Console.WriteLine($"Receiving events from device '{this.context.Device.Id}' on Event Hub '{builder.EntityPath}'");

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

            PartitionReceiver eventHubReceiver = eventHubClient.CreateReceiver(
                "$Default",
                EventHubPartitionKeyResolver.ResolveToPartition(
                    this.context.Device.Id,
                    (await eventHubClient.GetRuntimeInformationAsync()).PartitionCount),
                EventPosition.FromEnd());

            var result = new TaskCompletionSource <bool>();

            using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(10)))
            {
                using (cts.Token.Register(() => result.TrySetCanceled()))
                {
                    eventHubReceiver.SetReceiveHandler(
                        new PartitionReceiveHandler(
                            eventData =>
                    {
                        eventData.SystemProperties.TryGetValue("iothub-connection-device-id", out object devId);
                        eventData.SystemProperties.TryGetValue("iothub-connection-module-id", out object modId);

                        if (devId != null && devId.ToString().Equals(this.context.Device.Id) &&
                            modId != null && modId.ToString().Equals(moduleId))
                        {
                            result.TrySetResult(true);
                            return(true);
                        }

                        return(false);
                    }));

                    await result.Task;
                }
            }

            Console.WriteLine("VerifyDataOnIoTHub completed.");
            await eventHubReceiver.CloseAsync();

            await eventHubClient.CloseAsync();
        }
        public void Initialize(string connectionString, string eventHubName)
        {
            this.connectionString = connectionString;
            this.eventHubName     = eventHubName;

            var connectionStringBuilder = new EventHubsConnectionStringBuilder(connectionString)
            {
                EntityPath = eventHubName
            };

            var fullConnectionString = connectionStringBuilder.ToString();

            client = EventHubClient.CreateFromConnectionString(fullConnectionString);
        }
Esempio n. 7
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log)
        {
            var STORAGE_ACCT     = System.Environment.GetEnvironmentVariable("STORAGE_ACCT", EnvironmentVariableTarget.Process);
            var STORAGE_ACCT_KEY = System.Environment.GetEnvironmentVariable("STORAGE_ACCT_KEY", EnvironmentVariableTarget.Process);
            var EVENT_HUB_CONN   = System.Environment.GetEnvironmentVariable("EVENT_HUB_CONN", EnvironmentVariableTarget.Process);
            var EhEntityPath     = "deployqueue";

            // Connect to EH
            var connectionStringBuilder = new EventHubsConnectionStringBuilder(EVENT_HUB_CONN)
            {
                EntityPath = EhEntityPath
            };
            var eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());

            CloudStorageAccount storageAccount = new CloudStorageAccount(
                new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                    STORAGE_ACCT,
                    STORAGE_ACCT_KEY), true);

            // Create a blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Get a reference to a container named "mycontainer."
            CloudBlobContainer container = blobClient.GetContainerReference("testblah");

            // Get blob
            CloudBlockBlob blockBlob = container.GetBlockBlobReference("azuredeploy.json");

            // Save blog to text var
            var templateContent = await blockBlob.DownloadTextAsync();

            //log.Info(templateContent);
            JObject template = JObject.Parse(templateContent);

            JObject templateParams = (JObject)template["parameters"];

            //log.Info(templateParams["adminUsername"]["defaultValue"].ToString());
            templateParams["adminUsername"]["defaultValue"]  = "esell";
            templateParams["adminPassword"]["defaultValue"]  = "B3stP4ss0nTheN3t!";
            templateParams["dnsLabelPrefix"]["defaultValue"] = "esellautogen";

            try
            {
                await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(template.ToString())));
            }
            catch (Exception ex) {
                log.Error(ex.Message);
            }
            return(req.CreateResponse(HttpStatusCode.OK, template.ToString()));
        }
 public static async Task ClassInitialize(TestContext context)
 {
     if (context.Properties.TryGetValue("EventHubNamespaceConnectionString", out object connectionStringValue) &&
         connectionStringValue is string connectionString &&
         context.Properties.TryGetValue("EventHubName", out object eventHubNameValue) &&
         eventHubNameValue is string eventHubName)
     {
         var connectionStringBuilder = new EventHubsConnectionStringBuilder(connectionString)
         {
             EntityPath = eventHubName
         };
         EventHub            = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
         EventHubInformation = await EventHub.GetRuntimeInformationAsync();
     }
Esempio n. 9
0
        public async Task <string> GetConnectionStringAsync(string eventHub)
        {
            await EnsureAuthenticatedAsync();

            AzureOperationResponse <AccessKeys> responseKeys = await _client.EventHubs.ListKeysWithHttpMessagesAsync(_azureResourceIdentifier.ResourceGroupName,
                                                                                                                     _azureResourceIdentifier.Name, eventHub, "sender");

            AccessKeys keys = responseKeys.Body;

            var eventHubNamespaceUri    = new Uri($"sb://{_azureResourceIdentifier.Name}.servicebus.windows.net");
            var connectionStringBuilder = new EventHubsConnectionStringBuilder(eventHubNamespaceUri, eventHub, keys.KeyName, keys.PrimaryKey);

            return(connectionStringBuilder.ToString());
        }
Esempio n. 10
0
        private EventHubClient CreateClient()
        {
            // Creates an EventHubsConnectionStringBuilder object from the connection string, and sets the EntityPath.
            // Typically, the connection string should have the entity path in it, but this simple scenario
            // uses the connection string from the namespace.
            var connectionStringBuilder = new EventHubsConnectionStringBuilder(config.EventHubConnectionString)
            {
                EntityPath = EventHubName
            };

            var eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());

            return(eventHubClient);
        }
        public async Task GetEventHubPartitionRuntimeInformation()
        {
            var cbs        = new EventHubsConnectionStringBuilder(TestUtility.EventHubsConnectionString);
            var ehClient   = EventHubClient.CreateFromConnectionString(TestUtility.EventHubsConnectionString);
            var partitions = await this.GetPartitionsAsync(ehClient);

            try
            {
                TestUtility.Log("Getting EventHubPartitionRuntimeInformation on each partition in parallel");
                var tasks = partitions.Select(async(pid) =>
                {
                    // Send some messages so we can have meaningful data returned from service call.
                    PartitionSender partitionSender = ehClient.CreatePartitionSender(pid);

                    try
                    {
                        TestUtility.Log($"Sending single event to partition {pid}");
                        var eDataToSend = new EventData(new byte[1]);
                        await partitionSender.SendAsync(eDataToSend);

                        TestUtility.Log($"Getting partition runtime information on partition {pid}");
                        var partition = await ehClient.GetPartitionRuntimeInformationAsync(pid);
                        TestUtility.Log($"Path:{partition.Path} PartitionId:{partition.PartitionId} BeginSequenceNumber:{partition.BeginSequenceNumber} LastEnqueuedOffset:{partition.LastEnqueuedOffset} LastEnqueuedTimeUtc:{partition.LastEnqueuedTimeUtc} LastEnqueuedSequenceNumber:{partition.LastEnqueuedSequenceNumber}");

                        // Validations.
                        Assert.True(partition.Path == cbs.EntityPath, $"Returned path {partition.Path} is different than {cbs.EntityPath}");
                        Assert.True(partition.PartitionId == pid, $"Returned partition id {partition.PartitionId} is different than {pid}");
                        Assert.True(partition.LastEnqueuedOffset != null, "Returned LastEnqueuedOffset is null");
                        Assert.True(partition.LastEnqueuedTimeUtc != null, "Returned LastEnqueuedTimeUtc is null");

                        // Validate returned data regarding recently sent event.
                        // Account 60 seconds of max clock skew.
                        Assert.True(partition.LastEnqueuedOffset != "-1", $"Returned LastEnqueuedOffset is {partition.LastEnqueuedOffset}");
                        Assert.True(partition.BeginSequenceNumber >= 0, $"Returned BeginSequenceNumber is {partition.BeginSequenceNumber}");
                        Assert.True(partition.LastEnqueuedSequenceNumber >= 0, $"Returned LastEnqueuedSequenceNumber is {partition.LastEnqueuedSequenceNumber}");
                        Assert.True(partition.LastEnqueuedTimeUtc >= DateTime.UtcNow.AddSeconds(-60), $"Returned LastEnqueuedTimeUtc is {partition.LastEnqueuedTimeUtc}");
                    }
                    finally
                    {
                        await partitionSender.CloseAsync();
                    }
                });

                await Task.WhenAll(tasks);
            }
            finally
            {
                await ehClient.CloseAsync();
            }
        }
Esempio n. 12
0
        private EventHubClient InitializeEventHub()
        {
            logger.LogInformation("Creating EventHub");

            var ehConnectionString = eventHubSettings.EventHubConnectionString;
            var entityPath         = eventHubSettings.EventHubPath;

            var connectionStringBuilder = new EventHubsConnectionStringBuilder(ehConnectionString)
            {
                EntityPath = entityPath
            };

            return(EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString()));
        }
Esempio n. 13
0
        /// <summary>
        /// This registers <see cref="IEventProcessorFactory"/> implementation with the host which is used to create an instance of
        /// <see cref="IEventProcessor"/> when it takes ownership of a partition.  This also starts the host and causes it to start participating
        /// in the partition distribution process.
        /// </summary>
        /// <param name="factory">Instance of <see cref="IEventProcessorFactory"/> implementation.</param>
        /// <param name="processorOptions"><see cref="EventProcessorOptions"/> to control various aspects of message pump created when ownership
        /// is acquired for a particular partition of EventHub.</param>
        /// <returns>A task to indicate EventProcessorHost instance is started.</returns>
        public async Task RegisterEventProcessorFactoryAsync(IEventProcessorFactory factory, EventProcessorOptions processorOptions)
        {
            if (factory == null || processorOptions == null)
            {
                throw new ArgumentNullException(factory == null ? nameof(factory) : nameof(processorOptions));
            }

            // Initialize partition manager options with default values if not already set by the client.
            if (this.PartitionManagerOptions == null)
            {
                // Assign partition manager with default options.
                this.PartitionManagerOptions = new PartitionManagerOptions();
            }

            ProcessorEventSource.Log.EventProcessorHostOpenStart(this.HostName, factory.GetType().ToString());

            try
            {
                // Override operation timeout by receive timeout?
                if (processorOptions.ReceiveTimeout > TimeSpan.MinValue)
                {
                    this.OperationTimeout = processorOptions.ReceiveTimeout;

                    if (this.eventHubConnectionString != null)
                    {
                        var cbs = new EventHubsConnectionStringBuilder(this.eventHubConnectionString)
                        {
                            OperationTimeout = processorOptions.ReceiveTimeout
                        };
                        this.eventHubConnectionString = cbs.ToString();
                    }
                }

                // Initialize lease manager if this is an AzureStorageCheckpointLeaseManager
                (this.LeaseManager as AzureStorageCheckpointLeaseManager)?.Initialize(this);

                this.ProcessorFactory      = factory;
                this.EventProcessorOptions = processorOptions;
                await this.PartitionManager.StartAsync().ConfigureAwait(false);
            }
            catch (Exception e)
            {
                ProcessorEventSource.Log.EventProcessorHostOpenError(this.HostName, e.ToString());
                throw;
            }
            finally
            {
                ProcessorEventSource.Log.EventProcessorHostOpenStop(this.HostName);
            }
        }
Esempio n. 14
0
        private static EventHubClient CreateClient(string eventHubName, string connection)
        {
            EnsureArg.IsNotNullOrWhiteSpace(eventHubName, nameof(eventHubName));
            EnsureArg.IsNotNullOrWhiteSpace(connection, nameof(connection));

            var sb = new EventHubsConnectionStringBuilder(connection);

            if (string.IsNullOrWhiteSpace(sb.EntityPath))
            {
                sb.EntityPath = eventHubName;
            }

            return(EventHubClient.CreateFromConnectionString(sb.ToString()));
        }
Esempio n. 15
0
        private static async Task MainEventHubAsync(string[] args)
        {
            WriteLine("Enter your Event Hub connection string:");
            var EventHubConnectionString = ReadLine();

            while (EventHubConnectionString.Length == 0)
            {
                WriteLine("Try again, this value must have a length > 0");
                WriteLine("Enter your Event Hub connection string:");
                EventHubConnectionString = ReadLine();
            }
            WriteLine("Enter your Event Hub name:");
            var EventHubName = ReadLine();

            while (EventHubName.Length == 0)
            {
                WriteLine("Try again, this value must have a length > 0");
                WriteLine("Enter your Event Hub name:");
                EventHubName = ReadLine();
            }

            WriteLine("Enter your path where the Video Event files are:");
            var pathofVidEVTfiles = ReadLine();

            while (pathofVidEVTfiles.Length == 0)
            {
                WriteLine("Try again, this value must have a length > 0");
                WriteLine("Enter your path where the Video Event files are:");
                pathofVidEVTfiles = ReadLine();
            }

            WriteLine("Enter number of events to add: ");
            int EventHubEventsToSend = 0;

            while (!int.TryParse(ReadLine(), out EventHubEventsToSend))
            {
                WriteLine("Try again, this value must be numeric.");
            }

            var connectionStringBuilder = new EventHubsConnectionStringBuilder(EventHubConnectionString)
            {
                EntityPath = EventHubName
            };

            eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
            await SendMessagesToEventHub(EventHubEventsToSend, pathofVidEVTfiles);

            await eventHubClient.CloseAsync();
        }
Esempio n. 16
0
        protected async Task VerifyDataOnIoTHubAsync()
        {
            var builder = new EventHubsConnectionStringBuilder(this.eventhubCompatibleEndpointWithEntityPath)
            {
                TransportType = this.eventHubClientTransportType
            };

            Console.WriteLine($"Receiving events from device '{this.context.Device.Id}' on Event Hub '{builder.EntityPath}'");

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

            PartitionReceiver eventHubReceiver = eventHubClient.CreateReceiver(
                "$Default",
                EventHubPartitionKeyResolver.ResolveToPartition(
                    this.context.Device.Id,
                    (await eventHubClient.GetRuntimeInformationAsync()).PartitionCount),
                EventPosition.FromEnqueuedTime(DateTime.Now.AddMinutes(-5)));

            var result = new TaskCompletionSource <bool>();

            using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3)))
            {
                using (cts.Token.Register(() => result.TrySetCanceled()))
                {
                    eventHubReceiver.SetReceiveHandler(
                        new PartitionReceiveHandler(
                            eventData =>
                    {
                        eventData.SystemProperties.TryGetValue("iothub-connection-device-id", out var devId);

                        if (devId != null && devId.ToString().Equals(this.context.Device.Id, StringComparison.Ordinal) &&
                            Encoding.UTF8.GetString(eventData.Body).Contains(this.context.MessageGuid, StringComparison.Ordinal))
                        {
                            result.TrySetResult(true);
                            return(true);
                        }

                        return(false);
                    }));

                    await result.Task;
                }
            }

            await eventHubReceiver.CloseAsync();

            await eventHubClient.CloseAsync();
        }
Esempio n. 17
0
        /// <summary>
        /// Create a new host to process events from an Event Hub.
        ///
        /// <para>This overload of the constructor allows maximum flexibility.
        /// This one allows the caller to specify the name of the processor host as well.
        /// The overload also allows the caller to provide their own lease and checkpoint managers to replace the built-in
        /// ones based on Azure Storage.</para>
        /// </summary>
        /// <param name="hostName">Name of the processor host. MUST BE UNIQUE. Strongly recommend including a Guid to ensure uniqueness.</param>
        /// <param name="eventHubPath">The name of the EventHub.</param>
        /// <param name="consumerGroupName">The name of the consumer group within the Event Hub.</param>
        /// <param name="eventHubConnectionString">Connection string for the Event Hub to receive from.</param>
        /// <param name="checkpointManager">Object implementing ICheckpointManager which handles partition checkpointing.</param>
        /// <param name="leaseManager">Object implementing ILeaseManager which handles leases for partitions.</param>
        public EventProcessorHost(
            string hostName,
            string eventHubPath,
            string consumerGroupName,
            string eventHubConnectionString,
            ICheckpointManager checkpointManager,
            ILeaseManager leaseManager)
        {
            Guard.ArgumentNotNullOrWhiteSpace(nameof(hostName), hostName);
            Guard.ArgumentNotNullOrWhiteSpace(nameof(consumerGroupName), consumerGroupName);
            Guard.ArgumentNotNull(nameof(checkpointManager), checkpointManager);
            Guard.ArgumentNotNull(nameof(leaseManager), leaseManager);

            var csb = new EventHubsConnectionStringBuilder(eventHubConnectionString);

            if (string.IsNullOrEmpty(eventHubPath))
            {
                // Entity path is expected in the connection string if not provided with eventHubPath parameter.
                if (string.IsNullOrEmpty(csb.EntityPath))
                {
                    throw new ArgumentException(nameof(eventHubConnectionString),
                                                "Provide EventHub entity path either in eventHubPath parameter or in eventHubConnectionString.");
                }
            }
            else
            {
                // Entity path should not conflict with connection string.
                if (!string.IsNullOrEmpty(csb.EntityPath) &&
                    string.Compare(csb.EntityPath, eventHubPath, StringComparison.OrdinalIgnoreCase) != 0)
                {
                    throw new ArgumentException(nameof(eventHubConnectionString),
                                                "Provided EventHub path in eventHubPath parameter conflicts with the path in provided EventHubs connection string.");
                }

                csb.EntityPath = eventHubPath;
            }

            this.HostName                 = hostName;
            this.EventHubPath             = csb.EntityPath;
            this.ConsumerGroupName        = consumerGroupName;
            this.eventHubConnectionString = csb.ToString();
            this.CheckpointManager        = checkpointManager;
            this.LeaseManager             = leaseManager;
            this.TransportType            = csb.TransportType;
            this.OperationTimeout         = csb.OperationTimeout;
            this.EndpointAddress          = csb.Endpoint;
            this.PartitionManager         = new PartitionManager(this);
            ProcessorEventSource.Log.EventProcessorHostCreated(this.HostName, this.EventHubPath);
        }
Esempio n. 18
0
        private static async Task MainAsync(string[] args)
        {
            var builder = new ConfigurationBuilder()
                          .AddEnvironmentVariables();
            var configuration = builder.Build();

            var connectionString        = configuration["EVENTHUB_SENDER_CONNECTION_STRING"];
            var connectionStringBuilder = new EventHubsConnectionStringBuilder(connectionString);

            _client = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());

            await SendBatchesToEventHub(100000, 1000);

            await _client.CloseAsync();
        }
        /// <summary>
        /// Registers a command auditor that writes to an event hub
        /// </summary>
        /// <param name="resolver">Dependency resolver</param>
        /// <param name="connectionString">Connection string to an event hub e.g.:
        /// Endpoint=sb://myeventhub.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=mysharedaccesskey
        /// </param>
        /// <param name="entityPath">The path to the event hub (usually just the event hub name</param>
        /// <param name="partitionKeyProvider">An optional partition key provider, if unspecified events will be sent unpartitioned</param>
        /// <param name="options">Options for the event hub auditor configuration</param>
        /// <returns>Dependency resolver</returns>
        public static ICommandingDependencyResolverAdapter AddEventHubCommandAuditing(this ICommandingDependencyResolverAdapter resolver,
                                                                                      string connectionString,
                                                                                      string entityPath,
                                                                                      IPartitionKeyProvider partitionKeyProvider = null,
                                                                                      AzureEventHubAuditorOptions options        = null)
        {
            EventHubsConnectionStringBuilder builder =
                new EventHubsConnectionStringBuilder(connectionString)
            {
                EntityPath = entityPath
            };

            Microsoft.Azure.EventHubs.EventHubClient client = Microsoft.Azure.EventHubs.EventHubClient.CreateFromConnectionString(builder.ToString());
            return(AddEventHubCommandAuditing(resolver, client, partitionKeyProvider, options));
        }
        public void GenerateEvents()
        {
            var connectionStringBuilder = new EventHubsConnectionStringBuilder(eventHubConnectionString)
            {
                EntityPath = eventHubName
            };

            eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());

            SendMessagesToEventHub(100).GetAwaiter().GetResult();

            eventHubClient.CloseAsync().GetAwaiter();

            //  ViewBag["Message"] = "Events Generated successfully";
        }
Esempio n. 21
0
        private static async Task MainAsync()
        {
            var connection = new EventHubsConnectionStringBuilder(EventHubConnectionString)
            {
                EntityPath = EventHubName
            };

            eventHubClient = EventHubClient.CreateFromConnectionString(connection.ToString());

            await SendMessageToHub();

            await eventHubClient.CloseAsync();

            Console.ReadLine();
        }
Esempio n. 22
0
        private static async Task MainAsync(string[] args)
        {
            // Creates an EventHubsConnectionStringBuilder object from the connection string, and sets the EntityPath.
            // Typically, the connection string should have the entity path in it, but for the sake of this simple scenario
            // we are using the connection string from the namespace.
            var connectionStringBuilder = new EventHubsConnectionStringBuilder(EhConnectionString)
            {
                EntityPath = EhEntityPath
            };

            eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
            await SendMessagesToEventHub(1);

            await eventHubClient.CloseAsync();
        }
        private static async Task MainAsync()
        {
            string EventHubConnectionString = Environment.GetEnvironmentVariable("EventHubConnectionString", EnvironmentVariableTarget.Machine);
            string EventHubName             = Environment.GetEnvironmentVariable("EventHubName", EnvironmentVariableTarget.Machine);

            var connectionStringBuilder = new EventHubsConnectionStringBuilder(EventHubConnectionString)
            {
                EntityPath = EventHubName
            };

            eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
            await SendMessagesToEventHub(100);

            await eventHubClient.CloseAsync();
        }
Esempio n. 24
0
        /// <summary>
        /// Attempts to build an Azure Event Hubs connections string from the component parts defined
        /// in this <see cref="AzureEventHubsLoggerOptions"/> instance.
        /// </summary>
        /// <param name="options"></param>
        /// <param name="connectionString"></param>
        /// <returns>True, if all parts are present and valid. Otherwise, false.</returns>
        public static bool TryGetConnectionString(this AzureEventHubsLoggerOptions options, out string connectionString)
        {
            try
            {
                var builder = new EventHubsConnectionStringBuilder(options.Endpoint, options.EntityPath, options.SharedAccessKeyName, options.SharedAccessKey);

                connectionString = builder.ToString();
                return(true);
            }
            catch
            {
                connectionString = null;
                return(false);
            }
        }
Esempio n. 25
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log)
        {
            var connectionStringBuilder = new EventHubsConnectionStringBuilder(EhConnectionString)
            {
                EntityPath = EhEntityPath
            };

            eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());

            log.Info("C# HTTP trigger function processed a request.");

            return(new HttpResponseMessage {
                StatusCode = HttpStatusCode.OK
            });
        }
Esempio n. 26
0
        public static async void Run()
        {
            _masterDb = Database.GetDatabase("master");
            var hubs           = HubRepository.GetHubs(_masterDb);
            var hubIds         = hubs.Select(p => p.ID);
            var hubsToAdd      = hubs.Where(p => !HubTokens.ContainsKey(p.ID)).ToList();
            var hubIdsToRemove = HubTokens.Select(p => p.Key).Where(p => !hubIds.Contains(p)).ToList();

            // Remove loops that are not necessary anymore
            foreach (var id in hubIdsToRemove)
            {
                if (!HubTokens.ContainsKey(id))
                {
                    continue;
                }
                foreach (var token in HubTokens[id])
                {
                    token.Cancel();
                }
            }

            // Create hub loops
            foreach (var hub in hubsToAdd)
            {
                // Create an EventHubClient instance to connect to the IoT Hub Event Hubs-compatible endpoint.
                var connectionString = new EventHubsConnectionStringBuilder(new Uri(hub.EventHubscompatibleEndpoint),
                                                                            hub.EventHubscompatiblePath, IotHubSasKeyName, hub.ServicePrimaryKey);
                var eventHubClient = EventHubClient.CreateFromConnectionString(connectionString.ToString());

                var tokens = new List <CancellationTokenSource>();
                HubTokens.Add(hub.ID, tokens);

                var runtimeInfo = await eventHubClient.GetRuntimeInformationAsync();

                var d2CPartitions = runtimeInfo.PartitionIds;

                var tasks = new List <Task>();
                foreach (var partition in d2CPartitions)
                {
                    var cts = new CancellationTokenSource();
                    tokens.Add(cts);
                    tasks.Add(ReceiveMessagesFromDeviceAsync(eventHubClient, partition, cts.Token));
                }

                // Wait for all the PartitionReceivers to finish.
                Task.WaitAll(tasks.ToArray());
            }
        }
Esempio n. 27
0
        /// <summary>
        /// Gets (instantiates) the <see cref="EventProcessorHost"/> for the specified <paramref name="eventHub"/> and <paramref name="consumerGroup"/>.
        /// </summary>
        /// <param name="config">The <see cref="IConfiguration"/>.</param>
        /// <param name="eventHub">The event hub name.</param>
        /// <param name="consumerGroup">The consumer group.</param>
        /// <returns>The <see cref="EventProcessorHost"/>.</returns>
        public EventProcessorHost GetEventProcessorHost(IConfiguration config, string eventHub, string consumerGroup)
        {
            if (!_eventHubs.TryGetValue(eventHub, out string connectionString))
            {
                throw new InvalidOperationException($"The specified Event Hub '{eventHub}' does not exist.");
            }

            // Build the storage connection string.
            var storageConnectionString = config.GetWebJobsConnectionString(ConnectionStringNames.Storage);

            EventHubsConnectionStringBuilder cs = new EventHubsConnectionStringBuilder(connectionString);

            if (cs.EntityPath != null)
            {
                eventHub      = cs.EntityPath;
                cs.EntityPath = null;
            }

            EventHubPath = EscapeBlobPath(cs.Endpoint.Host);
            EventHubName = EscapeBlobPath(eventHub);
            var blobPrefix = $"{EventHubPath}/{EventHubName}/";

            var maxRetryMinutes = config.GetValue <int>("MaxRetryMinutes");

            if (maxRetryMinutes > 0 && maxRetryMinutes <= 60 * 24)
            {
                MaxRetryTimespan = TimeSpan.FromMinutes(maxRetryMinutes);
            }

            var logPoisonCount = config.GetValue <int>("LogPoisonMessageAfterRetryCount");

            if (logPoisonCount > 0 && logPoisonCount <= 10)
            {
                LogPoisonMessageAfterRetryCount = logPoisonCount;
            }

            return(new EventProcessorHost(
                       hostName: Guid.NewGuid().ToString(),
                       eventHubPath: eventHub,
                       consumerGroupName: consumerGroup,
                       eventHubConnectionString: cs.ToString(),
                       storageConnectionString: storageConnectionString,
                       leaseContainerName: "azure-webjobs-eventhub",
                       storageBlobPrefix: blobPrefix)
            {
                PartitionManagerOptions = PartitionManagerOptions
            });
        }
Esempio n. 28
0
        /// <summary>
        /// Iterates through the customers and populates their data into SQL
        /// </summary>
        public async Task PopulateRecentRateAndUsageInformation(CustomerContainer container)
        {
            var connectionStringBuilder = new EventHubsConnectionStringBuilder(_eventConnectionString)
            {
                EntityPath = _eventHubName
            };
            var eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
            var now            = DateTime.UtcNow;
            //Set the sart and end dates to pull data into the db
            var lastUpdateReportedStart = DateTime.UtcNow.Subtract(TimeSpan.FromHours(6));
            var reportEndDate           = DateTime.UtcNow.Subtract(TimeSpan.FromHours(5));
            var usage = new AzureUsage(container);
            var rates =
                await usage.GetCombinedRatesAndUsage(lastUpdateReportedStart, reportEndDate, container.OfferId);

            int index         = 0;
            int count         = rates.Value.Count;
            var customerDatas = new List <CustomerData>();

            foreach (var rate in rates.Value)
            {
                string jsonOut = JsonConvert.SerializeObject(new CustomerData()
                {
                    Category             = rate.Properties.MeterCategory,
                    MeterId              = rate.Properties.MeterId,
                    Price                = rate.Properties.Price,
                    Cost                 = rate.Properties.Cost,
                    Unit                 = rate.Properties.Unit,
                    Quantity             = rate.Properties.Quantity,
                    MeterName            = rate.Properties.MeterName,
                    StartTime            = DateTime.Parse(rate.Properties.UsageStartTime),
                    EndTime              = DateTime.Parse(rate.Properties.UsageEndTime),
                    SubscriptionId       = container.SubscriptionId,
                    ResourceGroup        = rate.Properties.ResourceGroup,
                    ReportedStartDate    = rates.ReportedStartDate,
                    ReportedEndDate      = rates.ReportedEndData,
                    ResourceProvider     = rate.Properties.ResourceProvider,
                    ResourceName         = rate.Properties.ResourceName,
                    ResourceInstanceName = rate.Properties.ResourceSubName,
                    Tags                 = rate.Properties.Tags,
                    Location             = rate.Properties.Location
                });
                await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(jsonOut)));

                Console.WriteLine($"Adding record {++index} of {count} ");
            }
            await eventHubClient.CloseAsync();
        }
Esempio n. 29
0
        private static async Task MainAsync(string[] args)
        {
            #region STRING/AZURE SETUP

            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json");

            var configuration = builder.Build();

            var connectionStringBuilder = new EventHubsConnectionStringBuilder(configuration.GetSection("ConnectionStrings:DesktopTrackerHub").Value)
            {
                EntityPath = configuration.GetSection("DesktopTrackerEventHub:CloseMessage").Value
            };

            eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());

            #endregion

            AutoResetEvent autoEvent = new AutoResetEvent(false);

            Console.WriteLine("Checking Active Windows at: {0:h:mm:ss.fff} \n",
                              DateTime.UtcNow);

            #region WINDOW TIMER
            //Enter null for no break clause, e.g. program goes on forever
            ActiveChecker activeWindows = new ActiveChecker(null);

            //1000 = 1 second etc.
            Timer foregroundTimer = new Timer(activeWindows.CheckWindows, autoEvent, 0, 5000);

            #endregion

            #region HIBERNATE TIMER
            //Enter max time for Kiosk to hibernate
            DateTime maxTimeForHibernate = DateTime.UtcNow.AddMinutes(30);

            HibernateChecker hibernateChecker = new HibernateChecker(maxTimeForHibernate);

            //Checks silently every 0.25 seconds
            Timer hibernateTimer = new Timer(hibernateChecker.CheckHibernate, autoEvent, 0, 250);
            #endregion

#if DEBUG
            //Keeps console open
            Console.ReadLine();
#endif
        }
Esempio n. 30
0
        private static async Task Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json");

            Configuration = builder.Build();

            Console.WriteLine("IoT Hub Monitor - Read device to cloud messages. Ctrl-C to exit.\n");

            // Create an EventHubClient instance to connect to the
            // IoT Hub Event Hubs-compatible endpoint.
            var hubConnectionString = new EventHubsConnectionStringBuilder(new Uri(Configuration[EventHubEndpointSetting]), Configuration[EventHubCompatibleNameSetting], Configuration[SasKeyNameSetting], Configuration[SasKeySetting]);

            _eventHubClient = EventHubClient.CreateFromConnectionString(hubConnectionString.ToString());

            // Create a PartitionReciever for each partition on the hub.
            var hubRuntimeInfo = await _eventHubClient.GetRuntimeInformationAsync();

            List <Tuple <EventHubClient, string> > d2CPartitions =
                hubRuntimeInfo.PartitionIds
                .Select(id => new Tuple <EventHubClient, string>(_eventHubClient, id))
                .ToList();

            CancellationTokenSource cts = new CancellationTokenSource();

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

            var tasks = new List <Task>();

            foreach (Tuple <EventHubClient, string> eventHubClientPartitionPair in d2CPartitions)
            {
                tasks.Add(ReceiveMessagesFromDeviceAsync(eventHubClientPartitionPair.Item1, eventHubClientPartitionPair.Item2, cts.Token));
            }

            if (Configuration[SimulateEventsSetting] == "true")
            {
                tasks.Add(SimulateEvents(cts.Token));
            }

            // Wait for all the PartitionReceivers to finish.
            Task.WaitAll(tasks.ToArray());
        }