上篇博文中,我们介绍了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的更多相关文章

  1. Azure Event Hub 技术研究系列3-Event Hub接收事件

    上篇博文中,我们通过编程的方式介绍了如何将事件消息发送到Azure Event Hub: Azure Event Hub 技术研究系列2-发送事件到Event Hub 本篇文章中,我们继续:从Even ...

  2. Azure Event Bus 技术研究系列1-Event Hub入门篇

    前两个系列研究了Azure IoT Hub和Azure Messaging.最近准备继续研究Azure Event Bus,即Azure的事件中心.首先, Azure Event Hub的官方介绍: ...

  3. Azure Event Hub 技术研究系列1-Event Hub入门篇

    前两个系列研究了Azure IoT Hub和Azure Messaging.最近准备继续研究Azure Event Hub,即Azure的事件中心.首先, Azure Event Hub的官方介绍: ...

  4. Azure IoT 技术研究系列5-Azure IoT Hub与Event Hub比较

    上篇博文中,我们介绍了Azure IoT Hub的使用配额和缩放级别: Azure IoT 技术研究系列4-Azure IoT Hub的配额及缩放级别 本文中,我们比较一下Azure IoT Hub和 ...

  5. Azure IoT 技术研究系列4-Azure IoT Hub的配额及缩放级别

    上两篇博文中,我们介绍了将设备注册到Azure IoT Hub,设备到云.云到设备之间的通信: Azure IoT 技术研究系列2-设备注册到Azure IoT Hub Azure IoT 技术研究系 ...

  6. Azure IoT 技术研究系列2-起步示例之设备注册到Azure IoT Hub

    上篇博文中,我们主要介绍了Azure IoT Hub的基本概念.架构.特性: Azure IoT 技术研究系列1-入门篇 本文中,我们继续深入研究,做一个起步示例程序:模拟设备注册到Azure IoT ...

  7. Azure IoT 技术研究系列2-设备注册到Azure IoT Hub

    上篇博文中,我们主要介绍了Azure IoT Hub的基本概念.架构.特性: Azure IoT 技术研究系列1-入门篇 本文中,我们继续深入研究,做一个起步示例程序:模拟设备注册到Azure IoT ...

  8. Azure IoT 技术研究系列3-设备到云、云到设备通信

    上篇博文中我们将模拟设备注册到Azure IoT Hub中:我们得到了设备的唯一标识. Azure IoT 技术研究系列2-设备注册到Azure IoT Hub 本文中我们继续深入研究,设备到云.云到 ...

  9. Azure IoT 技术研究系列3

    上篇博文中我们将模拟设备注册到Azure IoT Hub中:我们得到了设备的唯一标识. Azure IoT 技术研究系列2-设备注册到Azure IoT Hub 本文中我们继续深入研究,设备到云.云到 ...

随机推荐

  1. 数据库dbutils

    common-dbutils.jarQueryRunnerupdate方法:* int update(String sql, Object... params) -->  可执行增.删.改语句* ...

  2. Linux线程的创建

    一.线程与进程的区别 1.线程自己不拥有系统资源,只拥有一点儿在运行中必不可少的资源,但它可与同属一个进程的其它线程共享进程所拥有的全部资源. 2.进程是资源分配的基本单位.所有与该进程有关的资源,都 ...

  3. nginx 入门配置

    这个星期公司的定期分享内容是Nginx,于是就要写作业了. 一.动静分离 1.下载Windows 版本的Nginx,解压,放到C盘下.进入目录,然后按然shift键右键,打开命令行,输入: start ...

  4. HackerRank The Chosen One [预处理][gcd]

    题解:tags:[预处理][gcd]故事背景:光头钻进了茫茫人海.这是一个典型の通过前缀后缀和来降低复杂度的问题.先用pre数组与suf数组分别维护前缀gcd和后缀gcd.如果 a[i] % gcd( ...

  5. js 操作属性

    操作属性: 对象.setAttribute('属性名','值'); - 添加属性 对象.getAttribute('属性名'); - 获取属性值,如无此属性,那么返回null 对象.removeAtt ...

  6. 手机交互应用服务(状态栏提示信息Notifications)

    官方的一个简单的例子: NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon ...

  7. Android中的socket本地通讯框架

    一.先分析Native层: 1.C++基类SocketListener: class SocketListener {     int mSock;     const char *mSocketNa ...

  8. Redis基础学习(五)—Redis的主从复制

    一.概述     Redis的主从复制策略是通过其持久化的rdb文件来实现的,其过程是先dump出rdb文件,将rdb文件全量传输给slave,然后再将dump后的操作实时同步到slave中.让从服务 ...

  9. js 形参和实参---2017-04-11

    一.定义 1.实参(argument):     全称为"实际参数"是在调用时传递给函数的参数. 实参可以是常量.变量.表达式.函数等, 无论实参是何种类型的量,在进行函数调用时, ...

  10. ThinkPHP框架前后台的分页调用

    一般ThinkPHP框架在底层给开发者提供了一个基本的分页类Page.class.php里面规定了Page类的一些基本的参数和结构. 2.应用分页 1> 调取文件use Think\Page; ...