I have been trying to add listeners for messages posted on the Azure storage Queues inside of the Service Fabric. The code snippets that I have used in my stateless service are as follows:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Fabric;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.ServiceFabric.Services.Communication.Runtime;
using Microsoft.ServiceFabric.Services.Runtime;
using Microsoft.WindowsAzure.Storage.Queue;
using Notification;
namespace My.Notification
{
/// <summary>
/// An instance of this class is created for each service instance by the Service Fabric runtime.
/// </summary>'
internal sealed class Notification : StatelessService
{
public Notification(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];
}
//First option
protected override async Task RunAsync(CancellationToken cancellationToken)
{
JobHostConfiguration config = new JobHostConfiguration();
config.DashboardConnectionString = "string";
config.StorageConnectionString = "stringg";
config.Queues.BatchSize = 10;
config.Queues.MaxDequeueCount = 8;
config.Queues.MaxPollingInterval = TimeSpan.FromSeconds(30);
var host = new JobHost(config);
//Breaks below
await host.CallAsync(typeof(Notification).GetMethod("ProcessMethod"), cancellationToken);
host.RunAndBlock();
}
//Second option
protected override async Task RunAsync(CancellationToken cancellationToken)
{
try
{
var config = new JobHostConfiguration
{
DashboardConnectionString = "string",//Real connection string,
StorageConnectionString = "string",
Queues =
{
BatchSize = 1,
MaxDequeueCount = 3,
MaxPollingInterval = TimeSpan.FromSeconds(30)
}
};
var host = new JobHost(config);
//Breaks here
await host.StartAsync(cancellationToken);
}
catch (Exception ex)
{
//ServiceEventSource.Current.ServiceStartupFailedEvent(ex.ToString());
throw;
}
}
[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("emailqueue")] string message)
{
Trace.TraceInformation(">ProcessNotificationsInQueue invoked with notification:{0}", message);
}
}
}
The error I get is shown below. I have tried both ways and I am not sure how to get more details other than this. Some posts have recommended that we enable MDAs and I have also tried that. Others have pointed it might be a CLR issue. I have also had a look at this post here and I have almost the same default setup.