WebJob SDK not working when running in a Service Fabric application

痞子三分冷 提交于 2019-11-30 21:12:38

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!