使用Azure WebJob从EventHub读取 [英] Using an Azure WebJob to read from an EventHub
问题描述
我正在尝试使用WebJob的EventHub消息,但无济于事.该作业将在不引发任何异常的情况下进行构建和运行,但是永远不会调用触发器.我指的是Microsoft.Azure.WebJobs,Microsoft.Azure.WebJobs.Extensions和Microsoft.Azure.WebJobs.ServiceBus v2.0.0.0-beta2.
I am trying to consume EventHub messages from a WebJob, without avail. The job builds and runs without throwing any exceptions, but the trigger is never called. I am referencing Microsoft.Azure.WebJobs, Microsoft.Azure.WebJobs.Extensions and Microsoft.Azure.WebJobs.ServiceBus v2.0.0.0-beta2.
这是我的代码:
Program.cs:
public static void Main()
{
var eventHubConfig = new EventHubConfiguration();
string eventHubName = "myHub";
eventHubConfig.AddReceiver(eventHubName, "Endpoint=sb://xxxx.servicebus.windows.net/;SharedAccessKeyName=xxxx;SharedAccessKey=yyyy");
config.UseEventHub(eventHubConfig);
JobHost host = new JobHost(config);
if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}
host.RunAndBlock();
}
Functions.cs:
public static void Trigger([EventHubTrigger("myHub")] string message)
{
_logger.Debug("Message received");
}
在我的app.config中,我为AzureWebJobsDashboard,AzureWebJobsServiceBus和AzureWebJobsStorage设置了适当的连接字符串.
In my app.config, I've set the appropriate connection strings for AzureWebJobsDashboard, AzureWebJobsServiceBus and AzureWebJobsStorage.
我已经尝试了一切,从使用大量消息到更改触发器方法的方法签名,特别是将参数类型更改为EventData或byte [].什么都行不通.我应该注意,消息是作为包裹在EventData中的字节数组发送到EventHub的.
I've tried everything from using batches of messages to changing the method signature of the trigger method, specifically the parameter type to EventData or byte[]. Nothing works. I should note messages are sent to the EventHub as byte arrays wrapped in an EventData.
我想念什么?
感谢您的时间,期待答复.
Thank you for your time and looking forward to replies.
推荐答案
根据您的描述,我遵循了使用事件中心开始,从我这一方面进行测试.我可以收到消息,您可以参考我的代码片段:
According to your description, I followed Azure WebJobs SDK EventHub support and Get started with Event Hubs to test this issue on my side. And I could receive messages, you could refer to my code snippet:
Program.cs
class Program
{
static string eventHubName = "{your-EventHub-name}";
static string connectionString = "{RootManageSharedAccessKey-connection-string}";
static void Main(string[] args)
{
JobHostConfiguration config = new JobHostConfiguration();
config.Tracing.ConsoleLevel = System.Diagnostics.TraceLevel.Error;
var eventHubConfig = new EventHubConfiguration();
eventHubConfig.AddReceiver(eventHubName, connectionString);
config.UseEventHub(eventHubConfig);
JobHost host = new JobHost(config);
if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}
//Send test messages
Task.Run(() => {
SendingRandomMessages();
});
host.RunAndBlock();
}
static void SendingRandomMessages()
{
var eventHubClient = EventHubClient.CreateFromConnectionString(connectionString,eventHubName);
while (true)
{
try
{
var message = Guid.NewGuid().ToString();
Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, message);
eventHubClient.Send(new EventData(Encoding.UTF8.GetBytes(message)));
}
catch (Exception exception)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("{0} > Exception: {1}", DateTime.Now, exception.Message);
Console.ResetColor();
}
Thread.Sleep(4000);
}
}
}
Functions.cs
public static void Trigger([EventHubTrigger("{your-EventHub-name}")] EventData message)
{
string data = Encoding.UTF8.GetString(message.GetBytes());
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Message received. Data: '{data}'");
Console.ResetColor();
}
Packages.config
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net451" />
<package id="Microsoft.Azure.ServiceBus.EventProcessorHost" version="2.2.6" targetFramework="net451" />
<package id="Microsoft.Azure.WebJobs" version="2.0.0-beta2" targetFramework="net451" />
<package id="Microsoft.Azure.WebJobs.Core" version="2.0.0-beta2" targetFramework="net451" />
<package id="Microsoft.Azure.WebJobs.ServiceBus" version="2.0.0-beta2" targetFramework="net451" />
<package id="Microsoft.Data.Edm" version="5.6.4" targetFramework="net451" />
<package id="Microsoft.Data.OData" version="5.6.4" targetFramework="net451" />
<package id="Microsoft.Data.Services.Client" version="5.6.4" targetFramework="net451" />
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="1.8.0.0" targetFramework="net451" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net451" />
<package id="System.Spatial" version="5.6.4" targetFramework="net451" />
<package id="WindowsAzure.ServiceBus" version="3.4.1" targetFramework="net451" />
<package id="WindowsAzure.Storage" version="7.2.1" targetFramework="net451" />
</packages>
结果:
此外,您可以登录Azure门户,如下所示查看事件中心的概述刀片:
Also, you could log into Azure Portal, check the overview blade of your Event Hub as follows:
这篇关于使用Azure WebJob从EventHub读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!