0
How do I get the entire value returned through a Task? I have tried several ways, as I have seen in forums on the net, but I could not. I tried to use var customerId = Bus.Sendcommand(registerCommand). Result, but it doesn’t work... Thank you all :)
public class CustomerCommandHandler : CommandHandler,
IRequestHandler<RegisterNewCustomerCommand, int>,
{
private readonly ICustomerRepository _customerRepository;
private readonly IMediatorHandler Bus;
public CustomerCommandHandler(ICustomerRepository customerRepository,
IUnitOfWork uow,
IMediatorHandler bus,
INotificationHandler<DomainNotification> notifications) :base(uow, bus, notifications)
{
_customerRepository = customerRepository;
Bus = bus;
}
public Task<int> Handle(RegisterNewCustomerCommand message, CancellationToken cancellationToken)
{
if (!message.IsValid())
{
NotifyValidationErrors(message);
return Task.FromResult(0);
}
var customer = new Customer(Guid.NewGuid(), message.Name, message.Email, message.BirthDate);
if (_customerRepository.GetByEmail(customer.Email) != null)
{
Bus.RaiseEvent(new DomainNotification(message.MessageType, "The customer e-mail has already been taken."));
return Task.FromResult(0);
}
_customerRepository.Add(customer);
if (Commit())
{
Bus.RaiseEvent(new CustomerRegisteredEvent(customer.Id, customer.Name, customer.Email, customer.BirthDate));
}
return Task.FromResult(customer.Id);
}
public void Dispose()
{
_customerRepository.Dispose();
}
}
Calling for:
public void Register(CustomerViewModel customerViewModel)
{
var registerCommand = _mapper.Map<RegisterNewCustomerCommand>(customerViewModel);
var customerId = Bus.SendCommand(registerCommand); /////????????????????????
}
SendCommand
isasync
?– Barbetta
It’s not async @Barbetta. :)
– Master JR
In that case it would be good to see the error that gives, I always used
.Result
– Barbetta
The problem is that it gives a build error if I use . Result (Bus.Sendcommand(registerCommand).Result;). It’s like it doesn’t exist... Do you have a reference? I updated the post with the full class.
– Master JR
Weird, and what error message displayed? When referencing the
System.Threading.Tasks
to use Task<T> should already be enough– Barbetta
This way an error appears saying that it is not possible to convert TASK to Integer: var registerCommand = _mapper. Map<Registernewcustomercommand>(customerViewModel); var return = Task.Fromresult(Bus.Sendcommand(registerCommand)). Result; int customerId = return;
– Master JR
Now I’m getting paid, but I can’t play the value of Result for a variable...
– Master JR
Let’s go continue this discussion in chat.
– Barbetta
Your Sendcommand probably returns a Task, so making the implicit conversion would not work. As it returns a task, just add . Result in the end, if it still didn’t work, post your concrete Immediathandler class, it is possible that the Sendcommand implementation is incorrect. Follow a sample project, make a comparison https://github.com/superrfm/MediaTrAspNetCore
– Rafael
So it is: public interface Immediathandler { Task Sendcommand<T>(T command) Where T : Command; Task Raiseevent<T>(T @Event) Where T : Event; }
– Master JR