Exemple #1
0
        public void Configure(IApplicationBuilder app,
                              IEventStoreBus eventBus,
                              IEventStoreProjections projections,
                              WebSocketHandler wsHandler,
                              VotingReadModelService readModelService,
                              ILogger <Startup> logger)
        {
            app.UseCors("CorsPolicy");
            app.UseMvc();
            app.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Voting API"));
            app.UseWebSockets();
            app.MapWebSocketManager("/ws", wsHandler);

            projections.CreateAsync(Projections.Voting)
            .DefaultRetryAsync()
            .Wait();

            eventBus.Subscribe(
                async(@event) =>
            {
                var snapshot = await readModelService.AddOrUpdate(@event);
                logger.LogInformation(snapshot.ToString());
                await wsHandler.SendMessageToAllAsync(snapshot);
            })
            .DefaultRetryAsync()
            .Wait();
        }
        public async Task Subscribe <TAggregate>(Func <Guid, object, Task> messageSender)
            where TAggregate : IAggregate
        {
            await DefaultRetryAsync(() => SubscribeImpl());

            async Task SubscribeImpl()
            {
                var streamName = $"{typeof(TAggregate).Name}Stream";
                await _projections.CreateAsync(streamName, DefaultProjection.Default(typeof(TAggregate).Name, streamName));

                await Subscribe(streamName, messageSender);
            }
        }