I'm using Event Grid in a C# console app for send events that are stored in a QueueStorage account in Azure portal. The problem is ¿How can I receive the message stored in a C# console app? for example:
In the image you can see the topic created and the subscriptions that exist. So, the subscription "queue-sub" have stored all the messages sent from another C# console app. This C# console app implementing the library of eShopContainer of "EventBus"and I'm crating a library to use "EventGrid" with that Interface of "EventBus". For example:
public void Subscribe<T, TH>()
where T : IntegrationEvent
where TH : IIntegrationEventHandler<T>
{
}
This if a fragment code from that implement Interface "IEventBus" and that Interface told me that "I need subscribe for listening the events" but I don´t know how do that.
Here is the git link if you check the whole project that I'm working. https://github.com/Angel1803/EventGridListenerMessage.git
This is how you can write code for receiving messages using IEventBus
:
namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.EventHandling
{
public class ProductPriceChangedIntegrationEventHandler :
IIntegrationEventHandler<ProductPriceChangedIntegrationEvent>
{
private readonly IBasketRepository _repository;
public ProductPriceChangedIntegrationEventHandler(
IBasketRepository repository)
{
_repository = repository;
}
public async Task Handle(ProductPriceChangedIntegrationEvent @event)
{
var userIds = await _repository.GetUsers();
foreach (var id in userIds)
{
var basket = await _repository.GetBasket(id);
await UpdatePriceInBasketItems(@event.ProductId, @event.NewPrice, basket);
}
}
private async Task UpdatePriceInBasketItems(int productId, decimal newPrice,
CustomerBasket basket)
{
var itemsToUpdate = basket?.Items?.Where(x => int.Parse(x.ProductId) ==
productId).ToList();
if (itemsToUpdate != null)
{
foreach (var item in itemsToUpdate)
{
if(item.UnitPrice != newPrice)
{
var originalPrice = item.UnitPrice;
item.UnitPrice = newPrice;
item.OldUnitPrice = originalPrice;
}
}
await _repository.UpdateBasket(basket);
}
}
}
}
For complete tutorial on IEventBus
usage, you can visit Subscribing to events
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.