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 c
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.