[英]How can I receive the message from a QueueStorage of Event Grid (Azure Portal) in a C# Console app?
I'm using Event Grid in a C# console app for send events that are stored in a QueueStorage account in Azure portal.我在 C# 控制台应用程序中使用事件网格来发送存储在 Azure 门户的 QueueStorage 帐户中的事件。 The problem is ¿How can I receive the message stored in a C# console app?
问题是 ¿ 如何接收存储在 C# 控制台应用程序中的消息? 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.
因此,订阅“queue-sub”存储了从另一个 C# 控制台应用程序发送的所有消息。 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".
这个 C# 控制台应用程序实现了“EventBus”的 eShopContainer 库,我正在创建一个库来使用“EventGrid”和“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.如果来自该实现接口“IEventBus”的片段代码和该接口告诉我“我需要订阅以监听事件”,但我不知道该怎么做。
Here is the git link if you check the whole project that I'm working.如果您检查我正在工作的整个项目,这是 git 链接。 https://github.com/Angel1803/EventGridListenerMessage.git
https://github.com/Angel1803/EventGridListenerMessage.git
This is how you can write code for receiving messages using IEventBus
:这是您如何编写使用
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有关
IEventBus
使用的完整教程,您可以访问订阅事件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.