static void Main(string[] args) { // Show Usage information if (args.Length < 3) Usage(); // Init _init(); // Setup service bus string connectionString = GetServiceBusConnectionString(); NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString); eventHubName = ConfigurationManager.AppSettings["EventHubName"]; partitionCurrent = 0; // Assumes you already have the Event Hub created. client = EventHubClient.Create(eventHubName); // Generate the data GenerateData(args); Console.ReadKey(); }
//------------------------------------------------------------------------------------------------------------------------ #endregion #region Constructor //------------------------------------------------------------------------------------------------------------------------ public EventHubDirectConsumer(EventHubClient client) { //create event hub direct consumer EventHubConsumerGroup group = client.GetDefaultConsumerGroup(); var x = client.GetRuntimeInformation().PartitionIds[0]; eventHubDirectReceiver = group.CreateReceiver(client.GetRuntimeInformation().PartitionIds[0]); }
/// <summary> /// A sink that puts log events into a provided Azure Event Hub. /// </summary> /// <param name="loggerConfiguration">The logger configuration.</param> /// <param name="eventHubClient">The Event Hub to use to insert the log entries to.</param> /// <param name="partitionKey">The partition key used to group log events in the Event Hub.</param> /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> /// <param name="outputTemplate">A message template describing the format used to write to the sink. /// the default is "{Timestamp} [{Level}] {Message}{NewLine}{Exception}".</param> /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param> /// <param name="writeInBatches">Use a periodic batching sink, as opposed to a synchronous one-at-a-time sink; this alters the partition /// key used for the events so is not enabled by default.</param> /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <returns>Logger configuration, allowing configuration to continue.</returns> /// <exception cref="ArgumentNullException">A required parameter is null.</exception> public static LoggerConfiguration AzureEventHub( this LoggerSinkConfiguration loggerConfiguration, EventHubClient eventHubClient, string partitionKey = null, string outputTemplate = DefaultOutputTemplate, IFormatProvider formatProvider = null, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, bool writeInBatches = false, TimeSpan? period = null, int? batchPostingLimit = null ) { if (loggerConfiguration == null) throw new ArgumentNullException("loggerConfiguration"); if (eventHubClient == null) throw new ArgumentNullException("eventHubClient"); if (outputTemplate == null) throw new ArgumentNullException("outputTemplate"); var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider); var sink = writeInBatches ? (ILogEventSink) new AzureEventHubBatchingSink( eventHubClient, partitionKey ?? DefaultPartitionKey, formatter, batchPostingLimit ?? DefaultBatchPostingLimit, period ?? DefaultPeriod) : new AzureEventHubSink( eventHubClient, partitionKey ?? DefaultPartitionKey, formatter); return loggerConfiguration.Sink(sink, restrictedToMinimumLevel); }
//------------------------------------------------------------------------------------------------------------------------ #endregion #region Constructor //------------------------------------------------------------------------------------------------------------------------ public EventHubDev(string evhubname, string connectionstring) { this.eventHubName = evhubname; this.connectionName = connectionstring; //create eventhub client eventHubClient = EventHubClient.CreateFromConnectionString(this.connectionName, this.eventHubName); }
//------------------------------------------------------------------------------------------------------------------------ #endregion #region Constructor public AzureIOTApplication(string connectionString) { //create an eventhubclient for receiving messages from the Azure IOT Hub eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, "messages/events"); //create a sericeclient, able to send messages to the Azure IOT Hub Devices serviceClient = ServiceClient.CreateFromConnectionString(connectionString); }
public static void Main() { // string connectionString = //ConfigurationManager.ConnectionStrings["RootManageSharedAccessKey"].ConnectionString; // Action<BrokeredMessage> callback = x => // { // }; // var clients = new List<SubscriptionClient>(); // for (int i = 0; i < 5; i++) // { // var client = TopicClient.CreateFromConnectionString(connectionString, "signalr_topic_push_" + i); // client. // client.OnMessage(callback); // clients.Add(client); // } // Console.ReadLine(); //var ctx = GlobalHost.ConnectionManager.GetHubContext<yourhub>(); //ctx.Clients.Client(connectionId).< your method > var cloudStorage = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["DataStorage"].ConnectionString); var tableClient = cloudStorage.CreateCloudTableClient(); _tickEvents = tableClient.GetTableReference("tickevents"); _tickEvents.CreateIfNotExists(); var host = new JobHost(); var cancelToken = new WebJobsShutdownWatcher().Token; _eventHubClient = EventHubClient.CreateFromConnectionString(ConfigurationManager.ConnectionStrings["IotHubConnection"].ConnectionString, iotHubD2cEndpoint); var d2CPartitions = _eventHubClient.GetRuntimeInformation().PartitionIds; Task.WaitAll(d2CPartitions.Select(partition => ListenForEvent(host, partition, cancelToken)).ToArray(), cancelToken); host.RunAndBlock(); }
static void Main(string[] args) { //Create the event hub var manager = Microsoft.ServiceBus.NamespaceManager.CreateFromConnectionString(ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"]); var tweethub = manager.CreateEventHubIfNotExists("TweetHubSimple"); tweetHubClient = EventHubClient.Create(tweethub.Path); var oauth = CredentialsCreator_CreateFromRedirectedCallbackURL_SingleStep(ConfigurationManager.AppSettings["twitterkey"], ConfigurationManager.AppSettings["twittersecret"]); // Setup your credentials TwitterCredentials.SetCredentials(oauth.AccessToken, oauth.AccessTokenSecret, oauth.ConsumerKey, oauth.ConsumerSecret); // Access the filtered stream var filteredStream = Tweetinvi.Stream.CreateFilteredStream(); filteredStream.AddTrack("globalazurebootcamp"); filteredStream.AddTrack("azure"); filteredStream.AddTrack("microsoft"); filteredStream.MatchingTweetReceived += (sender, a) => { Console.WriteLine(a.Tweet.Text); var str = JsonConvert.SerializeObject(new { Tweet = a.Tweet.Text, Lang = a.Tweet.Language, Created_At = a.Tweet.CreatedAt }); tweetHubClient.Send(new EventData(System.Text.Encoding.UTF8.GetBytes(str))); }; //filteredStream.JsonObjectReceived += (sender, json) => //{ // ProcessTweet(json.Json); //}; filteredStream.StartStreamMatchingAllConditions(); }
public override bool OnStart() { // Set the maximum number of concurrent connections ServicePointManager.DefaultConnectionLimit = 12; // For information on handling configuration changes // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357. bool result = base.OnStart(); Trace.TraceInformation("EventsForwarding OnStart()...\n"); connectionString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"]; eventHubName = ConfigurationManager.AppSettings["Microsoft.ServiceBus.EventHubName"]; string storageAccountName = ConfigurationManager.AppSettings["AzureStorage.AccountName"]; string storageAccountKey = ConfigurationManager.AppSettings["AzureStorage.Key"]; string storageAccountString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", storageAccountName, storageAccountKey); string iotHubConnectionString = ConfigurationManager.AppSettings["AzureIoTHub.ConnectionString"]; iotHubServiceClient = ServiceClient.CreateFromConnectionString(iotHubConnectionString); eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName); var defaultConsumerGroup = eventHubClient.GetDefaultConsumerGroup(); string eventProcessorHostName = "SensorEventProcessor"; EventProcessorHost eventProcessorHost = new EventProcessorHost(eventProcessorHostName, eventHubName, defaultConsumerGroup.GroupName, connectionString, storageAccountString); eventProcessorHost.RegisterEventProcessorAsync<SensorEventProcessor>().Wait(); Trace.TraceInformation("Receiving events...\n"); return result; }
public async Task StartAsync(EventHubClient client, CancellationToken token) { var groups = await _nsm.GetConsumerGroupsAsync(EventHubName); groups.ToList() .ForEach(g => ConsumerGroups.Add(g.Name)); var defaultConsumerGroup = ConsumerGroups.First(); var runtime = await client.GetRuntimeInformationAsync(); runtime.PartitionIds .Select(partitionId => { Func<Task<PartitionDescription>> f = () => _nsm.GetEventHubPartitionAsync(EventHubName, defaultConsumerGroup, partitionId); return new PartitionViewModel(partitionId, client, f); }) .ToList() .ForEach(p => Partitions.Add(p)); foreach (var partition in Partitions) { partition.WhenEventReceived.Subscribe(x => { _aggregatedEventCount.OnNext(x); }); _listeners.Add(partition.StartAsync(CancellationToken.None)); } _aggregatedEventCount .Buffer(TimeSpan.FromSeconds(5)) .TimeInterval() .Select(x => x.Value.Sum() / x.Interval.TotalSeconds) .Subscribe(rate => RatePerSecond = rate); }
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 async Task OpenAsync(PartitionContext context) { if (!WebJobsHelper.RunAsWebJobs) Console.WriteLine(string.Format("EventProcessor initialization. Partition: '{0}', Offset: '{1}'", context.Lease.PartitionId, context.Lease.Offset)); partitionContext = context; var retries = 3; while (retries > 0) { try { retries--; hubClient = EventHubClient.CreateFromConnectionString( ConfigurationManager.ConnectionStrings["SigfoxDemoAlertSender"].ConnectionString, "alert"); cacheConnection = await ConnectionMultiplexer.ConnectAsync(ConfigurationManager.ConnectionStrings["SigfoxDemoCache"].ConnectionString); cacheDatabase = cacheConnection.GetDatabase(); sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["SigfoxDemoDatabase"].ConnectionString); //sqlConnection.Open(); //sqlCommand = new SqlCommand("InsertAlert", sqlConnection) { CommandType = CommandType.StoredProcedure }; //sqlCommand.Parameters.Add(new SqlParameter("@Device", SqlDbType.VarChar)); retries = 0; } catch (Exception e) { Console.Error.WriteLine("Error opening destination Event Hub: " + e.Message); if (retries == 0) throw; } } checkpointStopWatch = new Stopwatch(); checkpointStopWatch.Start(); }
private void SendDeviceEventStream(EventHubClient eventHubClient) { var allEvents = new List<EventData>(); for (int i = 0; i < DeviceSendingDetails.NumberOfDevices; i++) { string deviceName = "device" + i; var rand = new Random(); // set up the modifier to enable float modifier = 1.0F; if (DeviceSendingDetails.FailureConditions.Any(device => device.FailedDeviceId == i)) { var deviceDetails = DeviceSendingDetails.FailureConditions.First(device => device.FailedDeviceId == i); modifier = modifier += deviceDetails.FailedDeviceGradient; } var deviceValue = rand.Next((int) ((DeviceSendingDetails.TemperatureMin * modifier)*100), (int) ((DeviceSendingDetails.TemperatureMax * modifier)*100)); var deviceData = new DeviceData() { deviceid = deviceName, temperature = (deviceValue/100F), timestamp = DateTime.UtcNow }; var jsonDeviceDetail = JsonConvert.SerializeObject(deviceData); var encodedPayload = Encoding.UTF8.GetBytes(jsonDeviceDetail); var eventData = new EventData(encodedPayload) { PartitionKey = "devices" }; allEvents.Add(eventData); } eventHubClient.SendBatch(allEvents); }
private async Task Publish(EventHubClient client, QuickBlockTransferSegment segment) { try { await client.SendAsync(new EventData(segment.Content) { PartitionKey = segment.GetKey(), Properties = { {nameof(segment.Header), segment.Header}, {nameof(segment.ReceivedAt), segment.ReceivedAt}, {nameof(segment.Filename), segment.Filename}, {nameof(segment.TimeStamp), segment.TimeStamp}, {nameof(segment.Checksum), segment.Checksum}, {nameof(segment.BlockNumber), segment.BlockNumber}, {nameof(segment.TotalBlocks), segment.TotalBlocks}, {nameof(segment.Version), segment.Version}, {nameof(segment.Source), segment.Source} } }); } catch (Exception ex) { _log.Error(ex.ToString()); } }
internal EvalOperation(OnlineTrainerSettingsInternal settings) { this.telemetry = new TelemetryClient(); // evaluation pipeline this.evalEventHubClient = EventHubClient.CreateFromConnectionString(settings.EvalEventHubConnectionString); this.evalBlock = new TransformManyBlock<object, EvalData>( (Func<object, IEnumerable<EvalData>>)this.OfflineEvaluate, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 4, BoundedCapacity = 1024 }); this.evalBlock.Completion.ContinueWith(t => { this.telemetry.TrackTrace($"Stage 3 - Evaluation pipeline completed: {t.Status}"); if (t.IsFaulted) this.telemetry.TrackException(t.Exception); }); // batch output together to match EventHub throughput by maintaining maximum latency of 5 seconds this.evalBlockDisposable = this.evalBlock.AsObservable() .GroupBy(k => k.PolicyName) .Select(g => g.Window(TimeSpan.FromSeconds(5)) .Select(w => w.Buffer(245 * 1024, e => Encoding.UTF8.GetByteCount(e.JSON))) .SelectMany(w => w) .Subscribe(this.UploadEvaluation)) .Publish() .Connect(); }
public Task OpenAsync(PartitionContext context) { if (!WebJobsHelper.RunAsWebJobs) Console.WriteLine(string.Format("EventProcessor initialization. Partition: '{0}', Offset: '{1}'", context.Lease.PartitionId, context.Lease.Offset)); partitionContext = context; var retries = 3; while (retries > 0) { try { retries--; hubClient = EventHubClient.CreateFromConnectionString( ConfigurationManager.ConnectionStrings["SigfoxDemoDispatchSender"].ConnectionString, "dispatch"); retries = 0; } catch (Exception e) { Console.Error.WriteLine("Error opening destination Event Hub: " + e.Message); if (retries == 0) throw; } } checkpointStopWatch = new Stopwatch(); checkpointStopWatch.Start(); return Task.FromResult<object>(null); }
public void GenerateMessages() { if (DeviceSendingDetails.RedirectToLocalFile == false) { var connectionString = String.Format( "Endpoint=sb://{0}.servicebus.windows.net/;SharedAccessKeyName={1};SharedAccessKey={2};TransportType=Amqp", EventHubConnectionDetails.ServiceBusNamespace, EventHubConnectionDetails.SasPolicyName, EventHubConnectionDetails.SasPolicyKey); var factory = MessagingFactory.CreateFromConnectionString(connectionString); EventHubClient = factory.CreateEventHubClient(EventHubConnectionDetails.EventHubName); } if (DeviceSendingDetails.RedirectToLocalFile) Console.WriteLine("Redirecting messages to file {0}", DeviceSendingDetails.RedirectFileName); var startTime = DateTime.UtcNow; for (var i = 0; i < DeviceSendingDetails.IterationSeconds; i++) { SendDeviceEventStream(); Console.WriteLine("Messages fired onto the eventhub!"); Thread.Sleep(DeviceSendingDetails.MillisecondDelay); } var duration = DateTime.UtcNow - startTime; Console.WriteLine("Total Time (ms) : {0}", duration.TotalMilliseconds); }
/// <summary> /// Creates a new instance which consumes messages from iot hub. /// </summary> /// <param name="endpointId">Id of the <see cref="DeviceToCloudEndpoint"/> producing the messages.</param> /// <param name="iotHubConnectionString">Iot hub connection string.</param> public CloudToDeviceEndpoint(string endpointId, string iotHubConnectionString) { if (String.IsNullOrEmpty(iotHubConnectionString)) throw new ArgumentException($"Argument {nameof(iotHubConnectionString)} must be non empty string."); if (String.IsNullOrEmpty(endpointId)) throw new ArgumentException($"Argument {nameof(endpointId)} must be non empty string."); _endpointId = endpointId; _iotHubConnectionString = iotHubConnectionString; _eventHubClient = EventHubClient.CreateFromConnectionString(iotHubConnectionString, _iotHubDeviceToCloudEndpoint); }
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; }
protected void StartEH_Click(object sender, EventArgs e) { if (SendButton.Text.StartsWith("Start")) { Global.ifStart = true; SendButton.Text = "Stop Event Hub"; //set Event Hub Client MessagingFactory factory = MessagingFactory.CreateFromConnectionString(Global.EventHubPars.ConnectionString + ";TransportType=Amqp"); client = factory.CreateEventHubClient(Global.EventHubPars.EHname); //initialise sensors sensors = new List<ConnectSensor> { new ConnectSensor("5eb6aaff-02de-40e9-a5e6-547f4b456360", sensortype1.Value, sensorunit1.Value), new ConnectSensor("9e511c82-57a6-4608-ab39-b0226d2112ea", sensortype2.Value, sensorunit2.Value), new ConnectSensor("5a15b803-cbc5-4ae6-a0e6-5ca4935469cc", sensortype3.Value, sensorunit3.Value) }; SetSensorAttributes(); //set min/max ranges min = new int[] { Convert.ToInt32(minrange1.Value), Convert.ToInt32(minrange2.Value), Convert.ToInt32(minrange3.Value) }; max = new int[] { Convert.ToInt32(maxrange1.Value), Convert.ToInt32(maxrange2.Value), Convert.ToInt32(maxrange3.Value) }; //initialise random int generator rand = new Random(); //set Timer using the user-defined value double seconds = Convert.ToDouble(datafreq.Value) * 1000; Global.EventHubTimer = new System.Timers.Timer(); SetTimer(Global.EventHubTimer, seconds); } else if (SendButton.Text.StartsWith("Stop")) { SendButton.Text = "Start Event Hub"; try { Global.EventHubTimer.Enabled = false; Global.EventHubTimer.Close(); } catch (NullReferenceException nullRef) { System.Diagnostics.Debug.WriteLine(nullRef.Message); } } }
/// <summary> /// Constructor /// </summary> /// <param name="client">Event Hub Client</param> public HubClient(EventHubClient client) { if (null == client) { throw new ArgumentNullException("client"); } this.client = client; }
public EventSender( MessagingFactory messagingFactory, SimulatorConfiguration config, Func<object, byte[]> serializer) { this._serializer = serializer; this._eventHubClient = messagingFactory.CreateEventHubClient(config.EventHubPath); }
static void Main(string[] args) { string eventHubConnectionString = ConfigurationManager.AppSettings["eventHubReceiveRuleConnectionString"]; string eventHubName = ConfigurationManager.AppSettings["eventHubName"]; _client = EventHubClient.CreateFromConnectionString(eventHubConnectionString, eventHubName); MonitorEventHub().Wait(); }
// Please set the following connection strings in app.config for this WebJob to run: // AzureWebJobsDashboard and AzureWebJobsStorage static void Main() { _host = new JobHost(); var connectionString = ConfigurationManager.ConnectionStrings["IoTHub"].ConnectionString; _eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, "messages/events"); _serviceClient = ServiceClient.CreateFromConnectionString(connectionString); Task.WaitAll(_eventHubClient.GetRuntimeInformation().PartitionIds.Select(Listen).ToArray()); _host.RunAndBlock(); }
/// <summary> /// Construct a sink that saves log events to the specified EventHubClient. /// </summary> /// <param name="eventHubClient">The EventHubClient to use in this sink.</param> /// <param name="partitionKey">PartitionKey to group events by within the Event Hub.</param> /// <param name="formatter">Provides formatting for outputting log data</param> public AzureEventHubSink( EventHubClient eventHubClient, string partitionKey, ITextFormatter formatter) { _eventHubClient = eventHubClient; _partitionKey = partitionKey; _formatter = formatter; }
/// <summary> /// Add an existing client for sending messages to an event hub. Infer the eventHub name from client.path /// </summary> /// <param name="client"></param> public void AddEventHubClient(EventHubClient client) { if (client == null) { throw new ArgumentNullException("client"); } string eventHubName = client.Path; AddEventHubClient(eventHubName, client); }
/// <summary> /// Creates the event sender action block. /// </summary> /// <param name="hubClient">The hub client.</param> /// <param name="options">The options.</param> /// <returns>ActionBlock<System.Byte[]>.</returns> public static ActionBlock<byte[]> Create(EventHubClient hubClient, ExecutionDataflowBlockOptions options) { return new ActionBlock<byte[]>(data => { //var index = Array.IndexOf(data, (byte)'>'); //if (index < 0) return Task.FromResult(0); //var key = Encoding.UTF8.GetString(data, 0, index); var eventData = new EventData(data); return hubClient.SendAsync(eventData); }, options); }
public MessageSender( MessagingFactory messagingFactory, SimulatorConfiguration config, Func<object, byte[]> serializer, ISenderInstrumentationPublisher telemetryPublisher) { this._serializer = serializer; this._instrumentationTelemetryPublisher = telemetryPublisher; this._eventHubClient = messagingFactory.CreateEventHubClient(config.EventHubPath); }
protected void Application_Start( object sender, EventArgs e ) { ThreadPool.QueueUserWorkItem( _ => { eventHubClient = EventHubClient.CreateFromConnectionString( connectionString, iotHubD2cEndpoint ); var d2cPartitions = eventHubClient.GetRuntimeInformation( ).PartitionIds; foreach ( string partition in d2cPartitions ) ReceiveMessagesFromDeviceAsync( partition ); } ); }
public async void ProcessEvents() { eventHubName = "azureguidanceevnthub"; connectionString = GetServiceBusConnectionString(); NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString); eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName); defaultConsumerGroup = eventHubClient.GetDefaultConsumerGroup(); string blobConnectionString = ConfigurationManager.AppSettings["AzureStorageConnectionString"]; // Required for checkpoint/state eventProcessorHost = new EventProcessorHost("AzureGuidanceReceiver", eventHubClient.Path, defaultConsumerGroup.GroupName, connectionString, blobConnectionString); await eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>(); }
/// <summary> /// Add an existing client for sending messages to an event hub. Infer the eventHub name from client.path /// </summary> /// <param name="eventHubName">name of the event hub</param> /// <param name="client"></param> public void AddEventHubClient(string eventHubName, EventHubClient client) { if (eventHubName == null) { throw new ArgumentNullException("eventHubName"); } if (client == null) { throw new ArgumentNullException("client"); } _senders[eventHubName] = client; }
/// <summary> /// Create wrapper /// </summary> /// <param name="client"></param> public EventHubClientWrapper(EventHubClient client) { _client = client; }
public EventHubMessageBus( EventHubClient eventHubClient, IMessageSerializer messageSerializer) : this(eventHubClient, new EventDataSerializer(messageSerializer)) { }
static void Main(string[] args) { Trace.Listeners.Add(new TextWriterTraceListener("console.log")); Trace.AutoFlush = true; instanceTime = DateTime.Now; conn = new SqlConnection("Server=tcp:juleeasia.database.windows.net,1433;Initial Catalog=iotdb;Persist Security Info=False;User ID=julee;Password=Passw0rd;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"); conn.Open(); dvcSDKver = ".Net 1.0.2"; svcSDKver = ".Net 1.0.3"; //System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\julee\iothubperf.txt", true); Console.WriteLine("Start to send Cloud-to-Device message\n"); serviceClient = ServiceClient.CreateFromConnectionString(serviceConnectionString); //you have to enable flag when sending message //ReceiveFeedbackAsync(); //operations monitoring //operHubClient = EventHubClient.CreateFromConnectionString(operConnectionString, operEndpoint); //var operPartitions = operHubClient.GetRuntimeInformation().PartitionIds; //foreach (string partition in operPartitions) //{ // ReceiveMessagesFromMonitorAsync(partition); //} Console.Write("Enter the device ID:"); deviceId = Console.ReadLine(); //string deviceId = "demo"; Console.Write("Enter the device SDK version:"); dvcSDKver = Console.ReadLine(); Console.Write("Enter a description:"); desc = Console.ReadLine(); //while (true) //{ //receive용 eventHubClient = EventHubClient.CreateFromConnectionString(eventhubconnectionString, iotHubD2cEndpoint); var d2cPartitions = eventHubClient.GetRuntimeInformation().PartitionIds; foreach (string partition in d2cPartitions) { ReceiveMessagesFromDeviceAsync(partition); } try { SendCloudToDeviceMessageAsync().Wait(); } catch (Exception e) { } // while (!commandProcessed) // { // if (commandReceived != "") // { // } // commandReceived = ""; // } // //no command received yet // //Console.WriteLine("Command Not Received"); // Thread.Sleep(100); // //if timeout occurred // } //} Console.WriteLine("Program Running. Pless anykey to terminate!"); Console.ReadLine(); conn.Close(); }
void IDisposable.Dispose() { Debug.Assert(EventsToPublish.Any(), $"There are undelivered events on {PublisherName}"); eventHubClient?.Close(); eventHubClient = null; }
public LogQueue(int capLimit, int timeLimit, EventHubClient eventHubClient) { Init(capLimit, timeLimit); _eventHubClient = eventHubClient; }
internal static Task SendToPartitionAsync(EventHubClient ehClient, string partitionId, string messageBody, int numberOfMessages = 1) { return(SendToPartitionAsync(ehClient, partitionId, new EventData(Encoding.UTF8.GetBytes(messageBody)), numberOfMessages)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services .AddControllers() .AddNewtonsoftJson(); services.AddInfrastructure(); services.AddResponseCompression(opts => { opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat( new[] { "application/octet-stream" }); }); services.AddDbContext <AccessControlContext> ((sp, options) => { options.UseSqlServer( sp.GetRequiredService <IConfiguration>().GetConnectionString("appservice-db")); options.EnableSensitiveDataLogging(); }) .AddIdentity <User, IdentityRole>() .AddEntityFrameworkStores <AccessControlContext>() .AddDefaultTokenProviders(); services.AddCors(options => { options.AddPolicy("AllowAnyOrigin", builder => builder .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader()); }); services.AddAuthorization(options => { options.AddPolicy(JwtBearerDefaults.AuthenticationScheme, policy => { policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme); policy.RequireClaim(ClaimTypes.NameIdentifier); }); options.AddPolicy("AdministratorsOnly", policy => policy.RequireClaim("Administrator")); }); services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = Configuration["Jwt:Issuer"], ValidAudience = Configuration["Jwt:Audience"], IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"])) }; options.Events = new JwtBearerEvents { OnMessageReceived = context => { var accessToken = context.Request.Query["access_token"]; if (!string.IsNullOrEmpty(accessToken) && (context.HttpContext.WebSockets.IsWebSocketRequest || context.Request.Headers["Accept"] == "text/event-stream")) { context.Token = context.Request.Query["access_token"]; } return(Task.CompletedTask); } }; }); services.AddOpenApiDocument(); services.AddSingleton(sp => NotificationHubClient.CreateClientFromConnectionString(Configuration["Notifications:ConnectionString"], Configuration["Notifications:Path"])); services.AddSingleton(sp => ServiceClient.CreateFromConnectionString(Configuration["Hub:ConnectionString"])); services.AddSingleton(sp => EventHubClient.CreateFromConnectionString(Configuration["Events:ConnectionString"])); services.AddSingleton <DeviceController>(); services.AddHostedService <AlarmService>(); services.AddTransient <IJwtTokenService, JwtTokenService>(); services.AddSingleton <IAccessLogNotifier, AccessLogNotifier>(); services.AddSingleton <IAccessLogger, AccessLogger>(); services.AddSignalR(); services.AddMediatR(typeof(AuthCommand)); //services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidatorBehavior<,>)); //services.AddValidatorsFromAssemblyContaining<LoginCommandValidator>(); }
protected async Task SendWithEventDataBatch( string partitionKey = null, int maxPayloadSize = 1024, int minimumNumberOfMessagesToSend = 1000) { await using (var scope = await EventHubScope.CreateAsync(1)) { var connectionString = TestUtility.BuildEventHubsConnectionString(scope.EventHubName); var ehClient = EventHubClient.CreateFromConnectionString(connectionString); var partitions = await this.GetPartitionsAsync(ehClient); var receivers = new List <PartitionReceiver>(); // Create partition receivers starting from the end of the stream. TestUtility.Log("Discovering end of stream on each partition."); foreach (var partitionId in partitions) { var lastEvent = await ehClient.GetPartitionRuntimeInformationAsync(partitionId); receivers.Add(ehClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partitionId, EventPosition.FromOffset(lastEvent.LastEnqueuedOffset))); } try { // Start receicing messages now. var receiverTasks = new List <Task <List <EventData> > >(); foreach (var receiver in receivers) { receiverTasks.Add(ReceiveAllMessagesAsync(receiver)); } // Create initial batcher. EventDataBatch batcher = null; // We will send a thousand messages where each message is 1K. var totalSent = 0; var rnd = new Random(); TestUtility.Log("Starting to send."); do { if (batcher == null) { // Exercise both CreateBatch overloads. if (partitionKey != null) { batcher = ehClient.CreateBatch(new BatchOptions() { PartitionKey = partitionKey }); } else { batcher = ehClient.CreateBatch(); } } // Send random body size. var ed = new EventData(new byte[rnd.Next(0, maxPayloadSize)]); if (!batcher.TryAdd(ed) || totalSent + batcher.Count >= minimumNumberOfMessagesToSend) { await ehClient.SendAsync(batcher); totalSent += batcher.Count; TestUtility.Log($"Sent {batcher.Count} messages in the batch."); batcher = null; } } while (totalSent < minimumNumberOfMessagesToSend); TestUtility.Log($"{totalSent} messages sent in total."); var pReceived = await Task.WhenAll(receiverTasks); var totalReceived = pReceived.Sum(p => p.Count); TestUtility.Log($"{totalReceived} messages received in total."); // Sent at least a message? Assert.True(totalSent > 0, $"Client was not able to send any messages."); // All messages received? Assert.True(totalReceived == totalSent, $"Sent {totalSent}, but received {totalReceived} messages."); if (partitionKey != null) { // Partition key is set then we expect all messages from the same partition. Assert.True(pReceived.Count(p => p.Count > 0) == 1, "Received messsages from multiple partitions."); // Find target partition. var targetPartition = pReceived.Single(p => p.Count > 0); // Validate partition key is delivered on all messages. Assert.True(!targetPartition.Any(p => p.SystemProperties.PartitionKey != partitionKey), "Identified at least one event with a different partition key value."); } } finally { await Task.WhenAll(receivers.Select(r => r.CloseAsync())); await ehClient.CloseAsync(); } } }
public async Task RunTest() { Console.WriteLine("EventHubOrderTest"); string pk = Guid.NewGuid().ToString(); var senderCxn = new EventHubsConnectionStringBuilder(targetNamespaceConnectionString) { EntityPath = targetEventHub }; var sendSideClient = EventHubClient.CreateFromConnectionString(senderCxn.ToString()); var receiverCxn = new EventHubsConnectionStringBuilder(sourceNamespaceConnectionString) { EntityPath = sourceEventHub }; var receiveSideClient = EventHubClient.CreateFromConnectionString(receiverCxn.ToString()); var senderInfo = await sendSideClient.GetRuntimeInformationAsync(); var receiverInfo = await receiveSideClient.GetRuntimeInformationAsync(); Assert.Equal(senderInfo.PartitionCount, receiverInfo.PartitionCount); var start = DateTime.UtcNow; var sw = new Stopwatch(); sw.Start(); var tracker = new List <Tuple <string, long> >(); int messageCount = 5000; int sizeInBytes = 128; var data = new byte[sizeInBytes]; Array.Fill <byte>(data, 0xff); int sent = 0; Console.WriteLine($"sending {messageCount} messages of {sizeInBytes} bytes ..."); for (int j = 0; j < messageCount; j++) { string msgid = Guid.NewGuid().ToString(); tracker.Add(new Tuple <string, long>(msgid, sw.ElapsedTicks)); var eventData = new EventData(data); eventData.Properties["message-id"] = msgid; // we need to send those all one-by-one to preserve order during sends await sendSideClient.SendAsync(eventData, pk).ContinueWith(t => { int s = Interlocked.Increment(ref sent); if (s % 1000 == 0) { Console.WriteLine($"sent {s} messages ..."); } }); } List <Task> receiveTasks = new List <Task>(); ConcurrentBag <long> durations = new ConcurrentBag <long>(); Console.Write("receiving: "); foreach (var partitionId in receiverInfo.PartitionIds) { receiveTasks.Add(Task.Run(async() => { int received = 0; var receiver = receiveSideClient.CreateReceiver(this.sourceConsumerGroup, partitionId, EventPosition.FromEnqueuedTime(start)); Console.WriteLine($"Partition {partitionId} starting "); while (tracker.Count > 0) { var eventData = await receiver.ReceiveAsync(100, TimeSpan.FromSeconds(30)); if (eventData != null) { foreach (var ev in eventData) { if (ev.SystemProperties.PartitionKey != pk) { continue; } string msgid = ev.Properties["message-id"] as string; Assert.Equal(tracker[0].Item1, msgid); durations.Add(sw.ElapsedTicks - tracker[0].Item2); tracker.RemoveAt(0); int s = Interlocked.Increment(ref received); if (s % 5000 == 0) { Console.WriteLine($"Partition {partitionId} received {s} messages ..."); } } } else { Console.WriteLine($"Partition {partitionId} empty."); break; } } Console.WriteLine($"Partition {partitionId} received {received} messages. Done."); })); } await Task.WhenAll(receiveTasks); Console.WriteLine(); Assert.Empty(tracker); Console.WriteLine($"Duration {((double)durations.Sum() / (double)durations.Count) / TimeSpan.TicksPerMillisecond}"); }
public CoffeeMachineDataSender(string eventHubConnectionString) { _eventHubClient = EventHubClient.CreateFromConnectionString(eventHubConnectionString); }
public async void Disconnect(EventHubClient eventHubClient) { await eventHubClient.CloseAsync(); }
public EventHubService(ILoggerFactory loggerFactory, EventHubSettings eventHubSettings) { this.logger = loggerFactory.CreateLogger(nameof(EventHubService)); this.eventHubSettings = eventHubSettings; this.eventHubClient = InitializeEventHub(); }
public async Task ReceiveHandlerReregister() { await using (var scope = await EventHubScope.CreateAsync(1)) { var connectionString = TestUtility.BuildEventHubsConnectionString(scope.EventHubName); var totalNumberOfMessagesToSend = 100; var partitionId = "0"; var ehClient = EventHubClient.CreateFromConnectionString(connectionString); var pInfo = await ehClient.GetPartitionRuntimeInformationAsync(partitionId); var partitionReceiver = ehClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partitionId, EventPosition.FromOffset(pInfo.LastEnqueuedOffset)); await TestUtility.SendToPartitionAsync(ehClient, partitionId, $"{partitionId} event.", totalNumberOfMessagesToSend); try { var handler = new TestPartitionReceiveHandler(); handler.MaxBatchSize = 1; // Not expecting any errors. handler.ErrorReceived += (s, e) => { // SetReceiveHandler will ignore any exception thrown so log here for output. TestUtility.Log($"TestPartitionReceiveHandler.ProcessError {e.GetType().Name}: {e.Message}"); throw new Exception($"TestPartitionReceiveHandler.ProcessError {e.GetType().Name}: {e.Message}"); }; int totalnumberOfMessagesReceived = 0; handler.EventsReceived += (s, eventDatas) => { int count = eventDatas != null?eventDatas.Count() : 0; Interlocked.Add(ref totalnumberOfMessagesReceived, count); TestUtility.Log($"Received {count} event(s)."); }; TestUtility.Log("Registering"); partitionReceiver.SetReceiveHandler(handler); TestUtility.Log("Unregistering"); partitionReceiver.SetReceiveHandler(null); TestUtility.Log("Registering"); partitionReceiver.SetReceiveHandler(handler); await Task.Delay(3000); // Second register call will trigger error handler but throw from handler should be ignored // so below register call should not fail. TestUtility.Log("Registering when already registered"); partitionReceiver.SetReceiveHandler(handler); TestUtility.Log("All register calls done."); // Send another set of messages. // Since handler is still registered we should be able to receive these messages just fine. await TestUtility.SendToPartitionAsync(ehClient, partitionId, $"{partitionId} event.", totalNumberOfMessagesToSend); // Allow 1 minute to receive all messages. await Task.Delay(TimeSpan.FromSeconds(60)); TestUtility.Log($"Received {totalnumberOfMessagesReceived}."); Assert.True(totalnumberOfMessagesReceived == totalNumberOfMessagesToSend * 2, $"Did not receive {totalNumberOfMessagesToSend * 2} messages, received {totalnumberOfMessagesReceived}."); } finally { // Unregister handler. partitionReceiver.SetReceiveHandler(null); // Close clients. await Task.WhenAll( partitionReceiver.CloseAsync(), ehClient.CloseAsync()); } } }
public async Task SendReceiveBasic() { await using (var scope = await EventHubScope.CreateAsync(2)) { var connectionString = TestUtility.BuildEventHubsConnectionString(scope.EventHubName); TestUtility.Log("Receiving Events via PartitionReceiver.SetReceiveHandler()"); var partitionId = "1"; var ehClient = EventHubClient.CreateFromConnectionString(connectionString); var partitionReceiver = ehClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partitionId, EventPosition.FromEnqueuedTime(DateTime.UtcNow.AddMinutes(-10))); var partitionSender = ehClient.CreatePartitionSender(partitionId); try { string uniqueEventId = Guid.NewGuid().ToString(); TestUtility.Log($"Sending an event to Partition {partitionId} with custom property EventId {uniqueEventId}"); var sendEvent = new EventData(Encoding.UTF8.GetBytes("Hello EventHub!")); sendEvent.Properties["EventId"] = uniqueEventId; await partitionSender.SendAsync(sendEvent); EventWaitHandle dataReceivedEvent = new EventWaitHandle(false, EventResetMode.ManualReset); var handler = new TestPartitionReceiveHandler(); // Not expecting any errors. handler.ErrorReceived += (s, e) => { throw new Exception($"TestPartitionReceiveHandler.ProcessError {e.GetType().Name}: {e.Message}"); }; handler.EventsReceived += (s, eventDatas) => { int count = eventDatas != null?eventDatas.Count() : 0; TestUtility.Log($"Received {count} event(s):"); if (eventDatas != null) { foreach (var eventData in eventDatas) { object objectValue; if (eventData.Properties != null && eventData.Properties.TryGetValue("EventId", out objectValue)) { TestUtility.Log($"Received message with EventId {objectValue}"); string receivedId = objectValue.ToString(); if (receivedId == uniqueEventId) { TestUtility.Log("Success"); dataReceivedEvent.Set(); break; } } } } }; partitionReceiver.SetReceiveHandler(handler); await Task.Delay(TimeSpan.FromSeconds(60)); if (!dataReceivedEvent.WaitOne(TimeSpan.FromSeconds(20))) { throw new InvalidOperationException("Data Received Event was not signaled."); } } finally { // Unregister handler. partitionReceiver.SetReceiveHandler(null); // Close clients. await Task.WhenAll( partitionSender.CloseAsync(), partitionReceiver.CloseAsync(), ehClient.CloseAsync()); } } }
public static IContainer RegisterDependencies(IServiceCollection services, IHostingEnvironment env, IConfiguration rootConfiguration) { var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var builder = new ContainerBuilder(); builder.Populate(services); RegisterCredentials(builder); if (env.IsDevelopment()) { OverrideWithLocalCredentials(builder); } builder.Register <IDiscoveryCache>(b => { var configuration = b.Resolve <IConfiguration>(); var cache = new DiscoveryCache(configuration["Auth:Client:Url"]); return(cache); }).SingleInstance(); builder.Register(b => { return(rootConfiguration); }).SingleInstance(); builder.Register(b => { var redis = b.Resolve <ConnectionMultiplexer>(); return(redis.GetDatabase()); }).InstancePerLifetimeScope(); builder.Register(b => { var configuration = b.Resolve <IConfiguration>(); var redis = b.Resolve <ConnectionMultiplexer>(); return(redis.GetServer(configuration["Redis:Url"])); }).InstancePerLifetimeScope(); builder.RegisterType <IEventDispatcher>().AsImplementedInterfaces().InstancePerLifetimeScope(); builder.RegisterAssemblyTypes(assemblies).AsClosedTypesOf(typeof(IEventHandler <>)).InstancePerDependency(); builder.RegisterType <CommandDispatcher>().As <ICommandDispatcher>().InstancePerLifetimeScope(); builder.RegisterAssemblyTypes(assemblies).AsClosedTypesOf(typeof(ICommandHandler <>)).InstancePerLifetimeScope(); builder.RegisterAssemblyTypes(assemblies).AsClosedTypesOf(typeof(ICommandHandler <,>)).InstancePerLifetimeScope(); builder.RegisterType <QueryDispatcher>().As <IQueryDispatcher>().InstancePerRequest().InstancePerLifetimeScope(); builder.RegisterAssemblyTypes(assemblies).AsClosedTypesOf(typeof(IQueryHandler <,>)).InstancePerLifetimeScope(); builder.RegisterAssemblyTypes(assemblies).AsClosedTypesOf(typeof(AppAbstractValidation <>)).InstancePerLifetimeScope(); builder.RegisterAssemblyTypes(assemblies).AsClosedTypesOf(typeof(IReadCacheHandler <,>)).InstancePerLifetimeScope(); builder.RegisterAssemblyTypes(assemblies).AsClosedTypesOf(typeof(IWriteCacheHandler <,>)).InstancePerLifetimeScope(); builder.Register <ILogger>(b => { var format = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {NewLine}{Message:lj}{NewLine}{Exception}"; return(new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.Console(outputTemplate: format) .WriteTo.File("Logs/log.txt", rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Error, outputTemplate: format) .WriteTo.Logger(cl => cl.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Information).WriteTo.File("Logs/queries.txt", rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Information, outputTemplate: format)) .CreateLogger()); }).SingleInstance(); builder.Register <IClock>(b => { return(SystemClock.Instance); }).SingleInstance(); builder.Register <IMemoryCache>(b => { return(new MemoryCache(new MemoryCacheOptions())); }).SingleInstance(); builder.RegisterType <GuidService>().AsImplementedInterfaces().InstancePerLifetimeScope(); builder.RegisterType <TelegramService>().AsImplementedInterfaces().InstancePerLifetimeScope(); builder.RegisterType <IdentityModelClient>().AsImplementedInterfaces().InstancePerLifetimeScope(); builder.RegisterType <CurrentUserProvider>().AsImplementedInterfaces().InstancePerLifetimeScope(); builder.RegisterType <RandomService>().AsImplementedInterfaces().SingleInstance(); builder.RegisterType <EventHubService>().AsImplementedInterfaces().SingleInstance(); builder.Register <IClock>(b => { return(SystemClock.Instance); }).SingleInstance(); builder.Register(b => { var connectionStringBuilder = b.Resolve <EventHubsConnectionStringBuilder>(); return(EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString())); }).InstancePerLifetimeScope(); builder.Register <IDatabaseConnector <TelegramDatabaseCredentials> >(b => { var databaseCredentials = b.Resolve <TelegramDatabaseCredentials>(); return(new TelegramStoreDapperConnector <TelegramDatabaseCredentials>(databaseCredentials)); }).InstancePerLifetimeScope(); builder.Register <IDatabaseConnector <HangfireDatabaseCredentials> >(b => { var databaseCredentials = b.Resolve <HangfireDatabaseCredentials>(); return(new DapperConnector <HangfireDatabaseCredentials>(new SqlConnection(databaseCredentials.ConnectionString))); }).InstancePerLifetimeScope(); return(builder.Build()); }
/// <summary> /// Runs the sample using the specified Event Hubs connection information. /// </summary> /// /// <param name="connectionString">The connection string for the Event Hubs namespace that the sample should target.</param> /// <param name="eventHubName">The name of the Event Hub, sometimes known as its path, that she sample should run against.</param> /// public async Task RunAsync(string connectionString, string eventHubName) { // We will start by creating a client using its default set of options. It will be used by the event processor to // communicate with the Azure Event Hubs service. await using (var client = new EventHubClient(connectionString, eventHubName)) { // An event processor is associated with a specific Event Hub and a consumer group. It receives events from // all partitions in the Event Hub, passing them to the user for processing. It's worth mentioning that an // event processor is a generic class, and it takes a partition processor as its underlying type. // // A partition processor is associated with a specific partition and is responsible for processing events when // requested by the event processor. In order to use it as the event processor's underlying type, two conditions // must be met: // // - It must be a class derived from BasePartitionProcessor. // // - It must have a parameterless constructor. // // We'll be using a SamplePartitionProcessor, whose implementation can be found at the end of this sample. // A partition manager may create checkpoints and list/claim partition ownership. The user can implement their // own partition manager by creating a subclass from the PartitionManager abstract class. Here we are creating // a new instance of an InMemoryPartitionManager, provided by the Azure.Messaging.EventHubs.Processor namespace. // This isn't relevant to understanding this sample, but is required by the event processor constructor. PartitionManager partitionManager = new InMemoryPartitionManager(); // It's also possible to specify custom options upon event processor creation. We want to receive events from // the latest available position so older events don't interfere with our sample. We also don't want to wait // more than 1 second for every set of events. EventProcessorOptions eventProcessorOptions = new EventProcessorOptions { InitialEventPosition = EventPosition.Latest, MaximumReceiveWaitTime = TimeSpan.FromSeconds(1) }; // Let's finally create our event processor. We're using the default consumer group that was created with the Event Hub. var eventProcessor = new EventProcessor <SamplePartitionProcessor>(EventHubConsumer.DefaultConsumerGroupName, client, partitionManager, eventProcessorOptions); // Once started, the event processor will start to receive events from all partitions. Console.WriteLine("Starting the event processor."); Console.WriteLine(); await eventProcessor.StartAsync(); Console.WriteLine(); Console.WriteLine("Event processor started."); // To test our event processor, we are publishing 10 sets of events to the Event Hub. Notice that we are not // specifying a partition to send events to, so these sets may end up in different partitions. EventData[] eventsToPublish = new EventData[] { new EventData(Encoding.UTF8.GetBytes("I am not the second event.")), new EventData(Encoding.UTF8.GetBytes("I am not the first event.")) }; int amountOfSets = 10; int expectedAmountOfEvents = amountOfSets * eventsToPublish.Length; await using (EventHubProducer producer = client.CreateProducer()) { Console.WriteLine(); Console.WriteLine("Sending events to the Event Hub."); Console.WriteLine(); for (int i = 0; i < amountOfSets; i++) { await producer.SendAsync(eventsToPublish); } } // Because there is some non-determinism in the messaging flow, the sent events may not be immediately // available. For this reason, we wait 500 ms before resuming. await Task.Delay(500); // Once stopped, the event processor won't receive events anymore. In case there are still events being // processed when the stop method is called, the processing will complete before the corresponding partition // processor is closed. Console.WriteLine(); Console.WriteLine("Stopping the event processor."); Console.WriteLine(); await eventProcessor.StopAsync(); // Print out the amount of events that we received. Console.WriteLine(); Console.WriteLine($"Amount of events received: { SamplePartitionProcessor.TotalEventsCount }. Expected: { expectedAmountOfEvents }."); } // At this point, our client and producer have passed their "using" scope and have safely been disposed of. We // have no further obligations. Console.WriteLine(); }
internal static async Task <Dictionary <string, string> > DiscoverEndOfStreamForPartitionsAsync(EventHubClient ehClient, string[] partitionIds) { // Mark offsets in all partitions so we can attempt to receive from that point. var partitionOffsets = new Dictionary <string, string>(); // Discover the end of stream on each partition. TestUtility.Log("Discovering end of stream on each partition."); foreach (var partitionId in partitionIds) { var lastEvent = await ehClient.GetPartitionRuntimeInformationAsync(partitionId); partitionOffsets.Add(partitionId, lastEvent.LastEnqueuedOffset); TestUtility.Log($"Partition {partitionId} has last message with offset {lastEvent.LastEnqueuedOffset}"); } return(partitionOffsets); }
public async Task ProducerSendsEventsInTheSameSetToTheSamePartition() { var partitions = 10; await using (EventHubScope scope = await EventHubScope.CreateAsync(partitions)) { var connectionString = TestEnvironment.BuildConnectionStringForEventHub(scope.EventHubName); await using (var client = new EventHubClient(connectionString)) await using (EventHubProducer producer = client.CreateProducer()) { var eventBatch = Enumerable .Range(0, 30) .Select(index => new EventData(Encoding.UTF8.GetBytes("I'm getting used to this amount of messages"))) .ToList(); var partitionIds = await client.GetPartitionIdsAsync(); var partitionsCount = 0; var receivedEventsCount = 0; var consumers = new List <EventHubConsumer>(); try { for (var index = 0; index < partitions; index++) { consumers.Add(client.CreateConsumer(EventHubConsumer.DefaultConsumerGroupName, partitionIds[index], EventPosition.Latest)); // Initiate an operation to force the consumer to connect and set its position at the // end of the event stream. await consumers[index].ReceiveAsync(1, TimeSpan.Zero); } // Send the batch of events. await producer.SendAsync(eventBatch); // Receive the events; because there is some non-determinism in the messaging flow, the // sent events may not be immediately available. Allow for a small number of attempts to receive, in order // to account for availability delays. foreach (EventHubConsumer consumer in consumers) { var receivedEvents = new List <EventData>(); var index = 0; while (++index < ReceiveRetryLimit) { receivedEvents.AddRange(await consumer.ReceiveAsync(eventBatch.Count + 10, TimeSpan.FromMilliseconds(25))); } if (receivedEvents.Count > 0) { partitionsCount++; receivedEventsCount += receivedEvents.Count; } } } finally { foreach (EventHubConsumer consumer in consumers) { consumer.Close(); } } Assert.That(partitionsCount, Is.EqualTo(1)); Assert.That(receivedEventsCount, Is.EqualTo(eventBatch.Count)); } } }
internal static async Task <Tuple <string, DateTime, string> > DiscoverEndOfStreamForPartitionAsync(EventHubClient ehClient, string partitionId) { TestUtility.Log($"Getting partition information for {partitionId}."); var pInfo = await ehClient.GetPartitionRuntimeInformationAsync(partitionId); return(Tuple.Create(pInfo.LastEnqueuedOffset, pInfo.LastEnqueuedTimeUtc, pInfo.LastEnqueuedOffset)); }
static void Main(string[] args) { string ehConnectionString = "Endpoint=sb://[your namespace].servicebus.windows.net;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=[your secret]"; //Create the connection string ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(ehConnectionString) { TransportType = TransportType.Amqp }; //Create the EH sender string eventHubName = "[EVENTHUBNAME]"; //OPTIONS 1 eventHubClient = EventHubClient.CreateFromConnectionString(builder.ToString(), eventHubName); //for (int i = 0; i < 5; i++) //{ // EventData eventData = new EventData(Encoding.UTF8.GetBytes("test")); // eventHubClient.SendAsync(eventData); //} //Check command params if (args.Length == 0) { Console.WriteLine("Combinations coud be [SendMultiplePartitionMessage] + [ Num of Messages to send] + [sleep time] + [partitionkey] + [Groupname]"); Environment.Exit(0); } messagesToSend = int.Parse(args[1]); Console.WriteLine("Create / Open EH {0}...", eventHubName); Console.WriteLine("Ready to send {0} messages... press button to start...", messagesToSend); switch (args[0]) { case "SendMessageLoopSync": //Send message using multiple round robin partition pattern SendMessageLoopSync(); break; case "SendMessageLoopASync": //Send message using multiple round robin partition pattern SendMessageLoopASync(); break; case "SendMessageLoopASyncsimpleMessage": //Send message using multiple round robin partition pattern SendMessageLoopASyncsimpleMessage(); break; case "SendMessagesThread": //Send message using multiple round robin partition pattern SendMessagesThread(messagesToSend); break; case "SendHighBatchMessagesThread": //Send message using multiple round robin partition pattern SendHighBatchMessagesThread(messagesToSend); break; default: break; } Console.ReadLine(); }
public async Task ProducerSendsEventsWithTheSamePartitionHashKeyToTheSamePartition() { var partitions = 10; var partitionKey = "some123key-!d"; await using (EventHubScope scope = await EventHubScope.CreateAsync(partitions)) { var connectionString = TestEnvironment.BuildConnectionStringForEventHub(scope.EventHubName); await using (var client = new EventHubClient(connectionString)) await using (EventHubProducer producer = client.CreateProducer()) { var batches = 5; var partitionIds = await client.GetPartitionIdsAsync(); var partitionsCount = 0; var receivedEventsCount = 0; var consumers = new List <EventHubConsumer>(); try { for (var index = 0; index < partitions; index++) { consumers.Add(client.CreateConsumer(EventHubConsumer.DefaultConsumerGroupName, partitionIds[index], EventPosition.Latest)); // Initiate an operation to force the consumer to connect and set its position at the // end of the event stream. await consumers[index].ReceiveAsync(1, TimeSpan.Zero); } // Send the batches of events. var batchOptions = new SendOptions { PartitionKey = partitionKey }; for (var index = 0; index < batches; index++) { await producer.SendAsync(new EventData(Encoding.UTF8.GetBytes($"Just a few messages ({ index })")), batchOptions); } // Receive the events; because there is some non-determinism in the messaging flow, the // sent events may not be immediately available. Allow for a small number of attempts to receive, in order // to account for availability delays. foreach (EventHubConsumer consumer in consumers) { var receivedEvents = new List <EventData>(); var index = 0; while (++index < ReceiveRetryLimit) { receivedEvents.AddRange(await consumer.ReceiveAsync(batches + 10, TimeSpan.FromMilliseconds(25))); } if (receivedEvents.Count > 0) { partitionsCount++; receivedEventsCount += receivedEvents.Count; foreach (EventData receivedEvent in receivedEvents) { Assert.That(receivedEvent.PartitionKey, Is.EqualTo(partitionKey)); } } } } finally { foreach (EventHubConsumer consumer in consumers) { consumer.Close(); } } Assert.That(partitionsCount, Is.EqualTo(1)); Assert.That(receivedEventsCount, Is.EqualTo(batches)); } } }
public EventSender() { _client = EventHubClient.CreateFromConnectionString(_connectionString, _hubName); }
public async Task ProducerDoesNotSendToSpecificPartitionWhenPartitionIdIsNotSpecified(bool nullPartition) { var partitions = 10; await using (EventHubScope scope = await EventHubScope.CreateAsync(partitions)) { var connectionString = TestEnvironment.BuildConnectionStringForEventHub(scope.EventHubName); await using (var client = new EventHubClient(connectionString)) { var producerOptions = new EventHubProducerOptions { }; if (nullPartition) { producerOptions.PartitionId = null; } await using (EventHubProducer producer = client.CreateProducer(producerOptions)) { var batches = 30; var partitionIds = await client.GetPartitionIdsAsync(); var partitionsCount = 0; var consumers = new List <EventHubConsumer>(); try { for (var index = 0; index < partitions; index++) { consumers.Add(client.CreateConsumer(EventHubConsumer.DefaultConsumerGroupName, partitionIds[index], EventPosition.Latest)); // Initiate an operation to force the consumer to connect and set its position at the // end of the event stream. await consumers[index].ReceiveAsync(1, TimeSpan.Zero); } // Send the batches of events. for (var index = 0; index < batches; index++) { await producer.SendAsync(new EventData(Encoding.UTF8.GetBytes("It's not healthy to send so many messages"))); } // Receive the events; because there is some non-determinism in the messaging flow, the // sent events may not be immediately available. Allow for a small number of attempts to receive, in order // to account for availability delays. foreach (EventHubConsumer consumer in consumers) { var receivedEvents = new List <EventData>(); var index = 0; while (++index < ReceiveRetryLimit) { receivedEvents.AddRange(await consumer.ReceiveAsync(batches + 10, TimeSpan.FromMilliseconds(25))); } if (receivedEvents.Count > 0) { partitionsCount++; } } } finally { await Task.WhenAll(consumers.Select(consumer => consumer.CloseAsync())); } Assert.That(partitionsCount, Is.GreaterThan(1)); } } } }
private async Task Run() { var eventHubClient = EventHubClient.CreateFromConnectionString(_eventHubClientConnectionString); var cts = new CancellationTokenSource(); var receiveMessagesFromDevicesTask = ReceiveMessagesFromDevices(eventHubClient, cts.Token); while (true) { Console.WriteLine("1 - Display all currently registered devices"); Console.WriteLine("2 - Add new device to hub"); Console.WriteLine("3 - Delete existing device from hub"); Console.WriteLine("4 - Send notification to device"); Console.WriteLine("5 - Display device twin"); Console.WriteLine("6 - Update desired property in device twin"); Console.WriteLine("X - Exit"); Console.WriteLine(); var selection = Console.ReadKey(true); switch (char.ToUpperInvariant(selection.KeyChar)) { case '1': await DisplayAllExistingDevices(); break; case '2': await AddNewDevice(); break; case '3': await DeleteExistingDevice(); break; case '4': await SendNotificationToDevice(); break; case '5': await DisplayDeviceTwin(cts.Token); break; case '6': await UpdateDesiredPropertyInDeviceTwin(cts.Token); break; case 'X': Console.WriteLine("Stopping receiving messages from devices."); cts.Cancel(); await receiveMessagesFromDevicesTask; return; default: Console.WriteLine("Choose again!"); break; } Console.WriteLine(); } }
public async Task SendEventsAsync(IReadOnlyCollection <EventData> events, long transmissionSequenceNumber, CancellationToken cancellationToken) { // Get a reference to the current connections array first, just in case there is another thread wanting to clean // up the connections with CleanUpAsync(), we won't get a null reference exception here. EventHubClient[] currentClients = Interlocked.CompareExchange <EventHubClient[]>(ref this.clients, this.clients, this.clients); if (currentClients == null || events == null || events.Count == 0) { return; } try { // Since event hub limits each message/batch to be a certain size, we need to // keep checking the size for exceeds and split into a new batch as needed List <List <MessagingEventData> > batches = new List <List <MessagingEventData> >(); int batchByteSize = 0; foreach (EventData eventData in events) { int messageSize; MessagingEventData messagingEventData = eventData.ToMessagingEventData(out messageSize); // If we don't have a batch yet, or the addition of this message will exceed the limit for this batch, then // start a new batch. if (batches.Count == 0 || batchByteSize + messageSize > EventHubMessageSizeLimit) { batches.Add(new List <MessagingEventData>()); batchByteSize = 0; } batchByteSize += messageSize; List <MessagingEventData> currentBatch = batches[batches.Count - 1]; currentBatch.Add(messagingEventData); } if (cancellationToken.IsCancellationRequested) { return; } EventHubClient hubClient = currentClients[transmissionSequenceNumber % ConcurrentConnections]; List <Task> tasks = new List <Task>(); foreach (List <MessagingEventData> batch in batches) { tasks.Add(hubClient.SendAsync(batch)); } await Task.WhenAll(tasks).ConfigureAwait(false); this.healthReporter.ReportHealthy(); } catch (Exception e) { ErrorHandlingPolicies.HandleOutputTaskError(e, () => { string errorMessage = nameof(EventHubOutput) + ": diagnostics data upload has failed." + Environment.NewLine + e.ToString(); this.healthReporter.ReportWarning(errorMessage, EventFlowContextIdentifiers.Output); }); } }
protected override async Task InternalRunAsync() { IEventHubClient eventHubClient = new EventHubClient( this.EventHubConnectionString, this.EventHubEntityPath); this.PrintObjectInfo(eventHubClient); IEventHubRuntimeInformation runtimeInformation = await eventHubClient.GetEventHubRuntimeInformationAsync() .ConfigureAwait(false); this.PrintObjectInfo(runtimeInformation); IEnumerable <IEventHubPartitionRuntimeInformation> partionInfos = runtimeInformation.PartitionIds .Select( pid => eventHubClient .GetPartitionRuntimeInformationAsync(pid) .ConfigureAwait(false).GetAwaiter().GetResult()) .ToArray(); this.PrintObjectInfo(partionInfos); int batchCount = 500; int batchSize = 100; for (int batchIdx = 0; batchIdx < batchCount; ++batchIdx) { await eventHubClient .BatchSendAsync( new int[batchSize].Select( (v, i) => $"Data {batchIdx}-{i} at {DateTime.UtcNow:o} dummy: {new string('$', 256)}")) .ConfigureAwait(false); } Console.WriteLine($"Total sent: {batchCount * batchSize}"); IEventProcessor eventProcessor = new EventProcessor( this.EventHubConsumerGroup, this.StorageAccountConnectionString, this.StorageAccountContainer) { Options = new EventProcessorOptions { MaxBatchSize = 50, PrefetchCount = 500 } }; int totalReceived = 0; Dictionary <string, List <IEventData> > bufferDic = new Dictionary <string, List <IEventData> >(); Dictionary <string, DateTimeOffset> lastDumpTimeDic = new Dictionary <string, DateTimeOffset>(); Dictionary <string, IEventHubPartitionContext> partitionContextDic = new Dictionary <string, IEventHubPartitionContext>(); CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); TaskFactory taskFactory = new TaskFactory(cancellationTokenSource.Token); List <Task> tasksList = new List <Task>(); DateTimeOffset initialTime = DateTimeOffset.FromUnixTimeSeconds( DateTimeOffset.UtcNow.ToUnixTimeSeconds()); foreach (var partitionInfo in partionInfos) { string partitionId = partitionInfo.PartitionId; bufferDic[partitionId] = new List <IEventData>(); lastDumpTimeDic[partitionId] = initialTime.AddSeconds( int.Parse(partitionId) % partionInfos.Count()); Console.WriteLine($"last dump time for partition {partitionId}: {lastDumpTimeDic[partitionId]}"); Task task = taskFactory.StartNew( async() => { while (true) { DateTimeOffset checkTime = DateTimeOffset.FromUnixTimeSeconds( DateTimeOffset.UtcNow.ToUnixTimeSeconds()); List <IEventData> buffer = bufferDic[partitionId]; if (cancellationTokenSource.Token.IsCancellationRequested) { if (buffer.Any()) { Console.WriteLine($"Dump {buffer.Count} messages from partition {partitionId} at {checkTime:o}."); totalReceived += buffer.Count(); buffer.Clear(); } break; } DateTimeOffset lastDumpTime = lastDumpTimeDic[partitionId]; double secondsSinceLastDump = checkTime.Subtract(lastDumpTime).TotalSeconds; if (buffer.Any() && secondsSinceLastDump >= partionInfos.Count() && (secondsSinceLastDump % partionInfos.Count()) == 0) { IEventData checkpointData = null; lock (buffer) { Console.WriteLine($"Dump {buffer.Count} messages from partition {partitionId} at {checkTime:o}."); totalReceived += buffer.Count(); checkpointData = buffer .OrderByDescending(e => e.SequenceNumber) .FirstOrDefault(); buffer.Clear(); } if (checkpointData != null) { IEventHubPartitionContext partitionContext = partitionContextDic[partitionId]; await partitionContext .CheckpointAsync(checkpointData).ConfigureAwait(false); } lastDumpTimeDic[partitionId] = checkTime; // Console.WriteLine($"last dump time for partition {partitionId}: {lastDumpTimeDic[partitionId]}"); } else { Thread.Sleep(TimeSpan.FromSeconds(1)); } } }); tasksList.Add(task); } Task eventProcessorHandler(object obj, EventProcessorEventArgs args) { // Console.WriteLine($"[Partition - {args.PartitionContext.PartitionId}] [{args.EventType}]"); // this.PrintObjectInfo(args); if (args.EventType == EventProcessorEventType.DataReceived) { string partitionId = args.PartitionContext.PartitionId; partitionContextDic[partitionId] = args.PartitionContext; List <IEventData> buffer = bufferDic[partitionId]; lock (buffer) { Console.WriteLine($"Buffered {args.DataCollection.Count()} messages from partition {partitionId} at {DateTimeOffset.UtcNow:o}."); buffer.AddRange(args.DataCollection); } } return(Task.CompletedTask); } eventProcessor.OnClosed += eventProcessorHandler; eventProcessor.OnOpened += eventProcessorHandler; eventProcessor.OnErrorOccurred += eventProcessorHandler; eventProcessor.OnDataReceived += eventProcessorHandler; IEventProcessorRegistration registration = await eventHubClient.RegisterAsync(eventProcessor) .ConfigureAwait(false); char c; do { Console.WriteLine("Receiving event data, press 'q' to exit."); c = Console.ReadKey(true).KeyChar; } while(c != 'q'); cancellationTokenSource.Cancel(); Task.WaitAll(tasksList.ToArray()); Console.WriteLine($"Total received: {totalReceived}"); await registration.UnregisterAsync().ConfigureAwait(false); }
public async Task EventProcessorCannotReceiveMoreThanMaximumMessageCountMessagesAtATime(int maximumMessageCount) { var partitions = 2; await using (EventHubScope scope = await EventHubScope.CreateAsync(partitions)) { var connectionString = TestEnvironment.BuildConnectionStringForEventHub(scope.EventHubName); await using (var client = new EventHubClient(connectionString)) { var unexpectedMessageCount = -1; // Send some events. await using (EventHubProducer producer = client.CreateProducer()) { var eventSet = Enumerable .Range(0, 20 * maximumMessageCount) .Select(index => new EventData(new byte[10])) .ToList(); // Send one set per partition. for (int i = 0; i < partitions; i++) { await producer.SendAsync(eventSet); } } // Create the event processor manager to manage our event processors. var eventProcessorManager = new EventProcessorManager ( EventHubConsumer.DefaultConsumerGroupName, client, options: new EventProcessorOptions { MaximumMessageCount = maximumMessageCount }, onProcessEvents: (partitionContext, events, cancellationToken) => { // Make it a list so we can safely enumerate it. var eventsList = new List <EventData>(events ?? Enumerable.Empty <EventData>()); // In case we find a message count greater than the allowed amount, we only store the first // occurrence and ignore the subsequent ones. if (eventsList.Count > maximumMessageCount) { Interlocked.CompareExchange(ref unexpectedMessageCount, eventsList.Count, -1); } } ); eventProcessorManager.AddEventProcessors(1); // Start the event processors. await eventProcessorManager.StartAllAsync(); // Make sure the event processors have enough time to stabilize and receive events. await eventProcessorManager.WaitStabilization(); // Stop the event processors. await eventProcessorManager.StopAllAsync(); // Validate results. Assert.That(unexpectedMessageCount, Is.EqualTo(-1), $"A set of { unexpectedMessageCount } events was received."); } } }
public ReadDeviceToCloudMessages() { _eventHubClient = EventHubClient.CreateFromConnectionString(AzureIoTHub.GetConnectionString(), iotHubD2cEndpoint); }
public EventHubMessageBus(EventHubClient eventHubClient) : this(eventHubClient, new EventDataSerializer()) { }
static void Main(string[] args) { string eventHubName = "devicemonitoring"; string eventHubNamespace = "eumariothub"; string sharedAccessPolicyName = "Default"; string sharedAccessPolicyKey = Constants.PoliceKey; Random MyRandom = new Random(); //Service Bus Init var settings = new MessagingFactorySettings() { TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(sharedAccessPolicyName, sharedAccessPolicyKey), TransportType = TransportType.Amqp }; var factory = MessagingFactory.Create(ServiceBusEnvironment.CreateServiceUri("sb", eventHubNamespace, ""), settings); EventHubClient client = factory.CreateEventHubClient(eventHubName); List <Task> tasks = new List <Task>(); //Création d'une variable liste pour stocker mes objets List <Device> MyDevices = new List <Device>(); List <string> Liste = new List <string>(); Liste.Add("device1"); Liste.Add("device2"); Liste.Add("device3"); Liste.Add("device4"); Liste.Add("device5"); while (!Console.KeyAvailable) { MyDevices.Clear(); MyDevices.Add(new Device(1, MyRandom.Next(0, 25), MyRandom.Next(0, 600), MyRandom.Next(0, 10), MyRandom.Next(0, 35), MyRandom.Next(0, 359), MyRandom.Next(0, 359), MyRandom.Next(0, 500), MyRandom.Next(0, 2000), MyRandom.Next(0, 2000), MyRandom.Next(-180, 180), MyRandom.Next(-90, 90), "Tunis")); MyDevices.Add(new Device(2, MyRandom.Next(0, 25), MyRandom.Next(0, 600), MyRandom.Next(0, 10), MyRandom.Next(0, 35), MyRandom.Next(0, 359), MyRandom.Next(0, 359), MyRandom.Next(0, 500), MyRandom.Next(0, 2000), MyRandom.Next(0, 2000), MyRandom.Next(-180, 180), MyRandom.Next(-90, 90), "France")); MyDevices.Add(new Device(3, MyRandom.Next(0, 25), MyRandom.Next(0, 600), MyRandom.Next(0, 10), MyRandom.Next(0, 35), MyRandom.Next(0, 359), MyRandom.Next(0, 359), MyRandom.Next(0, 500), MyRandom.Next(0, 2000), MyRandom.Next(0, 2000), MyRandom.Next(-180, 180), MyRandom.Next(-90, 90), "Algerie")); MyDevices.Add(new Device(4, MyRandom.Next(0, 25), MyRandom.Next(0, 600), MyRandom.Next(0, 10), MyRandom.Next(0, 35), MyRandom.Next(0, 359), MyRandom.Next(0, 359), MyRandom.Next(0, 500), MyRandom.Next(0, 2000), MyRandom.Next(0, 2000), MyRandom.Next(-180, 180), MyRandom.Next(-90, 90), "Sousse")); MyDevices.Add(new Device(5, MyRandom.Next(0, 25), MyRandom.Next(0, 600), MyRandom.Next(0, 10), MyRandom.Next(0, 35), MyRandom.Next(0, 359), MyRandom.Next(0, 359), MyRandom.Next(0, 500), MyRandom.Next(0, 2000), MyRandom.Next(0, 2000), MyRandom.Next(-180, 180), MyRandom.Next(-90, 90), "Allemagne")); for (int j = 0; j < 5; j++) { var Device = MyDevices[j]; Device.Time = DateTime.Now; // Serialize to JSON var serializedString = JsonConvert.SerializeObject(Device); Console.WriteLine(serializedString); EventData data = new EventData(Encoding.UTF8.GetBytes(serializedString)) { PartitionKey = Device.DeviceID.ToString() }; //client.Send(data); // Send the metric to Event Hub tasks.Add(client.SendAsync(data)); //client.SendAsync(data); } Thread.Sleep(1500); } }