1
votes

I'm completely brand new to C#, Microsoft Azure, and basically everything. I'm trying to set up an Azure Event Hub that I can send data to. Right now I'm just following the tutorial that can be found here.

It builds just fine, but I receive the same exception every time. The message is the following: An existing connection was forcibly closed by the remote host. This question has been asked before but never answered.

Just to be sure I'm doing this right I'm attaching pictures with where I obtained the values for the Event Hub Connection String and the Hub Name.

Where I got the Event Hub Connection String from.

Where I got the Event Hub Connection String from.

This one is within the namespace - not the hub itself.

Where I got the Hub Name from. Where I got the Hub Name from.

The code goes as follows:

private const string EventHubConnectionString = "<Connection String>";
private const string EventHubName = "eventhubtest";

Does the Hub Name have to be simply that or a path? Any ideas or help would be greatly appreciated. Thanks.

3

3 Answers

4
votes

@Jamie Penzien, I had been stuck with this exact same error for days and my colleague asked me to change the following part and it worked.

var connectionStringBuilder = new EventHubsConnectionStringBuilder(EventHubConnectionString)
{
    EntityPath = EventHubName,
    TransportType = TransportType.AmqpWebSockets                
};

I am still trying to understand the reason, and it may have something to do with the company's firewall settings.

1
votes

Eventhub name or Entity Path would be simply the name of EventHub found under an EventHub namespace.

You can use below code to create client:

EventHubClient eventHubClient;
var connectionStringBuilder = new EventHubsConnectionStringBuilder(EventHubConnectionString)
{
     EntityPath = EventHubName
};
eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());

EventHubsConnectionStringBuilder can be found under Microsoft.Azure.EventHubs package.

0
votes

I answered my own questions. First, I needed to change the connection string and make sure it contained the entity path within it. Then when establishing the hub client I did this:

eventHubClient = EventHubClient.CreateFromConnectionString(EventHubConnectionString);

HOWEVER, I was getting this exception specifically because of a firewall issue. I have to open ports (working on it right now) to allow outbound communication to the event hub. I believe these are ports 5671 and 5672.

Thank you to all that answered and @RayX who nailed it on the head.