我尝试将数据插入事件中心,以便遵循此link在Azure门户中创建免费试用事件中心,并在示例. Net应用程序(引用link)中配置connectionstring、eventHubName
样本代码:
class Program
{
const string eventHubName = "<eventHubName>";
const string connectionString = @"<connectionString>";
public static async Task Main(string[] args)
{
await EventHubIngestionAsync();
}
public static async Task EventHubIngestionAsync()
{
await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName))
{
int counter = 0;
for (int i = 0; i < 100; i++)
{
int recordsPerMessage = 3;
try
{
var records = Enumerable
.Range(0, recordsPerMessage)
.Select(recordNumber => $"{{\"timeStamp\": \"{DateTime.UtcNow.AddSeconds(100 * counter)}\", \"name\": \"{$"name {counter}"}\", \"metric\": {counter + recordNumber}, \"source\": \"EventHubMessage\"}}");
string recordString = string.Join(Environment.NewLine, records);
EventData eventData = new EventData(Encoding.UTF8.GetBytes(recordString));
Console.WriteLine($"sending message {counter}");
// Optional "dynamic routing" properties for the database, table, and mapping you created.
//eventData.Properties.Add("Table", "TestTable");
//eventData.Properties.Add("IngestionMappingReference", "TestMapping");
//eventData.Properties.Add("Format", "json");
using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();
eventBatch.TryAdd(eventData);
IEnumerable<EventData> dd = null;
await producerClient.SendAsync(dd);
}
catch (Exception exception)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("{0} > Exception: {1}", DateTime.Now, exception.Message);
Console.ResetColor();
}
counter += recordsPerMessage;
}
}
}
}
运行CreateBatchAsync时,获取"由于连接方未正确响应,连接尝试失败"。
我尝试了连接字符串和访问控制方法,得到相同的错误。
1条答案
按热度按时间vq8itlhq1#
该异常表明客户机无法与事件集线器服务建立连接。此特定故障最常见的原因是网络不允许通过AMQP端口(5761、5762)进行通信。
我建议配置客户端在web套接字上使用AMQP,如本示例所示,这将通过端口443引导流量,这在受限网络中更为常见。