public async Task StartAsync(CancellationToken cancellationToken) { Log.Information("Migrating Database..."); await _databaseContext.Database.MigrateAsync(cancellationToken); Log.Information("Activating NATS responders"); _natsResponders = NatsResponder.ActivateAll(_serviceProvider); // We need to access the IEnumerable for them to actually activate :( Log.Debug("Found {count} responders: {@names}", _natsResponders.Count(), _natsResponders.Select(x => x.GetType().Name)); Log.Information("Ready :)"); await Task.Delay(-1, cancellationToken); }
public async Task StartAsync(CancellationToken cancellationToken) { Log.Information("Activating NATS responders"); _natsResponders = NatsResponder.ActivateAll(_serviceProvider); // We need to access the IEnumerable for them to actually activate :( Log.Debug("Found {count} responders: {@names}", _natsResponders.Count(), _natsResponders.Select(x => x.GetType().Name)); Log.Debug("Initializing command handler"); await _commandHandler.InitializeAsync(); Log.Information("Logging in..."); await _discordService.LoginAndStart(); Log.Information("Bot started"); }
public void Subscribe(string eventName, MethodInfo methodInfo, NatsResponder responder, Type parseType) { _connection.SubscribeAsync(eventName, async(sender, args) => { var arg = JsonSerializer.Deserialize(args.Message.Data, parseType); Log.Debug("[Nats Request] {EventName}, {@data}", eventName, arg); var reply = methodInfo.Invoke(responder, new[] { arg }); if ((methodInfo.ReturnParameter?.ParameterType.IsGenericType ?? false) && methodInfo.ReturnParameter?.ParameterType.GetGenericTypeDefinition() == typeof(Task <>)) { reply = ((dynamic)reply)?.Result; } else if (methodInfo.ReturnParameter?.ParameterType == typeof(Task)) { ((Task)reply)?.Wait(); reply = null; } Log.Debug("[Nats Response] {EventName}, {@data}", eventName, reply); args.Message.Respond(JsonSerializer.SerializeToUtf8Bytes(reply)); }); }