static void Main(string[] args) { try { // Connection String as can be found on the azure portal https://manage.windowsazure.com/microsoft.onmicrosoft.com#Workspaces/ServiceBusExtension/namespaces // // The needed Shared access policy is Manage, this can either be the RootManageSharedAccessKey or one create with Manage priviledges // "Endpoint = sb://NAMESPACE.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=THISISMYKEY"; var policyName = "RootManageSharedAccessKey"; var policySharedAccessKey = "THISISMYKEY"; var serviceBusNamespace = "NAMESPACE"; var credentials = TokenProvider.CreateSharedAccessSignatureTokenProvider(policyName, policySharedAccessKey); // access the namespace var serviceBusUri = ServiceBusEnvironment.CreateServiceUri("sb", serviceBusNamespace, string.Empty); var manager = new NamespaceManager(serviceBusUri, credentials); // Create the eventhub if needed var description = manager.CreateEventHubIfNotExists("MyHub"); Console.WriteLine("AT: " + description.CreatedAt + " EventHub:" + description.Path + " Partitions: " + description.PartitionIds); } catch (Exception e) { Console.WriteLine(e.ToString()); } Console.ReadLine(); }
public EventReceiver() { // Create namespace client NamespaceManager namespaceClient = NamespaceManager.CreateFromConnectionString(_connectionString); namespaceClient.CreateEventHubIfNotExists(_eventHubName); }
static void Main(string[] args) { string serviceBusNamespace = "Please Provide Your ServiceBus Namespace"; string serviceBusManageKey = "Please Provide Your ServiceBus Shared Access Key"; Uri serviceBusUri = ServiceBusEnvironment.CreateServiceUri("sb", serviceBusNamespace, string.Empty); TokenProvider tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", serviceBusManageKey); NamespaceManager nameSpaceManager = new NamespaceManager(string.Format("//{0}/", serviceBusUri.Host), tokenProvider); string eventHubName = "EventHubCreatedWithCode"; string eventHubKeyName = "EventHubKey"; string eventHubKey = SharedAccessAuthorizationRule.GenerateRandomKey(); EventHubDescription eventHubDescription = new EventHubDescription(eventHubName) { PartitionCount = 8, MessageRetentionInDays = 1 }; SharedAccessAuthorizationRule eventHubSendRule = new SharedAccessAuthorizationRule(eventHubKeyName, eventHubKey, new[] { AccessRights.Send, AccessRights.Listen }); eventHubDescription.Authorization.Add(eventHubSendRule); eventHubDescription = nameSpaceManager.CreateEventHubIfNotExists(eventHubDescription); string primaryKey = ((SharedAccessAuthorizationRule)eventHubDescription.Authorization.First()).PrimaryKey; Console.WriteLine("Primary Key: {0}", primaryKey); Console.ReadLine(); }
static void Main(string[] args) { var csb = new ServiceBusConnectionStringBuilder(ConnString); csb.TransportType = TransportType.Amqp; // Create factories. nm = NamespaceManager.CreateFromConnectionString(ConnString); // Create Event Hub. try { nm.CreateEventHubIfNotExists(new EventHubDescription(EhName) { PartitionCount = PartitionCount, }); } catch (Exception ex) { Console.WriteLine($"Something went wrong while creating Event Hub {EhName}. Check the exception details."); throw; } // Spawn number of sender clients as requested. for (int clientInd = 0; clientInd < NumberOfParallelClients; clientInd++) { // Use dedicated factory for each sender. This will help to distribute load accross remote service nodes. var newSenderClient = new SenderClient(clientInd, csb, EhName, cancellationTokenSource.Token); senderClients.Add(newSenderClient); } // Set a timer so we can track the total number of events sent from each client. // Schedule the call for every 10 seconds. monitorTimer = new Timer( new TimerCallback(MonitorClients), null, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10)); // Keep sending until user interrupts. Console.WriteLine("Press ENTER to stop sending on all Event Hub clients."); Console.ReadLine(); // Signal and wait for all sender tasks to complete. Console.WriteLine("Signaling the clients to stop sending."); cancellationTokenSource.Cancel(); Task.WaitAll(senderClients.Select(sc => sc.SendTask).ToArray()); Console.WriteLine("All clients successfully completed sending."); monitorTimer.Dispose(); }
/// <summary> /// Checks if a event hub by the provided <paramref name="hubName"/> exists and /// Checks if a consumer group by the provided <paramref name="consumerGroupNames"/> exists. /// </summary> protected virtual void CheckHubExists(NamespaceManager namespaceManager, string hubName, string consumerGroupNames) { // Configure Queue Settings var eventHubDescription = new EventHubDescription(hubName) { MessageRetentionInDays = long.MaxValue, }; // Create the topic if it does not exist already namespaceManager.CreateEventHubIfNotExists(eventHubDescription); var subscriptionDescription = new SubscriptionDescription(eventHubDescription.Path, consumerGroupNames); if (!namespaceManager.SubscriptionExists(eventHubDescription.Path, consumerGroupNames)) { namespaceManager.CreateSubscription(subscriptionDescription); } }
public string GetCreateEHAndConsumerGroup(string ConsumenrGroup, ref string eventHubName, ref string eventHubConnectionString, ref string storageConnectionString) { try { //Set connection strings eventHubConnectionString = "Endpoint=sb://[NAMESPACE].servicebus.windows.net/;SharedAccessKeyName=All;SharedAccessKey=[SharedAccessKey]"; string eventHubConnectionStringAdmin = "Endpoint=sb://[NAMESPACE].servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=[SharedAccessKey]"; eventHubName = "[EVENTHUBNAME]"; string storageAccountName = "[STORAGEACCOUNTNAME]"; string storageAccountKey = "[STORAGEACCOUNTKEY]"; storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", storageAccountName, storageAccountKey); //Create connection string var builder = new ServiceBusConnectionStringBuilder(eventHubConnectionString) { TransportType = Microsoft.ServiceBus.Messaging.TransportType.Amqp }; ConsoleWriteLine("Create/Open the EH", ConsoleColor.Magenta); NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(eventHubConnectionStringAdmin); var evenhubDesc = new EventHubDescription(eventHubName); namespaceManager.CreateEventHubIfNotExists(evenhubDesc); if (ConsumenrGroup == null) { var client = EventHubClient.CreateFromConnectionString(builder.ToString(), eventHubName); EventHubConsumerGroup eventHubConsumerGroup = client.GetDefaultConsumerGroup(); return(eventHubConsumerGroup.GroupName); } else { namespaceManager.CreateConsumerGroupIfNotExists(eventHubName, ConsumenrGroup); return(ConsumenrGroup); } } catch (Exception ex) { ConsoleWriteLine("ERROR-" + ex.Message, ConsoleColor.Red); return(null); } }
// static string eventHubName = ConfigurationManager.AppSettings["eventHubName"]; // static string connectionString = ConfigurationManager.AppSettings["sbConnectionString"]; static void Main(string[] args) { string eventHubName = args[0]; string connectionString = args[1]; double mean = 0; double stdDev = 5 / 3; int numRooms = 2; int plantsInRoom = 4; string[] plantTypes = { "Rose", "SugarCane", "Watermelon", "Wheat" }; //Set up connections to Service Bus and create the client object ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connectionString); builder.TransportType = TransportType.Amqp; MessagingFactory factory = MessagingFactory.CreateFromConnectionString(builder.ToString()); NamespaceManager manager = NamespaceManager.CreateFromConnectionString(connectionString); EventHubDescription description = manager.CreateEventHubIfNotExists(eventHubName); EventHubClient client = factory.CreateEventHubClient(eventHubName); //Allows for number of threads to be specified try { numThreads = Convert.ToInt32(ConfigurationManager.AppSettings["numThreads"]); } catch { } List <Sensor> sensors = setupGreenhouse(numRooms, plantsInRoom, plantTypes); //This loop continues to run and has all of the sensors updated and events sent. while (true) { advanceTime(sensors, mean, stdDev, client); Thread.Sleep(10000); } }