I want to use the WebJob SDK in a stateless service running as a Service Fabric application. Unfortunately I’m not able to get it running properly. Below is part of a test code that reproduces the problem. The “ProcessMethod“ is never invoked. The triggered function “ProcessNotificationsInQueue“ is also never executed (yes, there are items in the queue). The “Health State” of the application is set to “Error” in the Service Fabric Explorer although the application is still running.
The DashboardConnectionString and StorageConnectionString both have the correct values.
I don’t see any problem with a very similar code when it is running in a console application or a WorkerRole.
Am I missing something? Does anyone has used already the WebJob SDK successfully in a Service Fabric application?
public sealed class TestStatelessService : StatelessService
{
public TestStatelessService(StatelessServiceContext context)
: base(context)
{ }
/// <summary>
/// Optional override to create listeners (e.g., TCP, HTTP) for this service replica to handle client or user requests.
/// </summary>
/// <returns>A collection of listeners.</returns>
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new ServiceInstanceListener[0];
}
/// <summary>
/// This is the main entry point for your service instance.
/// </summary>
/// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service instance.</param>
protected override async Task RunAsync(CancellationToken cancellationToken)
{
ConfigurationPackage configPackage = this.Context.CodePackageActivationContext.GetConfigurationPackageObject("Config");
KeyedCollection<string, ConfigurationProperty> 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
}
}