在Service Fabric应用程序中运行时,WebJob SDK无法正常工作

我想在作为Service Fabric应用程序运行的无状态服务中使用WebJob SDK。 不幸的是我无法让它正常运行。 下面是重现问题的测试代码的一部分。 永远不会调用“ProcessMethod”。 触发函数“ProcessNotificationsInQueue”也从不执行(是的,队列中有项目)。 虽然应用程序仍在运行,但应用程序的“运行状况”在Service Fabric Explorer中设置为“错误”。

DashboardConnectionString和StorageConnectionString都具有正确的值。

当它在控制台应用程序或WorkerRole中运行时,我看不到任何类似代码的问题。

我错过了什么吗? 有没有人在Service Fabric应用程序中成功使用过WebJob SDK?

public sealed class TestStatelessService : StatelessService { public TestStatelessService(StatelessServiceContext context) : base(context) { } ///  /// Optional override to create listeners (eg, TCP, HTTP) for this service replica to handle client or user requests. ///  /// A collection of listeners. protected override IEnumerable CreateServiceInstanceListeners() { return new ServiceInstanceListener[0]; } ///  /// This is the main entry point for your service instance. ///  /// Canceled when Service Fabric needs to shut down this service instance. protected override async Task RunAsync(CancellationToken cancellationToken) { ConfigurationPackage configPackage = this.Context.CodePackageActivationContext.GetConfigurationPackageObject("Config"); KeyedCollection parameters = configPackage.Settings.Sections["MyConfigSection"].Parameters; JobHostConfiguration config = new JobHostConfiguration(); config.DashboardConnectionString = parameters["AzureWebJobsDashboard"].Value; config.StorageConnectionString = parameters["AzureWebJobsStorage"].Value; config.Queues.BatchSize = 10; config.Queues.MaxDequeueCount = 8; config.Queues.MaxPollingInterval = TimeSpan.FromSeconds(30); var host = new JobHost(config); host.CallAsync(typeof(TestStatelessService).GetMethod("ProcessMethod"), cancellationToken); host.RunAndBlock(); } [NoAutomaticTrigger] public async Task ProcessMethod(CancellationToken cancellationToken) { long iterations = 0; while (true) { cancellationToken.ThrowIfCancellationRequested(); ServiceEventSource.Current.ServiceMessage(this, "Working-{0}", ++iterations); await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); } } [Timeout("00:03:00")] public static void ProcessNotificationsInQueue([QueueTrigger("newnotificationqueue")] Notification notification) { //Do something } } 

host.CallAsync(typeof(TestStatelessService).GetMethod(“ProcessMethod”),cancellationToken)

请注意TestStatelessService类没有定义无参数构造函数,因此您可以将ProcessMethod函数标记为静态。

根据您的描述,我按照本教程创建了一个Azure Service Fabric应用程序。基于您的代码,我在Service Fabric应用程序中成功测试了WebJob SDK。 这是我的代码示例,请尝试找出是否适合您。

TestStatelessService.cs

 ///  /// This is the main entry point for your service instance. ///  /// Canceled when Service Fabric needs to shut down this service instance. protected override async Task RunAsync(CancellationToken cancellationToken) { ConfigurationPackage configPackage = this.Context.CodePackageActivationContext.GetConfigurationPackageObject("Config"); KeyedCollection parameters = configPackage.Settings.Sections["MyConfigSection"].Parameters; JobHostConfiguration config = new JobHostConfiguration(); config.DashboardConnectionString = parameters["AzureWebJobsDashboard"].Value; config.StorageConnectionString = parameters["AzureWebJobsStorage"].Value; config.Queues.BatchSize = 10; config.Queues.MaxDequeueCount = 8; config.Queues.MaxPollingInterval = TimeSpan.FromSeconds(30); var host = new JobHost(config); host.CallAsync(typeof(TestStatelessService).GetMethod("ProcessMethod"),cancellationToken); host.RunAndBlock(); } [NoAutomaticTrigger] public static async Task ProcessMethod(CancellationToken cancellationToken) { long iterations = 0; while (true) { cancellationToken.ThrowIfCancellationRequested(); //log Trace.TraceInformation(">>[{0}]ProcessMethod Working-{1}",DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"),++iterations); //sleep for 5s await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); } } [Timeout("00:03:00")] public static void ProcessNotificationsInQueue([QueueTrigger("newnotificationqueue")] CloudQueueMessage notification) { Trace.TraceInformation(">ProcessNotificationsInQueue invoked with notification:{0}", notification.AsString); } 

结果

虽然应用程序仍在运行,但应用程序的“运行状况”在Service Fabric Explorer中设置为“错误”。

请尝试调试您身边的代码并查找详细错误。