我正在尝试使用来自WebJob的EventHub消息,但没有用。作业构建和运行时不会抛出任何异常,但永远不会调用触发器。我正在引用Microsoft.Azure.WebJobs,Microsoft.Azure.WebJobs.Extensions和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设置了相应的连接字符串。
我尝试过使用批量消息到更改触发器方法的方法签名,特别是参数类型为EventData或byte []。什么都行不通。我应该注意消息作为包装在EventData中的字节数组发送到EventHub。
我错过了什么?
感谢您的时间并期待回复。
答案 0 :(得分:2)
根据您的说明,我按照Azure WebJobs SDK EventHub support和Get started with Event Hubs来测试这个问题。我可以收到消息,你可以参考我的代码片段:
<强> 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 Portal,查看Event Hub的概述刀片,如下所示: