I am trying to use azure-sdk-for-cpp to connect to Azure Event Hub. But it is not working, I get error "Could not open Claims Based Security object."
I am following samples in the sources.
I have just this simple code and exception is thrown in GetEventHubProperties:
Azure::Messaging::EventHubs::ProducerClient producerClient(connectionString, eventhubName);
Azure::Messaging::EventHubs::Models::EventHubProperties eventhubProperties = producerClient.GetEventHubProperties();
In Azure Portal I have created resource group with Event Hubs Namespace. In it I have Event Hub called "tolp-985-event-hub" which I am using as eventhubName
. I was trying various things as connectionString: From Event Hubs Namespace – Settings – Shared Access Policies – RootManageSharedAccessKey – Connection string–primary key. I tried also from Event Hub itself – Settings – Shared Access Policies – it was empty, I created a new policy with Manage+Send+Listen and from there Connection string–primary key. I tried without "EntityPath" in the connection string. I tried with it and with empty eventhubName, I tried various combinations but none working. I am probably missing some simple configuration or something…
To test if this is no fundamental problem with my environment etc. I tried something different from samples, to create a blob container with some content such as:
auto containerClient = Azure::Storage::Blobs::BlobContainerClient::CreateFromConnectionString(connectionString, containerName);
containerClient.CreateIfNotExists();
for (int j = 0; j < 3; ++j) {
Azure::Storage::Blobs::BlockBlobClient blobClient = containerClient.GetBlockBlobClient(blobName + std::to_string(j));
blobClient.UploadFrom(reinterpret_cast<const uint8_t*>(blobContent.data()), blobContent.size());
}
There I use connection string from Storage Account and it works fine.
But connection string from Storage Account starts with "DefaultEndpointsProtocol=https;AccountName=" and maybe it is something very different from connection string for Event Hub, which starts with "Endpoint=sb://tolp-985-event-hubs-namespace.servicebus.windows.net/;SharedAccessKeyName=".
Thanks for any hint to get over this initial problem.
EDIT, code and images:
try {
string connectionString = "Endpoint=sb://tolp-985-event-hubs-namespace.servicebus.windows.net/;SharedAccessKeyName=tolp-985-policy;SharedAccessKey=GGQyg...;EntityPath=tolp-985-event-hub";
string eventhubName = "tolp-985-event-hub";
Azure::Messaging::EventHubs::ProducerClient producerClient(connectionString, eventhubName);
Azure::Messaging::EventHubs::Models::EventHubProperties eventhubProperties = producerClient.GetEventHubProperties();
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
2
Answers
The approach was correct. Configuration of Azure Portal was proper as verified by Python script doing the same operations. Connection string was used properly.
There was also no problem in the code itself built using Azure SDK for cpp.
Problem was in refused certificates. Although it worked from Python script and from C++ with BlobStorage, connection was refused from C++ with EventHub. Problem was in environment configuration related to Certificate Authority, SSL_CERT_FILE had to be set properly. Could be set in environment or in the code itself like
The test was done in Ubuntu.
To pinpoint the problem, detailed logs were activated by:
I have tried the sample code in my environment, it worked fine for me to send the messages to the Azure Event Hub.
Manage, Send
, andListen
permissions.The Azure Event Hub connection string must be in the below format,
main.cpp :
CMakeLists.txt :
Output :
I ran the below command to set the Azure EventHub connection string and Event Hub Name in the env varaibles.
The following code ran successfully and sent the messages to the Azure Event Hub as shown below.
Azure Event Hub :