在Service Fabric应用程序中运行时,WebJob SDK无法正常工作 [英] WebJob SDK not working when running in a Service Fabric application

查看:73
本文介绍了在Service Fabric应用程序中运行时,WebJob SDK无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在作为Service Fabric应用程序运行的无状态服务中使用WebJob SDK.很遗憾,我无法使其正常运行.下面是重现该问题的一部分测试代码.永远不会调用"ProcessMethod".触发函数"ProcessNotificationsInQueue"也永远不会执行(是的,队列中有项目).尽管该应用程序仍在运行,但在Service Fabric资源管理器中将其运行状况"设置为错误".

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.

DashboardConnectionString和StorageConnectionString都具有正确的值.

The DashboardConnectionString and StorageConnectionString both have the correct values.

当它在控​​制台应用程序或WorkerRole中运行时,非常相似的代码没有任何问题.

I don’t see any problem with a very similar code when it is running in a console application or a WorkerRole.

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

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"),cancelToken)

host.CallAsync(typeof(TestStatelessService).GetMethod("ProcessMethod"), cancellationToken)

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

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);
}

结果

尽管应用程序仍在运行,但在Service Fabric资源管理器中将其运行状况"设置为错误".

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.

这篇关于在Service Fabric应用程序中运行时,WebJob SDK无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆