public async Task <Guid> DispatchAsync <TCommand>(TCommand command) where TCommand : class, ICommand { IValidator <TCommand> validator = _context.GetService <IValidator <TCommand> >(); if (validator.IsNotNull()) { ValidationResult validationResult = await validator.ValidateAsync(command); if (!validationResult.IsValid) { throw new ValidationException(validationResult.Errors); } } command.CorrelationId = _guidService.New; IRequestClient <TCommand> client = _clientFactory.CreateRequestClient <TCommand>(new Uri("queue:commands"), TimeSpan.FromMinutes(10)); MassTransit.Response <FWTL.Common.Cqrs.Responses.Response> response = await client.GetResponse <FWTL.Common.Cqrs.Responses.Response>(command); if (response.Message.StatusCode == HttpStatusCode.BadRequest) { throw new AppValidationException(response.Message.Errors); } if (response.Message.StatusCode == HttpStatusCode.InternalServerError) { throw new InvalidOperationException(response.Message.Id.ToString()); } return(response.Message.Id); }
public async Task Should_publish_the_event_of_the_missing_instance() { IRequestClient <CheckStatus> requestClient = Bus.CreateRequestClient <CheckStatus>(InputQueueAddress, TestTimeout); (var status, var notFound) = await requestClient.GetResponse <Status, InstanceNotFound>(new CheckStatus("Z"), TestCancellationToken); Assert.That(notFound.IsCompletedSuccessfully(), Is.True); MassTransit.Response <InstanceNotFound> result = await notFound; Assert.AreEqual("Z", result.Message.ServiceName); Assert.That(async() => await status, Throws.TypeOf <TaskCanceledException>()); }
public async Task Should_match_an_existing_instance() { IRequestClient <Start> startClient = Bus.CreateRequestClient <Start>(InputQueueAddress, TestTimeout); await startClient.GetResponse <StartupComplete>(new Start("A", NewId.NextGuid())); IRequestClient <CheckStatus> statusClient = Bus.CreateRequestClient <CheckStatus>(InputQueueAddress, TestTimeout); (var status, var notFound) = await statusClient.GetResponse <Status, InstanceNotFound>(new CheckStatus("A"), TestCancellationToken); Assert.That(status.IsCompletedSuccessfully(), Is.True); MassTransit.Response <Status> result = await status; Assert.AreEqual("A", result.Message.ServiceName); Assert.That(async() => await notFound, Throws.TypeOf <TaskCanceledException>()); }