2

パブリッシャー/サブスクライバー パターンを使用していて、サブスクライバーの処理中に例外がスローされた場合、EasyNetQ はデフォルトでメッセージを EasyNetQ_Default_Error_Queue に入れます。

問題は、エラー メッセージに元の交換名しかないことです。本当に必要なのは元のキュー名です。これにより、メッセージを交換ではなくキューに再発行できます。

エラー メッセージにキュー名を含めるにはどうすればよいですか?

EasyNetQ_Default_Error_Queue のサンプル エラー メッセージ:

{
   "RoutingKey": "",
   "Exchange": "EiHD.Application.Restaurant.Events.RestaurantDeliveryMailChanged:EiHD",
   "Exception": "System.AggregateException: Um ou mais erros. ---> System.NullReferenceException: Referência de objeto não definida para uma instância de um objeto.\r\n   em EiHD.Infrastructure.Mailing.EmailSender.OnEvent(RestaurantDeliveryMailChanged evt) na d:\\Projetos\\EatInHouseDelivery\\sln\\Infrastructure\\Mailing\\EmailSender.cs:linha 136\r\n   em EiHD.Infrastructure.EventDispatching.EasyNeqQEventDispatcher.<>c__DisplayClass5`1.<RegisterListener>b__4(T evt) na d:\\Projetos\\EatInHouseDelivery\\sln\\Infrastructure\\EventDispatching\\EasyNeqQEventDispatcher.cs:linha 68\r\n   em EasyNetQ.RabbitBus.<>c__DisplayClass6`1.<Subscribe>b__5(T msg)\r\n   --- Fim do rastreamento de pilha de exceções internas ---\r\n---> (Exceção Interna N° 0) System.NullReferenceException: Referência de objeto não definida para uma instância de um objeto.\r\n   em EiHD.Infrastructure.Mailing.EmailSender.OnEvent(RestaurantDeliveryMailChanged evt) na d:\\Projetos\\EatInHouseDelivery\\sln\\Infrastructure\\Mailing\\EmailSender.cs:linha 136\r\n   em EiHD.Infrastructure.EventDispatching.EasyNeqQEventDispatcher.<>c__DisplayClass5`1.<RegisterListener>b__4(T evt) na d:\\Projetos\\EatInHouseDelivery\\sln\\Infrastructure\\EventDispatching\\EasyNeqQEventDispatcher.cs:linha 68\r\n   em EasyNetQ.RabbitBus.<>c__DisplayClass6`1.<Subscribe>b__5(T msg)<---\r\n",
   "Message": "{\"FromEmail\":\"someemail@gmail.com\",\"ToEmail\":\"anotheremail@gmail.com\",\"RestaurantName\":\"SomeRestaurant\"}",
   "DateTime": "2015-10-25T19:20:17.2317949Z",
   "BasicProperties": {
      "ContentType": null,
      "ContentEncoding": null,
      "Headers": {},
      "DeliveryMode": 2,
      "Priority": 0,
      "CorrelationId": "f212f734-6cd7-41fe-ac71-09335f44bb2c",
      "ReplyTo": null,
      "Expiration": null,
      "MessageId": null,
      "Timestamp": 0,
      "Type": "EiHD.Application.Restaurant.Events.RestaurantDeliveryMailChanged:EiHD",
      "UserId": null,
      "AppId": null,
      "ClusterId": null,
      "ContentTypePresent": false,
      "ContentEncodingPresent": false,
      "HeadersPresent": true,
      "DeliveryModePresent": true,
      "PriorityPresent": false,
      "CorrelationIdPresent": true,
      "ReplyToPresent": false,
      "ExpirationPresent": false,
      "MessageIdPresent": false,
      "TimestampPresent": false,
      "TypePresent": true,
      "UserIdPresent": false,
      "AppIdPresent": false,
      "ClusterIdPresent": false
   }
}
4

1 に答える 1

2

私は解決策を考え出しました。

DefaultConsumerErrorStrategy クラスの拡張バージョンをバスの IConsumerErrorStrategy に登録する必要がありました。

this.bus = RabbitHutch.CreateBus(this.connectionString, (serviceRegister) => {
    serviceRegister.Register<IConsumerErrorStrategy>((sp) => new ExtendedConsumerErrorStrategy(sp.Resolve<IConnectionFactory>(), sp.Resolve<ISerializer>(), sp.Resolve<IEasyNetQLogger>(), sp.Resolve<IConventions>(), sp.Resolve<ITypeNameSerializer>()));
});

public class ExtendedConsumerErrorStrategy : DefaultConsumerErrorStrategy
{
    public ExtendedConsumerErrorStrategy(IConnectionFactory connectionFactory, ISerializer serializer, IEasyNetQLogger logger, IConventions conventions, ITypeNameSerializer typeNameSerializer)
        : base(connectionFactory, serializer, logger, conventions, typeNameSerializer)
    {   
    }

    public override AckStrategy HandleConsumerError(ConsumerExecutionContext context, Exception exception)
    {
        context.Properties.Headers.Add("OriginalQueue", context.Info.Queue);
        return base.HandleConsumerError(context, exception);
    }
}

これで、メッセージのヘッダー プロパティに元のキュー名が含まれるようになりました。私のために働きます。

于 2015-10-25T23:28:18.290 に答える