2

I have a situation where I need to send JSON data (a JSON file, not convert to JSON) to Time Series Insights via Event Hubs. But I am not able to send the data due to my lack of experience in C#.

I am able to send other sample messages but not JSON. How can I do that?

Any help or insight would be appreciated.

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);

        }
    }
}

It throws an error in the async method though.

Severity Code Description Project File Line Suppression State Error CS0120 An object reference is required for the non-static field, method, or property 'EventHubClient.SendAsync(EventData)' ConsoleApp5 C:\Users\Shyam\source\repos\ConsoleApp5\ConsoleApp5\Program.cs 21 Active

UPDATE:

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();
        }
    }
}
Shyam V
  • 444
  • 4
  • 22
  • 1
    Replace `await EventHubClient.SendAsync(eventData);` with `await client.SendAsync(eventData);` to get rid of the error. – Peter Bons May 15 '18 at 17:29
  • Thank you. It worked. Apparently I was calling a Static method from a non static one. – Shyam V May 16 '18 at 06:00

3 Answers3

3

Wrap your events into a JSON array:

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);
}

Reference: learn.microsoft.com/time-series-insights-send-events

johnny 5
  • 19,893
  • 50
  • 121
  • 195
David
  • 641
  • 1
  • 8
  • 23
3

I'm sure you have figured this out by now but you're problem is not with JSON, it's with how you're using the event hub client.

Instead of this line:

await EventHubClient.SendAsync(eventData);

it should be this:

await client.SendAsync(eventData);
Dan
  • 3,583
  • 1
  • 23
  • 18
  • Thanks Dan. What is the difference between the two? – Shyam V May 29 '18 at 06:27
  • 1
    Well, I'm confused how your code even compiled because EventHubClient.SendAsync is not available as a static method. The only way to call SendAsync is to use an instance of the EventHubClient class. In your code, you created a client (object) but then never used the instance of the actual object you created. If you're new to programming or new to C# it might be better to start here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/ – Dan May 30 '18 at 12:15
  • 1
    It looks good. Is it working, or do you still have a problem with JSON? – Dan May 31 '18 at 09:13
  • Yes.Its working fine. So is it right then? I used many sources from the internet to put together this one. – Shyam V May 31 '18 at 09:45
1

JSON is just a string for Event Hubs, so as simple as

var json = File.ReadAllText("myfile.json");
var eventData = new EventData(Encoding.UTF8.GetBytes(json));
await eventHubClient.SendAsync(eventData);
Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107