-
-
Save teeroddesigns/4eb19238ead16c5ff4ed588d4b7014ac to your computer and use it in GitHub Desktop.
Simple service to test long running subscriptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net.Http; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using EventStore.Client; | |
using Microsoft.Extensions.Hosting; | |
using Microsoft.Extensions.Logging; | |
using Position = EventStore.Client.Position; | |
using ResolvedEvent = EventStore.Client.ResolvedEvent; | |
namespace LongRunningSubscriber | |
{ | |
internal class TestService : IHostedService | |
{ | |
private readonly ILogger<TestService> _logger; | |
public TestService(ILogger<TestService> logger) | |
{ | |
_logger = logger; | |
} | |
public async Task StartAsync(CancellationToken cancellationToken) | |
{ | |
_logger.LogInformation("LongRunningSubscriberService has started."); | |
//credentials removed for example | |
var credentials = new EventStore.Client.UserCredentials("admin", "changeit"); | |
var settings = new EventStoreClientSettings | |
{ | |
CreateHttpMessageHandler = () => | |
new HttpClientHandler | |
{ | |
ServerCertificateCustomValidationCallback = | |
(message, certificate2, x509Chain, sslPolicyErrors) => true // ignore https | |
}, | |
//cluster address removed for example | |
ConnectivitySettings = { Address = new Uri("esdb://hostedcluster.mesdb.eventstore.cloud:2113/") }, | |
ConnectionName = "TestConnection", | |
DefaultCredentials = credentials | |
}; | |
var client = new EventStoreClient(settings); | |
await client.SubscribeToAllAsync( Position.Start, EventAppearedAsync, resolveLinkTos: false, SubscriptionDropped, filterOptions: GetStreamFilterOptions(), cancellationToken: cancellationToken).ConfigureAwait(false); | |
_logger.LogInformation(client.ConnectionName); | |
} | |
public Task StopAsync(CancellationToken cancellationToken) | |
{ | |
_logger.LogInformation("LongRunningSubscriberService has stopped"); | |
return Task.CompletedTask; | |
} | |
private SubscriptionFilterOptions GetStreamFilterOptions() | |
{ | |
return new ( | |
EventTypeFilter.Prefix(new string[] | |
{ | |
"Security.Domain.Events.Group.UserAddedToGroup, Security.Domain.Events", | |
"Security.Domain.Events.Group.UserRemovedFromGroup, Security.Domain.Events", | |
"Security.Domain.Events.User.UserEnabled, Security.Domain.Events", | |
"Security.Domain.Events.User.UserDisabled, Security.Domain.Events" | |
})); | |
} | |
private void SubscriptionDropped(StreamSubscription streamSubscription, SubscriptionDroppedReason droppedReason, Exception arg3) | |
{ | |
_logger.LogError("Subscription dropped!", streamSubscription); | |
_logger.LogError("Reason: ", droppedReason); | |
_logger.LogError("Exception: ", arg3); | |
} | |
private Task EventAppearedAsync(StreamSubscription sub, ResolvedEvent @event, CancellationToken arg3) | |
{ | |
try | |
{ | |
_logger.LogInformation(@event.Event.EventType); | |
} | |
catch (Exception e) | |
{ | |
_logger.LogError(e.Message, e); | |
} | |
return Task.CompletedTask; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment