1. Create Event hub on Azure
2. Create a prj, Event hub sender, install NuGet pkg-azure service Bus
3. Check connection string
4. Sender Sample Code
static void Main (string[] args) {Console.WriteLine ("Press Ctrl-c to stop the sender process"); Console.WriteLine ("Press Enter to start now"); Console.ReadLine (); Sendingrandommessages (); } static string eventhubname = "Get from event hub connection information"; static string connectionString = "Get from event hub connection information"; static void Sendingrandommessages () {var eventhubclient = eventhubclient.createfromconnectionstring (con Nectionstring, Eventhubname); while (true) {try {var message = Guid.NewGuid (). ToString (); Console.WriteLine ("{0} > Sending message: {1}", DateTime.Now, message); Eventhubclient.send (New EventData (Encoding.UTF8.GetBytes (message))); } catch (Exception Exception) {Console.foregrouNdcolor = consolecolor.red; Console.WriteLine ("{0} > Exception: {1}", DateTime.Now, Exception. Message); Console.resetcolor (); } thread.sleep (200); } }
5. Create a Storage account
6. Install NuGet for receiver PRJ
7. Check keys
8. Sample code for receiver
Class Simpleeventprocessor:ieventprocessor {Stopwatch checkpointstopwatch; Async Task Ieventprocessor.closeasync (partitioncontext context, Closereason reason) {Console.WriteLine ( "Processor shutting down. Partition ' {0} ', Reason: ' {1} '. ', context. Lease.partitionid, reason); if (reason = = Closereason.shutdown) {await context. Checkpointasync (); }} Task Ieventprocessor.openasync (Partitioncontext context) {Console.WriteLine ("Simpleev Entprocessor initialized. Partition: ' {0} ', Offset: ' {1} ', context. Lease.partitionid, context. Lease.offset); This.checkpointstopwatch = new Stopwatch (); This.checkpointStopWatch.Start (); return task.fromresult<object> (NULL); } async Task Ieventprocessor.processeventsasync (partitioncontext context, ienumerable<eventdata> messages) {foreach (EventData EventData in MEssages) {String data = Encoding.UTF8.GetString (Eventdata.getbytes ()); Console.WriteLine (String. Format ("Message received. Partition: ' {0} ', Data: ' {1} ', context. Lease.partitionid, data)); }//call checkpoint every 5 minutes, so the worker can resume processing from 5 minutes to back if it restarts. if (This.checkpointStopWatch.Elapsed > Timespan.fromminutes (5)) {await context. Checkpointasync (); This.checkpointStopWatch.Restart (); }}}static void Main (string[] args) {string eventhubconnectionstring = "Get from Azure event" Hub connection Information "; String eventhubname = "Get from Azure Event hub connection Information"; String storageaccountname = "Get from Azure storage keys"; String Storageaccountkey = "Get from Azure storage keys"; String storageconnectionstring = String. Format ("DEFAULTENDPOINTSPROTOCOL=HTTPS; ACCOUNTNAME={0}; Accountkey={1} ", Storageaccountname, Storageaccountkey); String eventprocessorhostname = Guid.NewGuid (). ToString (); Eventprocessorhost eventprocessorhost = new Eventprocessorhost (Eventprocessorhostname, EventHubName, Eventhubconsumergroup.defaultgroupname, eventhubconnectionstring, storageconnectionstring); Console.WriteLine ("Registering eventprocessor ..."); var options = new Eventprocessoroptions (); Options. exceptionreceived + = (sender, E) = = {Console.WriteLine (e.exception);}; Eventprocessorhost.registereventprocessorasync<simpleeventprocessor> (Options). Wait (); Console.WriteLine ("Receiving. Press ENTER key to stop worker. "); Console.ReadLine (); Eventprocessorhost.unregistereventprocessorasync (). Wait (); }
Azure Event Hub Usage