问题
I understand the let NServiceBus handles the error and let it retry or use the second level of retries this are valid for some unknown error within the applicaiton.
For example let say it the transactionId == null then i dont just want to throw the exception and let NService bus handles it as i know this never gonna pass with any extra attempts. For know exception scenarios what is the best this do?
I am using saga which call the what different endpoints (A,B,C,D). how do you handle the above error scenario in this endpoints as i dont want my saga to be in a hanging state.
回答1:
If you have an exception (or class of exceptions) that you know no number of retries will help, then you are correct, it is best to fail fast.
let's say you have a handler like this..
public void Handle(BadMath message)
{
var zero = 0;
var crash = 5 / zero;
var msg = "I will never be reached...";
this.SendReply(msg );
}
And you want to trap & fail fast for div/0...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Features;
using NServiceBus.SecondLevelRetries.Helpers;
namespace My.Namespace.Messaging.Handlers
{
public class ChangeRetryPolicy : INeedInitialization
{
public void Init()
{
Configure.Features.Disable<SecondLevelRetries>();
Configure.Features.SecondLevelRetries(s => s.CustomRetryPolicy((tm) =>
{
// retry max 3 times
if (TransportMessageHelpers.GetNumberOfRetries(tm) >= 3)
{
// To send back a value less than zero tells the SecondLevelRetry
// satellite not to retry this message anymore.
return TimeSpan.MinValue;
}
if (tm.Headers["NServiceBus.ExceptionInfo.ExceptionType"] == typeof(System.DivideByZeroException).FullName)
{
return TimeSpan.MinValue;
}
return TimeSpan.FromSeconds(5);
}));
Configure.Features
}
}
}
If you try to run the handler via a unit test, the test will of course blow up and throw an error. If you put a break point in the handler on "crash = 5 / zero", then re-run your project you should see that when NServiceBus tries to redeliver the message, the second-level retries do not occur and your undelivered goes directly to the error queue.
来源:https://stackoverflow.com/questions/18453270/nservicebus-error-handling