Azure Event Hub 技术研究系列2-发送事件到Event Hub
上篇博文中,我们介绍了Azure Event Hub的一些基本概念和架构:
Azure Event Hub 技术研究系列1-Event Hub入门篇
本篇文章中,我们继续深入研究,了解Azure Event Hub的创建、编程SDK,实现将事件发送到云端的Azure Event Hub。
一、Azure Portal中创建Event Hub

创建一个新的Event Hub:



将连接字符串拷贝出来,备用。

二、通过Event Hub的SDK将事件发送到Event Hub
新建一个Console工程:EventHubSend
添加Nuget:
Microsoft.Azure.EventHubs

添加关键引用:
using Microsoft.Azure.EventHubs;
using System.Text;
using System.Threading.Tasks;
添加常量作为事件中心连接字符串和实体路径(单个事件中心名称)
private static EventHubClient eventHubClient;
private const string EhConnectionString = "{Event Hubs connection string}"; //第一步拷贝的连接字符串
private const string EhEntityPath = "{Event Hub path/name}"; //MyEventHub
新加MainAsync函数
private static async Task MainAsync(string[] args)
{
var connectionStringBuilder = new EventHubsConnectionStringBuilder(EhConnectionString)
{
EntityPath = EhEntityPath
}; eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString()); await SendEvents(); await eventHubClient.CloseAsync(); Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();
}
将100个事件消息发送到EventHub方法:SendEvents
/// <summary>
/// 创建100个消息事件,异步发送到EventHub
/// </summary>
/// <param name="count">个数</param>
/// <returns></returns>
private static async Task SendEvents(int count)
{
for (var i = ; i < count; i++)
{
try
{
var eventEntity = $"Event {i}";
Console.WriteLine($"Sending Event: {eventEntity}");
await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(eventEntity)));
}
catch (Exception exception)
{
Console.WriteLine($"{DateTime.Now} > Exception: {exception.Message}");
} await Task.Delay();
} Console.WriteLine($"{count} messages sent.");
}
在Main函数中添加:
static void Main(string[] args)
{
MainAsync(args).GetAwaiter().GetResult();
}
Run:

发现错误了:The messaging entity 'sb://myeventhubtest.servicebus.chinacloudapi.cn/MyEventHub' could not be found.
MyEventHub这个是我们在代码中指定的。
private const string EhEntityPath = "MyEventHub"; //MyEventHub
这个是否需要在Azure Portal中提前创建好?


再次Run:

这次可以了。
周国庆
2017/5/17
Azure Event Hub 技术研究系列2-发送事件到Event Hub的更多相关文章
- Azure Event Hub 技术研究系列3-Event Hub接收事件
上篇博文中,我们通过编程的方式介绍了如何将事件消息发送到Azure Event Hub: Azure Event Hub 技术研究系列2-发送事件到Event Hub 本篇文章中,我们继续:从Even ...
- Azure Event Bus 技术研究系列1-Event Hub入门篇
前两个系列研究了Azure IoT Hub和Azure Messaging.最近准备继续研究Azure Event Bus,即Azure的事件中心.首先, Azure Event Hub的官方介绍: ...
- Azure Event Hub 技术研究系列1-Event Hub入门篇
前两个系列研究了Azure IoT Hub和Azure Messaging.最近准备继续研究Azure Event Hub,即Azure的事件中心.首先, Azure Event Hub的官方介绍: ...
- Azure IoT 技术研究系列5-Azure IoT Hub与Event Hub比较
上篇博文中,我们介绍了Azure IoT Hub的使用配额和缩放级别: Azure IoT 技术研究系列4-Azure IoT Hub的配额及缩放级别 本文中,我们比较一下Azure IoT Hub和 ...
- Azure IoT 技术研究系列4-Azure IoT Hub的配额及缩放级别
上两篇博文中,我们介绍了将设备注册到Azure IoT Hub,设备到云.云到设备之间的通信: Azure IoT 技术研究系列2-设备注册到Azure IoT Hub Azure IoT 技术研究系 ...
- Azure IoT 技术研究系列2-起步示例之设备注册到Azure IoT Hub
上篇博文中,我们主要介绍了Azure IoT Hub的基本概念.架构.特性: Azure IoT 技术研究系列1-入门篇 本文中,我们继续深入研究,做一个起步示例程序:模拟设备注册到Azure IoT ...
- Azure IoT 技术研究系列2-设备注册到Azure IoT Hub
上篇博文中,我们主要介绍了Azure IoT Hub的基本概念.架构.特性: Azure IoT 技术研究系列1-入门篇 本文中,我们继续深入研究,做一个起步示例程序:模拟设备注册到Azure IoT ...
- Azure IoT 技术研究系列3-设备到云、云到设备通信
上篇博文中我们将模拟设备注册到Azure IoT Hub中:我们得到了设备的唯一标识. Azure IoT 技术研究系列2-设备注册到Azure IoT Hub 本文中我们继续深入研究,设备到云.云到 ...
- Azure IoT 技术研究系列3
上篇博文中我们将模拟设备注册到Azure IoT Hub中:我们得到了设备的唯一标识. Azure IoT 技术研究系列2-设备注册到Azure IoT Hub 本文中我们继续深入研究,设备到云.云到 ...
随机推荐
- phpcms2008常用函数小结
{$head[title]} 页面标题,用法: <title>{$head[title]}-{$PHPCMS[sitename]}</title> {$PHPCMS[siten ...
- [SinGuLaRiTy] 数论基础
[SinGuLaRiTy-1004] Copyright (c) SinGuLaRiTy 2017 . All Rights Reserved. 整除: 设a,b为整数,且a不为0,如果存在一个整数q ...
- H5与Android之间的交互
关于Android与JS网页端的交互,网上有很多教程,刚做这功能,参考了多方资料,最终出来后觉得简单,但是为实现的话有诸多小问题,最终效果如下: 现在简单整理一下:(直接贴代码,注释详细,应该能懂的) ...
- 可以随鼠标拖拽的div
可以拖拽的div <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...
- JavaScript基础学习(五)—其他引用类型
JavaScript定义了两个内置对象: Global和Math. 一.Global对象 1.URI编码方法 Global对象的encodeURI()和encodeURICompo ...
- AJAX载入外部JS文件到页面并让其执行的方法(附源码)
一. 向HTML页面中动态添加JS文件(从外部载入)并让其执行的两种方法 1.只适用于IE浏览器的简单方法: 先在文档中放置一张JS"空床"并添加ID:<script id= ...
- js的apply()与call()的区别
1.各自对应的不同的语法: /*apply()方法*/ function.apply(thisObj[, argArray]) /*call()方法*/ function.call(thisObj[, ...
- C++中的继承详解(3)作用域与重定义,赋值兼容规则
作用域与同名隐藏 一样的,先上代码 1 class A 2 { 3 public: 4 int a_data; 5 void a() 6 { 7 cout << "A" ...
- Circular placeholder reference 'jdbc.driver' in property definitions
Caused by: java.lang.IllegalArgumentException: Circular placeholder reference 'jdbc.driver' in prope ...
- form表单的enctype
form表单在你不写enctype属性时,也默认为其添加了enctype属性值,默认值是enctype="application/x- www-form-urlencoded".这 ...