【发布时间】:2017-01-12 21:53:41
【问题描述】:
我想在作为 Service Fabric 应用程序运行的无状态服务中使用 WebJob SDK。不幸的是,我无法让它正常运行。下面是重现问题的测试代码的一部分。 “ProcessMethod”永远不会被调用。触发的函数“ProcessNotificationsInQueue”也永远不会执行(是的,队列中有项目)。尽管应用程序仍在运行,但应用程序的“健康状况”在 Service Fabric Explorer 中设置为“错误”。
DashboardConnectionString 和 StorageConnectionString 都有正确的值。
当在控制台应用程序或 WorkerRole 中运行非常相似的代码时,我没有发现任何问题。
我错过了什么吗?是否有人已经在 Service Fabric 应用程序中成功使用过 WebJob SDK?
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
}
}
【问题讨论】:
标签: c# azure-service-fabric azure-webjobs