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
}
}
host.CallAsync(typeof(TestStatelessService).GetMethod("ProcessMethod"), cancellationToken)
Please pay attention that the TestStatelessService
class has no parameterless constructor defined, so you could mark the ProcessMethod
function as static.
According to your description, I followed this tutorial to create an Azure Service Fabric application.Based on your code, I tested the WebJob SDK successfully in my Service Fabric application. Here is my code sample, please try to find out whether is works on your side.
TestStatelessService.cs
/// <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 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);
}
Result
The “Health State” of the application is set to “Error” in the Service Fabric Explorer although the application is still running.
Please try to debug the code on your side and find the detailed errors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With