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 本文中我们继续深入研究,设备到云.云到 ...
随机推荐
- css常用居中
对一个已知大小的元素上下左右居中(已知大小了,直接margin也就行了): css如下:.parent{height:100px;width:100px;background:grey;positio ...
- 深度学习实践系列(2)- 搭建notMNIST的深度神经网络
如果你希望系统性的了解神经网络,请参考零基础入门深度学习系列,下面我会粗略的介绍一下本文中实现神经网络需要了解的知识. 什么是深度神经网络? 神经网络包含三层:输入层(X).隐藏层和输出层:f(x) ...
- JDK中日期和时间的几个常用类浅析(五)
LocalDateTime LocalDateTime是JDK8中才引入的类,用来表示不包含时区信息的本地日期和时间.我们可以把LocalDateTime看作是LocalDate和LocalTim ...
- php调试之路
解析php中die(),exit(),return的区别 die()停止程序运行,输出内容exit是停止程序运行,不输出内容return是返回值die是遇到错误才停止exit是直接停止,并且不运行后续 ...
- nodejs oj在线笔试应对方案(讲几种输入处理方法)
最近参加了一些线上笔试.但是...我不是学计算机的,只会js不会c++,java,c(好吧都学过,不过忘了).可怕的是我也没学过nodejs,怎么 办,怎么办.node不就是用的js吗?所以只用学会标 ...
- SpringBoot-SpringMvc的Interceptor拦截器配置
Interceptor拦截器实现对每一个用户请求处理前后的业务处理,比如我们需要对用户请求进行响应时间的记录,需要记录请求从开始到结束所耗的时间,这时我们就需要用到拦截器了 下面我们以记录请求处理时间 ...
- PIC32MZ Live update bootloader
PIC32MZ 的 flash memory 支持live update, 这是个全新的特性,在之前的所有PIC不管是8位还是16位的单片机上面都没有这个特性.我写过很多PIC 8位和16位单片机的b ...
- SpringCloud网关ZUUL集成consul
最近一直在搞基于springcloud的微服务开发,为了不限定微服务开发语言,服务发现决定采用consul不多说上代码 pom文件 <project xmlns="http://mav ...
- Xamarin XAML语言教程使用Xamarin Studio创建XAML(二)
Xamarin XAML语言教程使用Xamarin Studio创建XAML(二) 使用Xamarin Studio创建XAML Xamarin Studio和Visual Studio创建XAML文 ...
- CSharpGL(41)改进获取字形贴图的方法
CSharpGL(41)改进获取字形贴图的方法 在(http://www.cnblogs.com/bitzhuwei/p/CSharpGL-28-simplest-way-to-creating-fo ...