I am using WebJob template for Net 4.6.1 and EventStore.Client 4.1.1
In Program.cs
var host = new jobHost(config);
host.Call(typeof(Functions).GetMethod("Processmethod"));
host.RunAndBlock;
And I have created a public function in a Functions class such as
[NoAutomaticTrigger]
static void ProcessMethod {
I am using a persistent connection and this works as a normal console application fine. When I try to have it long running as a WebJob the EvernStore.Client will connect, maybe get 1 event sometimes, then disconnect.
Close Reason: [Success] Connection close requested by client.
The code is in a using such as
using (var conn = EvenStoreConnection.Create....)))
{
con.ConnectAsync().Wait();
con.ConnectToPersistentSubscription(STREAM, GROUP, (_,X) =>
{
int en = x.EventNumber;
...
I use a while(true) to block the thread. This works as a normal console app.
Is there something I need to do specifically for WebJobs to keep the connection from dropping out? If I run it locally or in the cloud the same thing happens.
I am just trying to find a way to subscribe to event store events using some Azure way of doing it so we can monitor it. Am I doing this incorrectly maybe? How can I get a persistent Subscription working in Azure?
It seems like the client will disconnect depending on various internal events, such as exceptions or errors.
So one of the key options is to use KeepReconnecting()
var settings = ConnectionSettings
.Create()
.KeepReconnecting()
.KeepRetrying()
//.EnableVerboseLogging()
.UseConsoleLogger();
That on its own will prevent the client from dropping out and should reconnect. So the actual connection code will look something like this
_subscription = _conn.ConnectToPersistentSubscription(STREAM, GROUP,
(_base, _event) => { EventAppeared(_base, _event); },
(_base, _reason, _exception) => { SubscriptionDropped(_base, _reason, _exception); },
User, bufferSize, autoAck);
That is a bit out of the ordinary for a .NET type of client connection that I have ever worked with. What is happening there is quite obvious for STREAM and GROUP but the next two are Action<> Delegates that handle the Event that appeared and the usefull Connection dropped.
In that case for good measure my method for handling the connection closed looks like this
private void SubscriptionDropped(EventStorePersistentSubscriptionBase eventStorePersistentSubscriptionBase, SubscriptionDropReason subscriptionDropReason, Exception ex)
{
Console.WriteLine($"**** Connection dropped reason? '{subscriptionDropReason}' exception? '{ex.Message}'- Reconnecting ...");
ConnectToSubscription();
}
And for the event appeared
private static void EventAppeared(EventStorePersistentSubscriptionBase eventStorePersistentSubscriptionBase, ResolvedEvent resolvedEvent)
{
var x = resolvedEvent;
... your code
}
This persistent subscriber has been running for weeks without issue, as an AppService WebJob!