【问题标题】:Routing JSON data to Event Hubs in Azure将 JSON 数据路由到 Azure 中的事件中心
【发布时间】:2018-10-25 09:52:38
【问题描述】:

我有一种情况,我需要通过事件中心将 JSON 数据(一个 JSON 文件,而不是转换为 JSON)发送到时序见解。但由于缺乏 C# 经验,我无法发送数据。

我可以发送其他示例消息,但不能发送 JSON。我该怎么做?

任何帮助或见解将不胜感激。

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using System.IO;
using Microsoft.ServiceBus.Messaging;

namespace ConsoleApp5
{
    class Program
    {
        static string _connectionString = "Endpoint..;

        static async Task MainAsync(string[] args)
        {
            var client = EventHubClient.CreateFromConnectionString(_connectionString, "eventhub");
            var json = File.ReadAllText(@"C:\Users\Shyam\Downloads\personal.json");
            var eventData = new EventData(Encoding.UTF8.GetBytes(json));
            await EventHubClient.SendAsync(eventData);

        }
    }
}

但它会在异步方法中引发错误。

严重性代码描述项目文件行抑制状态 错误 CS0120 非静态字段、方法或属性“EventHubClient.SendAsync(EventData)”需要对象引用 ConsoleApp5 C:\Users\Shyam\source\repos\ConsoleApp5\ConsoleApp5\Program.cs 21 Active

更新:

namespace jsonData
{
    using System;
    using System.Text;
    using System.IO;
    using System.Threading.Tasks;
    using Microsoft.Azure.EventHubs;

    public class Program
    {
        private static EventHubClient eventHubClient;
        private const string EhConnectionString = "Endpoint=sb://";
        private const string EhEntityPath = "hub";

        public static void Main(string[] args)
        {
            MainAsync(args).GetAwaiter().GetResult();
        }

        private static async Task MainAsync(string[] args)
        {
            // Creates an EventHubsConnectionStringBuilder object from the connection string, and sets the EntityPath.
            // Typically, the connection string should have the entity path in it, but this simple scenario
            // uses the connection string from the namespace.
            var connectionStringBuilder = new EventHubsConnectionStringBuilder(EhConnectionString)
            {
                EntityPath = EhEntityPath
            };

            eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());

            var json = File.ReadAllText(@"D:\Sample.json");
            var eventData = new EventData(Encoding.UTF8.GetBytes(json));
            await eventHubClient.SendAsync(eventData);

            await eventHubClient.CloseAsync();


            Console.WriteLine("Press ENTER to exit.");
            Console.ReadLine();
        }
    }
}

【问题讨论】:

  • await EventHubClient.SendAsync(eventData); 替换为await client.SendAsync(eventData); 以消除错误。
  • 谢谢。有效。显然我是从非静态方法调用静态方法。

标签: c# .net azure azure-eventhub azure-timeseries-insights


【解决方案1】:

将您的事件包装到 JSON 数组中:

using (var ms = new MemoryStream())
using (var sw = new StreamWriter(ms))
{
    // Wrap events into JSON array:
    sw.Write("[");
    for (int i = 0; i < events.Count; ++i)
    {
        if (i > 0)
        {
            sw.Write(',');
        }
        sw.Write(events[i]);
    }
    sw.Write("]");

    sw.Flush();
    ms.Position = 0;

    // Send JSON to event hub.
    EventData eventData = new EventData(ms);
    eventHubClient.Send(eventData);
}

参考:docs.microsoft.com/time-series-insights-send-events

【讨论】:

    【解决方案2】:

    我相信您现在已经弄清楚了,但您的问题不在于 JSON,而在于您使用事件中心客户端的方式。

    代替这一行:

    await EventHubClient.SendAsync(eventData);
    

    应该是这样的:

    await client.SendAsync(eventData);
    

    【讨论】:

    • 谢谢丹。两者有什么区别?
    • 好吧,我很困惑你的代码是如何编译的,因为 EventHubClient.SendAsync 不能作为静态方法使用。调用 SendAsync 的唯一方法是使用 EventHubClient 类的实例。在您的代码中,您创建了一个客户端(对象),但从未使用过您创建的实际对象的实例。如果您是编程新手或 C# 新手,最好从这里开始:docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
    • 看起来不错。它是否有效,还是 JSON 仍有问题?
    • 是的。它工作正常。那么它是对的吗?我使用了许多来自互联网的资源来整理这个。
    【解决方案3】:

    JSON 只是事件中心的一个字符串,所以很简单

    var json = File.ReadAllText("myfile.json");
    var eventData = new EventData(Encoding.UTF8.GetBytes(json));
    await eventHubClient.SendAsync(eventData);
    

    【讨论】:

    • 谢谢米哈伊尔。我对此有一些疑问。我已经更新了摘要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    相关资源
    最近更新 更多