0

I've tried to come up with something from the example in the WebJobsSDK gitHub

var eventHubConfig = new EventHubConfiguration();
string eventHubName = "MyHubName";
eventHubConfig.AddSender(eventHubName,"Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=SendRule;SharedAccessKey=xxxxxxxx");
eventHubConfig.AddReceiver(eventHubName, "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=ReceiveRule;SharedAccessKey=yyyyyyy");

config.UseEventHub(eventHubConfig);
JobHost host = new JobHost(config);

But I'm afraid that's not far enough for someone of my limited "skillset"!

  • I can find no instance of JobHostConfiguration that has a UseEventHub property (using the v1.2.0-alpha-10291 version of the Microsoft.AzureWebJobs package), so I can't pass the EventHubConfiguration to the JobHost.

  • I've used EventHub before, not within the WebJob context. I don't see if the EventHostProcessor is still required if using the WebJob triggering...or does the WebJob trigger essentially act as the EventHostProcessor?

Anyway, if anyone has a more complete example for a simpleton like me that would be really sweet! Thanks

Thomas
  • 24,234
  • 6
  • 81
  • 125
WirelessG
  • 227
  • 2
  • 11

2 Answers2

2

From the documentation here, you should have all the information you need.

What you are missing is a reference of the Microsoft.Azure.WebJobs.ServiceBus.1.2.0-alpha-10291 nuget package.

The UseEventHub is an extension method that is declared in this package.

Otherwise your configuration seems ok. Here is an example on how to receive or send messages from/to an EventHub:

public class BasicTest
{
    public class Payload
    {
        public int Counter { get; set; }
    }
    public static void SendEvents([EventHub("MyHubName")] out Payload x)
    {
        x = new Payload { Counter = 100 };
    }

    public static void Trigger(
        [EventHubTrigger("MyHubName")] Payload x,
        [EventHub("MyHubName")] out Payload y)
    {
        x.Counter++;
        y = x;
    }
}
Thomas
  • 24,234
  • 6
  • 81
  • 125
  • Thanks Thomas, I was using that nuget package and following that rather 'light' example. I didn't see the extension method so once I added a "using Microsoft.Azure.WebJobs.ServiceBus" I could at least get it to compile. I'm still a ways from anything actually working though. As I mentioned, that example is a little oversimplified for my skills. – WirelessG Apr 20 '16 at 13:47
  • @WirelessG, feel free to edit your question or ask a new one if you need more detail on implemting a solution. – Thomas Apr 20 '16 at 20:54
1

EventProcessorHost is still required, as the WebJob just provides the hosting environment for running it. As far as I know, EventProcessorHost is not integrated so deeply into WebJob, so its triggering mechanism cannot be used for processing EventHub messages. I use WebJob for running EventProcessorHost continuously:

public static void Main()
{
    RunAsync().Wait();
}

private static async Task RunAsync()
{
    try
    {
        using (var shutdownWatcher = new WebJobsShutdownWatcher())
        {
            await Console.Out.WriteLineAsync("Initializing...");

            var eventProcessorHostName = "eventProcessorHostName";
            var eventHubName = ConfigurationManager.AppSettings["eventHubName"];
            var consumerGroupName = ConfigurationManager.AppSettings["eventHubConsumerGroupName"];
            var eventHubConnectionString = ConfigurationManager.ConnectionStrings["EventHub"].ConnectionString;
            var storageConnectionString = ConfigurationManager.ConnectionStrings["EventHubStorage"].ConnectionString;

            var eventProcessorHost = new EventProcessorHost(eventProcessorHostName, eventHubName, consumerGroupName, eventHubConnectionString, storageConnectionString);

            await Console.Out.WriteLineAsync("Registering event processors...");

            var processorOptions = new EventProcessorOptions();

            processorOptions.ExceptionReceived += ProcessorOptions_ExceptionReceived;

            await eventProcessorHost.RegisterEventProcessorAsync<CustomEventProcessor>(processorOptions);

            await Console.Out.WriteLineAsync("Processing...");

            await Task.Delay(Timeout.Infinite, shutdownWatcher.Token);

            await Console.Out.WriteLineAsync("Unregistering event processors...");

            await eventProcessorHost.UnregisterEventProcessorAsync();

            await Console.Out.WriteLineAsync("Finished.");
        }
        catch (Exception ex)
        {
            await HandleErrorAsync(ex);
        }
    }
}

private static async void ProcessorOptions_ExceptionReceived(object sender, ExceptionReceivedEventArgs e)
{
    await HandleErrorAsync(e.Exception);
}

private static async Task HandleErrorAsync(Exception ex)
{
    await Console.Error.WriteLineAsync($"Critical error occured: {ex.Message}{ex.StackTrace}");
}
Attila Cseh
  • 235
  • 2
  • 13
  • Thanks Attila, I've done it that way before, but I think in the new pre-release of azure-webjobs-sdk it's supposed be more deeply integrated. Using the Trigger mechanism in a similar way to webjobs with ServiceBus...I just can't figure it out from the wiki. https://github.com/Azure/azure-webjobs-sdk/wiki/EventHub-support – WirelessG Apr 19 '16 at 20:51
  • I've run into this kind of hosting issue recently too, but I had just found [Azure WebJobs SDK Extensions](https://github.com/Azure/azure-webjobs-sdk-extensions), which does not deal with EventHub at all. My main concern with the [WebJob SDK EventHub support](https://github.com/Azure/azure-webjobs-sdk/wiki/EventHub-support) is its automatic checkpointing after processing messages: a) in high throughput scenarios it might degrade performance; b) how to retry processing messages after a temporary processing failure (eg.: permanent storage is not available). – Attila Cseh Apr 20 '16 at 07:09
  • The new [EventHubTrigger] attribute (in the service bus extension) will handle the listening for you. It will create an EventProcessHost. For checking-pointing, you can receive a batch of events (https://github.com/Azure/azure-webjobs-sdk/wiki/EventHub-support ) and the checkpoint is called once for the whole batch. – Mike S May 18 '16 at 19:15