Using an Azure WebJob to read from an EventHub

。_饼干妹妹 提交于 2020-01-05 06:00:27

问题


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.

Here's my code:

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");
}

In my app.config, I've set the appropriate connection strings for AzureWebJobsDashboard, AzureWebJobsServiceBus and AzureWebJobsStorage.

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.

What am I missing?

Thank you for your time and looking forward to replies.


回答1:


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>

Result:

Also, you could log into Azure Portal, check the overview blade of your Event Hub as follows:



来源:https://stackoverflow.com/questions/41953543/using-an-azure-webjob-to-read-from-an-eventhub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!