我使用Net 5 API,它通过MassTransit发布和使用消息。 问题是我试图找到一种方法来为这两种情况创建自定义中间件或管道(发布消耗)来测量发布或使用消息的时间......
我尝试创建一个完全按照文档演示 https://masstransit-project.com//advanced/middleware/custom.html 创建自定义中间件,但问题是当我尝试发布消息时无法调用这些方法。根据本主题,它仅适用于使用的消息。 有人知道如何为这两种情况创建管道或中间件(发布消费)吗?
据我所知,您只需要测量(记录?)发布和使用消息的时间 - 如果是这样,您可以使用观察器。具体而言,发布观察器和使用观察器。
例如,消费观察器的示例是:
public class ConsumeObserver : IConsumeObserver
{
Task IConsumeObserver.PreConsume<T>(ConsumeContext<T> context)
{
// called before the consumer's Consume method is called
}
Task IConsumeObserver.PostConsume<T>(ConsumeContext<T> context)
{
// called after the consumer's Consume method is called
// -> Log DateTime.Now with message details found in context. <-
}
Task IConsumeObserver.ConsumeFault<T>(ConsumeContext<T> context, Exception exception)
{
// called if the consumer's Consume method throws an exception
}
}
有两种方法,ConfigureSend
和ConfigurePublish
,可以通过IBusFactoryConfigurator
访问,其中过滤器可以分别添加到发送和发布管道中。