C# 如何将消息从eventhub发送到另一个eventhub?
我想将eventhub发送给客户,然后将其下载到天气等示例数据,并发送另一个eventhub。我的代码工作不正常。没有错误,但数据未发送到数据库C# 如何将消息从eventhub发送到另一个eventhub?,c#,azure,azure-eventhub,C#,Azure,Azure Eventhub,我想将eventhub发送给客户,然后将其下载到天气等示例数据,并发送另一个eventhub。我的代码工作不正常。没有错误,但数据未发送到数据库 public Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages) { try { foreach (EventData me
public Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
{
try
{
foreach (EventData message in messages)
{
string data = Encoding.UTF8.GetString(message.GetBytes());
NewClient Client = JsonConvert.DeserializeObject<NewClient>(data);
if (Client.City != null && Client.Street != null )
{
GoogleGeoApi GeoClient = new GoogleGeoApi();
GeoClient.SetCoordinates(Client.City, Client.Street);
WeatherApi WeatherApiobject = new WeatherApi();
WeatherApiobject.GetJson(GeoClient.convertlat, GeoClient.convertlng);
string weatherdata = WeatherApiobject.sendEvent;
SenderEvent NewSenderEvent = new SenderEvent();
NewSenderEvent.DataSender(weatherdata, ConstFile.WeatherEventHubName);
//StartH(ConstFile.WeatherEventHubName).Wait();
}
Interlocked.Increment(ref this.totalMessages);
this.LastMessageOffset = message.Offset;
}
if (this.IsClosed)
{
this.IsReceivedMessageAfterClose = true;
}
if (this.checkpointStopWatch.Elapsed > TimeSpan.FromMinutes(1))
{
lock (this)
{
this.checkpointStopWatch.Reset();
return context.CheckpointAsync();
}
}
}
catch (Exception ex)
{
Console.WriteLine("{0} > Event Hub Exception: {1}", DateTime.Now, ex.Message);
}
return Task.FromResult<object>(null);
}
公共任务流程事件同步(分区上下文,IEnumerable消息)
{
尝试
{
foreach(消息中的EventData消息)
{
字符串数据=Encoding.UTF8.GetString(message.GetBytes());
NewClient Client=JsonConvert.DeserializeObject(数据);
if(Client.City!=null&&Client.Street!=null)
{
GoogleGeoApi GeoClient=新的GoogleGeoApi();
GeoClient.SetCoordinates(Client.City,Client.Street);
WeatherApi WeatherApi对象=新的WeatherApi();
GetJson(GeoClient.convertlat,GeoClient.convertlng);
字符串weatherdata=WeatherApiobject.sendEvent;
SenderEvent NewSenderEvent=新的SenderEvent();
DataSender(weatherdata,ConstFile.WeatherEventHubName);
//StartH(ConstFile.WeatherEventHubName.Wait();
}
联锁增量(参考此totalMessages);
this.LastMessageOffset=message.Offset;
}
如果(本文件已关闭)
{
this.IsReceivedMessageAfterClose=true;
}
如果(this.checkpointStopWatch.appeased>TimeSpan.FromMinutes(1))
{
锁(这个)
{
这个.checkpointStopWatch.Reset();
返回context.CheckpointAsync();
}
}
}
捕获(例外情况除外)
{
WriteLine(“{0}>事件中心异常:{1}”,DateTime.Now,ex.Message);
}
返回Task.FromResult(空);
}
我要补充的是,我的receiver eventhub如下所示:
您可以使用流分析非常轻松地做到这一点!您的第一个事件中心是流内分析。然后,您可以针对流编写一个脚本(从[Input1]中选择*这将获得所有内容)。您可以将流返回到另一个事件中心。您可以使用流分析非常轻松地做到这一点!您的第一个事件中心是流内分析。然后,您可以针对流编写一个脚本(从[Input1]中选择*这将获得所有内容)。您可以将流返回到另一个事件中心。这取决于:
private static async Task StartHost(string eventHubName)
{
string eventProcessorHostName = "1";
string storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", ConstFile.storageAccountName, ConstFile.storageAccountKey);
host = new EventProcessorHost(
eventProcessorHostName,
eventHubName,
ConstFile.ConsumerGroup,
ConstFile.eventHubConnectionString,
storageConnectionString, eventHubName.ToLowerInvariant());
factory = new DemoEventProcessorFactory(eventProcessorHostName);
try
{
var options = new EventProcessorOptions();
options.ExceptionReceived += (sender, e) => { Console.WriteLine(e.Exception); };
await host.RegisterEventProcessorFactoryAsync(factory);
}
catch (Exception exception)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("{0} > Exception: {1}", DateTime.Now.ToString(), exception.Message);
Console.ResetColor();
}
}
}
}
这个类是什么?这个类负责将数据发送到另一个
EventHub
?如果是这样,请张贴该代码。将代码粘贴到下面。SenderEvent对于两条消息(客户端和weatherdata)都是相同的,并且应用程序是否到达行eventhubclient.Send(data1)粘贴代码的代码>是否无异常?第二个活动中心的收听过程是什么?我希望大家都能理解。我没有任何例外。在下面的注释中,我添加了code eventhub侦听。这个类是什么SenderEvent
,该类负责将数据发送到另一个eventhub
?如果是这样,请张贴该代码。将代码粘贴到下面。SenderEvent对于两条消息(客户端和weatherdata)都是相同的,并且应用程序是否到达行eventhubclient.Send(data1)粘贴代码的代码>是否无异常?第二个活动中心的收听过程是什么?我希望大家都能理解。我没有任何例外。在下面的评论中,我添加了代码eventhub。
private static async Task StartHost(string eventHubName)
{
string eventProcessorHostName = "1";
string storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", ConstFile.storageAccountName, ConstFile.storageAccountKey);
host = new EventProcessorHost(
eventProcessorHostName,
eventHubName,
ConstFile.ConsumerGroup,
ConstFile.eventHubConnectionString,
storageConnectionString, eventHubName.ToLowerInvariant());
factory = new DemoEventProcessorFactory(eventProcessorHostName);
try
{
var options = new EventProcessorOptions();
options.ExceptionReceived += (sender, e) => { Console.WriteLine(e.Exception); };
await host.RegisterEventProcessorFactoryAsync(factory);
}
catch (Exception exception)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("{0} > Exception: {1}", DateTime.Now.ToString(), exception.Message);
Console.ResetColor();
}
}
}
}