使用EventHub的WebJob的任何示例?

时间:2016-04-19 19:34:00

标签: azure azure-webjobs azure-eventhub azure-webjobssdk

我试图从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);

但我担心这对我有限的“技能组合”的人来说还不够!

  • 我找不到具有UseEventHub属性的JobHostConfiguration实例(使用Microsoft.AzureWebJobs包的v1.2.0-alpha-10291版本),因此我无法将EventHubConfiguration传递给JobHost。 / p>

  • 之前我曾使用过EventHub,而不是在WebJob上下文中。如果使用WebJob触发,我不知道是否仍然需要EventHostProcessor ......或者WebJob触发器是否基本上充当EventHostProcessor?

无论如何,如果有人对像我这样的傻瓜有一个更完整的例子,那将是非常甜蜜的!感谢

2 个答案:

答案 0 :(得分:2)

从文档here中,您应该拥有所需的所有信息。

您缺少的是it('test clicking on login by email change state', () => { let signInEmail = TestUtils.findRenderedDOMComponentWithClass(component, 'sign-in-with-email'); TestUtils.Simulate.click(signInEmail); expect(component.state.isEmailSignIn).toBe(true); }); nuget包的参考。

Microsoft.Azure.WebJobs.ServiceBus.1.2.0-alpha-10291是在此包中声明的扩展方法。

否则您的配置似乎没问题。 以下是如何从/向EventHub接收或发送消息的示例:

UseEventHub

答案 1 :(得分:1)

仍然需要EventProcessorHost,因为WebJob只提供运行它的托管环境。据我所知,EventProcessorHost没有深入集成到WebJob中,因此其触发机制不能用于处理EventHub消息。我使用WebJob连续运行EventProcessorHost:

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