![](/img/trans.png)
[英]C# console app: How can I continue returning an error message until user enters valid data?
[英]How can I receive the message from a QueueStorage of Event Grid (Azure Portal) in a C# Console app?
我在 C# 控制台應用程序中使用事件網格來發送存儲在 Azure 門戶的 QueueStorage 帳戶中的事件。 問題是 ¿ 如何接收存儲在 C# 控制台應用程序中的消息? 例如:
在圖像中,您可以看到創建的主題和存在的訂閱。 因此,訂閱“queue-sub”存儲了從另一個 C# 控制台應用程序發送的所有消息。 這個 C# 控制台應用程序實現了“EventBus”的 eShopContainer 庫,我正在創建一個庫來使用“EventGrid”和“EventBus”接口。 例如:
public void Subscribe<T, TH>()
where T : IntegrationEvent
where TH : IIntegrationEventHandler<T>
{
}
如果來自該實現接口“IEventBus”的片段代碼和該接口告訴我“我需要訂閱以監聽事件”,但我不知道該怎么做。
如果您檢查我正在工作的整個項目,這是 git 鏈接。 https://github.com/Angel1803/EventGridListenerMessage.git
這是您如何編寫使用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);
}
}
}
}
有關IEventBus
使用的完整教程,您可以訪問訂閱事件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.