public HttpResponseMessage Subscribe([FromBody] EventHubInput input) { if (!String.IsNullOrEmpty(input.callbackUrl)) { if (!InMemoryTriggerStore.Instance.GetStore().ContainsKey(input.callbackUrl)) { HostingEnvironment.QueueBackgroundWorkItem(async ct => await InMemoryTriggerStore.Instance.RegisterTrigger(input)); } } return(Request.CreateResponse()); }
/// <summary> /// The method that registers Event Hub listeners and assigns them to a Receive event. When I receive an event from the event hub listener, I trigger the callbackURL /// </summary> /// <param name="triggerId"></param> /// <param name="triggerInput"></param> public async Task RegisterTrigger(EventHubInput input) { var client = EventHubClient.CreateFromConnectionString(input.eventHubConnectionString, input.eventHubName); EventHubConsumerGroup group = String.IsNullOrEmpty(input.consumerGroup) ? client.GetDefaultConsumerGroup() : client.GetConsumerGroup(input.consumerGroup); string[] partitions; //If they specified partitions, iterate over their list to only listen to the partitions they specified if (!String.IsNullOrEmpty(input.eventHubPartitionList)) { partitions = input.eventHubPartitionList.Split(','); } //If they left it blank, create a list to listen to all partitions else { partitions = new string[client.GetRuntimeInformation().PartitionCount]; for (int x = 0; x < partitions.Length; x++) { partitions[x] = x.ToString(); } } List <CancellationTokenSource> tokenSources = new List <CancellationTokenSource>(); //For ever partition I should listen to, create a thread with a listener on it foreach (var p in partitions) { p.Trim(); var Receiver = group.CreateReceiver(client.GetRuntimeInformation().PartitionIds[int.Parse(p)], DateTime.UtcNow); EventHubListener listener = new EventHubListener(Receiver); //Register the event. When I Receive a message, call the method to trigger the logic app listener.MessageReceived += (sender, e) => TriggerLogicApps(input.callbackUrl, e); var ts = new CancellationTokenSource(); CancellationToken ct = ts.Token; listener.StartListening(ct); tokenSources.Add(ts); } //Register the triggerID in my store, so on subsequent checks from the logic app I don't spin up a new set of listeners _store[input.callbackUrl] = tokenSources; }