MassTransit JobConsumer middleware for the purpose of validating a message - masstransit

I would like to have an equivalent behavior but for Job Consumers :
public class MessageValidatorFilter<T> : IFilter<ConsumeContext<T>>
where T : class
{
private readonly ILogger<MessageValidatorFilter<T>> _logger;
private readonly IValidator<T> _validator;
public MessageValidatorFilter(ILogger<MessageValidatorFilter<T>> logger, IServiceProvider serviceProvider)
{
_logger = logger;
_validator = serviceProvider.GetService<IValidator<T>>();
}
public async Task Send(ConsumeContext<T> context, IPipe<ConsumeContext<T>> next)
{
var validationResult = _validator is not null
? await _validator.ValidateAsync(context.Message, context.CancellationToken)
: new ValidationResult();
if (validationResult.IsValid is false)
{
_logger.LogError("Message validation errors: {Errors}", validationResult.Errors);
await context.Send(
destinationAddress: new($"queue:yourcontext-{KebabCaseEndpointNameFormatter.Instance.SanitizeName(typeof(T).Name)}-validation-errors"),
message: new ValidationResultMessage<T>(context.Message, validationResult));
return;
}
await next.Send(context);
}
public void Probe(ProbeContext context) { }
}
But there is no Middleware for JobConsumers, this documentation (https://masstransit-project.com/advanced/middleware/custom.html) use ConsumeContext which does not work with Job Consumers

You can't, it isn't supported. If you want to validate the message, you'd need to do so in the job consumer, via an injected dependency.

Related

How to leave a message in the queue after failing masstrasit filter?

Masstransit, but I have such a situation that several message consumers can be launched on one queue, I created a filter that could help me receive the necessary messages for those producers who made the request, but there is a problem that after the message gets into the filter it is marked as read.
Is it possible to make it so that after hitting the filter and its unsuccessful passage, the message remains in the queue.
public class FilterConsumer <TConsumer, TMessage>: IFilter <ConsumerConsumeContext <TConsumer, TMessage>>
where TConsumer: class
where TMessage: class, ICacheKey {
private readonly MemoryCacheHelper _cache;
public FilterConsumer(IMemoryCache cache) {
_cache = new MemoryCacheHelper(cache);
}
public void Probe(ProbeContext context) {
context.CreateFilterScope("filterConsumer");
context.Add("output", "console");
}
public async Task Send(ConsumerConsumeContext <TConsumer, TMessage> context, IPipe <ConsumerConsumeContext < TConsumer, TMessage>> next) {
Console.WriteLine(context.Message.CacheKey());
if (_cache.CheckCache(context.Message.CacheKey()))
await next.Send(context);
else
await context.NotifyConsumed(TimeSpan.Zero, $ "Filtered");
}
}
public class AccountsConsumerDefinition: ConsumerDefinition < AccountsConsumer > {
private readonly IMemoryCache _cache;
public AccountsConsumerDefinition(IMemoryCache cache) {
_cache = cache;
}
protected override void ConfigureConsumer(IReceiveEndpointConfigurator endpointConfigurator, IConsumerConfigurator <AccountsConsumer> consumerConfigurator) {
consumerConfigurator.ConsumerMessage <AccountsBusResponse> (m => m.UseFilter(new FilterConsumer <AccountsConsumer, AccountsBusResponse> (_cache)));
}
}
services.AddMassTransit < TBus > (x => {
if (consumer != null)
x.AddConsumer < TConsumer > (consumerDifinition);
}

Test MassTransit state machine saga activity

I'm trying to do unit tests on a custom Activity that I have for my MassTransit state machine saga.
It looks something like this:
public class UpdateActivity : Activity<UpdateState>
{
private readonly ConsumeContext _consumeContext;
private readonly ILogger<UpdateActivity> _logger;
public UpdateActivity(
ConsumeContext consumeContext,
ILogger<UpdateActivity> logger
)
{
_consumeContext = consumeContext;
_logger = logger;
}
public void Probe(ProbeContext context) => context.CreateScope(nameof(UpdateActivity));
public void Accept(StateMachineVisitor visitor) => visitor.Visit(this);
public async Task Execute(BehaviorContext<UpdateState> context, Behavior<UpdateState> next)
{
await DoStuffAsync(context.Instance);
await next.Execute(context).ConfigureAwait(false);
}
public async Task Execute<T>(BehaviorContext<UpdateState, T> context, Behavior<UpdateState, T> next)
{
await DoStuffAsync(context.Instance);
await next.Execute(context).ConfigureAwait(false);
}
public Task Faulted<TException>(BehaviorExceptionContext<UpdateState, TException> context, Behavior<UpdateState> next) where TException : Exception
=> next.Faulted(context);
public Task Faulted<T, TException>(BehaviorExceptionContext<UpdateState, T, TException> context, Behavior<UpdateState, T> next) where TException : Exception
=> next.Faulted(context);
}
What I can't figure out is how I can mock/fake expectations for the ConsumeContext when writing unit tests for this class. I've tried to find something using the InMemoryTestHarness but can't find anything suitable.
EDIT:
I might as well throw this one as well in there. How do I mock context or run this in a test harness? So that I can unit test this Activity as well?
public class UpdateActivity : Activity<UpdateState, IDataUpdatedEvent>
{
private readonly ILogger<UpdateActivity> _logger;
public UpdateActivity(
ILogger<UpdateActivity > logger
)
{
_logger = logger;
}
public void Probe(ProbeContext context) => context.CreateScope(nameof(UpdateActivity));
public void Accept(StateMachineVisitor visitor) => visitor.Visit(this);
public async Task Execute(BehaviorContext<UpdateState, IDataUpdatedEvent> context, Behavior<UpdateState, IDataUpdatedEvent> next)
{
MassTransit has test harnesses to allow state machines to be tested, along with activities using Dependency Injection.
The idea of "testing in isolation with mocks" is fairly pointless given the availability of these harnesses.

DomainService not registered in AppService

I am trying to incorporate DomainService into my application, and tried to do it like the code below shows.
Here is the sample code for the manager:
namespace FlexSped.DefaultColors
{
public class DefaultColorManager : FlexSpedDomainServiceBase, IDefaultColorsManager
{
private readonly IRepository<DefaultColor> _defaultColorRepository;
public DefaultColorManager(IRepository<DefaultColor> defColorRep)
{
_defaultColorRepository = defColorRep;
}
public async Task Create(DefaultColor input)
{
await _defaultColorRepository.InsertAsync(input);
}
public Task Update(int id)
{
throw new NotImplementedException();
}
}
}
And this is the application service:
namespace FlexSped.DefaultColors
{
[AbpAuthorize(AppPermissions.Pages_Administration_DefaultColors)]
public class DefaultColorsAppService : FlexSpedAppServiceBase, IDefaultColorsAppService
{
private readonly IDefaultColorsManager _defaultColorManager;
private readonly IRepository<DefaultColor> _defaultColorRepository;
//private readonly IIocResolver _iocResolver;
public DefaultColorsAppService(IRepository<DefaultColor> defaultColorRepository, IDefaultColorsManager defColManager)
{
_defaultColorRepository = defaultColorRepository;
_defaultColorManager = defColManager;
//_iocResolver = iocResolver;
}
public async Task CreateOrEdit(CreateOrEditDefaultColorDto input)
{
if (input.Id == null)
{
await Create(input);
}
else
{
await Update(input);
}
}
[AbpAuthorize(AppPermissions.Pages_Administration_DefaultColors_Create)]
private async Task Create(CreateOrEditDefaultColorDto input)
{
DefaultColor dt = ObjectMapper.Map<DefaultColor>(input);
await _defaultColorManager.Create(dt);
}
}
}
All this produces this error:
'FlexSped.DefaultColors.DefaultColorsAppService' is waiting for the following dependencies:
- Service 'FlexSped.DefaultColors.IDefaultColorsManager' which was not registered.
Not sure what the problem is.I was following convention here.
ABPboiler is registering the services dependencies by name so your should match the implementation with definition. In your case:
IDefaultColors.IDefaultColorsManager should be IDefaultColors.IDefaultColorManager
or vice versa DefaultColorManager should be DefaultColorsManager.

Simple Injector inject dependency into custom global authentication filters and OWIN middle ware OAuthAuthorizationServerProvider

I used Simple Injector as our Ioc container; we have two problems.
We want to inject into our custom authentication filter; we read the post of converting attribute to a passive attribute: Convert Attribute into a passive. But we can't convert custom authentication filter attribute into a passive.
public class BearerAuthentication : Attribute, IAuthenticationFilter
{
public async Task AuthenticateAsync(
HttpAuthenticationContext context, CancellationToken cancellationToken)
{
}
public Task ChallengeAsync(
HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
{
}
}
We want to inject dependency into OWin middleware OAuthAuthorizationServerProvider; we know we can use begin execution context scope, but we want an elegant solution.
using (Ioc.Container.BeginExecutionContextScope())
{
}
Updated
public interface IAuthenticationFilter<TAttribute> where TAttribute : Attribute
{
Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken);
Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken);
}
public class BearerAuthenticationFilter : Attribute, IAuthenticationFilter<BearerAuthenticationFilter>
{
private readonly IAuthenticationBusinessEngine _authenticationBusinessEngine;
private readonly IHttpContextAccessor _httpContextAccessor;
public BearerAuthenticationFilter(IAuthenticationBusinessEngine authenticationBusinessEngine, IHttpContextAccessor httpContextAccessor)
{
_authenticationBusinessEngine = authenticationBusinessEngine;
_httpContextAccessor = httpContextAccessor;
}
public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
public class AuthenticationFilterDispatcher : IAuthenticationFilter
{
private readonly Func<Type, IEnumerable> _container;
public AuthenticationFilterDispatcher(Func<Type, IEnumerable> container)
{
_container = container;
}
public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
var descriptor = context.ActionContext.ActionDescriptor;
var attributes = descriptor.ControllerDescriptor.GetCustomAttributes<Attribute>(true)
.Concat(descriptor.GetCustomAttributes<Attribute>(true));
foreach (var attribute in attributes)
{
var filterType = typeof(IAuthenticationFilter<>).MakeGenericType(attribute.GetType());
var filters = _container.Invoke(filterType);
foreach (dynamic actionFilter in filters)
{
await actionFilter.AuthenticateAsync(context, cancellationToken);
}
}
}
public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public bool AllowMultiple
{
get
{
return true;
}
}
}
The equivalent code for working with IAuthenticationFilter is:
public interface IAuthenticationFilter<TAttribute> where TAttribute : Attribute
{
Task AuthenticateAsync(TAttribute attribute, HttpAuthenticationContext context);
}
public class AuthenticationFilterDispatcher : IAuthenticationFilter
{
private readonly Func<Type, IEnumerable> container;
public AuthenticationFilterDispatcher(Func<Type, IEnumerable> container) {
this.container = container;
}
public async Task AuthenticateAsync(HttpAuthenticationContext context,
CancellationToken token) {
var descriptor = context.ActionContext.ActionDescriptor;
var attributes = descriptor.ControllerDescriptor
.GetCustomAttributes<Attribute>(true)
.Concat(descriptor.GetCustomAttributes<Attribute>(true));
foreach (var attribute in attributes) {
Type filterType = typeof(IAuthenticationFilter<>)
.MakeGenericType(attribute.GetType());
IEnumerable filters = this.container.Invoke(filterType);
foreach (dynamic actionFilter in filters) {
await actionFilter.AuthenticateAsync((dynamic)attribute, context);
}
}
}
public async Task ChallengeAsync(HttpAuthenticationChallengeContext context,
CancellationToken token) { }
public bool AllowMultiple { get { return true; } }
}
Registration is done as follows:
GlobalConfiguration.Configuration.Filters.Add(
new AuthenticationFilterDispatcher(container.GetAllInstances));
// For Simple Injector 2.x:
container.RegisterManyForOpenGeneric(typeof(IAuthenticationFilter<>),
container.RegisterAll,
new[] { typeof(IAuthenticationFilter<>).Assembly });
// For Simple Injector 3.x:
container.RegisterCollection(typeof(IAuthenticationFilter<>),
new[] { typeof(IAuthenticationFilter<>).Assembly });
Now instead of making your attributes active, you can make the attribute passive and implement the required logic inside an IAuthenticationFilter<MyPassiveAttribute> implementation.
Your attribute and new component might look like this:
// NOTE: This attribute does not derive from anything Web API specific,
// just from Attribute
public class RequiresBearerAuthenticationAttribute : Attribute
{
// put here properties if required
}
public class BearerAuthenticationFilter
: IAuthenticationFilter<RequiresBearerAuthenticationAttribute>
{
private readonly IAuthenticationBusinessEngine _authenticationBusinessEngine;
private readonly IHttpContextAccessor _httpContextAccessor;
public BearerAuthenticationFilter(
IAuthenticationBusinessEngine authenticationBusinessEngine,
IHttpContextAccessor httpContextAccessor)
{
_authenticationBusinessEngine = authenticationBusinessEngine;
_httpContextAccessor = httpContextAccessor;
}
public async Task AuthenticateAsync(RequiresBearerAuthenticationAttribute attribute,
HttpAuthenticationContext context)
{
// TODO: Behavior here
}
}

How to properly set up WebApi, EasyNetQ and Ninject?

This is what I have thus far. I'm stuck on the concept/where to implement the IBus injection/creation point (the publisher). I was trying to keep the publisher functionality all within the project and not create a separate service for it.
bus = RabbitHutch.CreateBus("host=localhost", x => x.Register<IEasyNetQLogger>(_ => logger));
This is my first pass at this so I'm open to suggestions and best practice advice :-)
Things left to do:
create a rabbitmq config file with proper settings defining queue name and ?
create a message handler that takes care of connection management that is?
create the publisher at app start up, dispose properly when ?
EasyNetQ Wrapper to replace EasyNetQ internal IoC, Ninject replacement:
public class NinjectContainerWrapper: IContainer, IDisposable
{
public NinjectContainerWrapper()
{
}
//ninject container/kernal? here
//private readonly ISomeNinjectInterface container;
public TService Resolve<TService>() where TService : class
{
throw new NotImplementedException();
}
public IServiceRegister Register<TService, TImplementation>()
where TService : class
where TImplementation : class, TService
{
throw new NotImplementedException();
}
public IServiceRegister Register<TService>(Func<EasyNetQ.IServiceProvider, TService> serviceCreator) where TService : class
{
throw new NotImplementedException();
}
public void Dispose()
{
throw new NotImplementedException();
}
}
NinjectWebCommon.cs
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IAppSettings>().To<AppSettings>();
kernel.Bind<IDmpeService>().To<DmpeService>();
kernel.Bind<IPublisher>().To<DmpePublisher>();
kernel.Bind<IEasyNetQLogger>().To<GdmEasyNetQLogger>();
kernel.Bind<ILoggingService>().ToMethod(x =>
{
var scope = x.Request.ParentRequest.Service.FullName;
var log = (ILoggingService)LogManager.GetLogger(scope, typeof(LoggingService));
return log;
});
}
the publisher class:
public class DmpePublisher: IPublisher
{
private readonly IEasyNetQLogger _logger;
private readonly IAppSettings _appSettings;
private readonly IBus bus = null;
public DmpePublisher(IEasyNetQLogger logger, IAppSettings appSettings)
{
this._logger = logger;
this._appSettings = appSettings;
// register our alternative container factory
RabbitHutch.SetContainerFactory(() =>
{
var ninjectContainer = new NinjectContainerWrapper();
// wrap it in our implementation of EasyNetQ.IContainer
//return new NinjectContainerWrapper(ninjectContainer);
});
bus = RabbitHutch.CreateBus("host=localhost", x => x.Register<IEasyNetQLogger>(_ => logger));
}
public void PublishMessage(Messages.IMessage message)
{
throw new NotImplementedException();
//log post
//_logger.InfoWrite("Publishing message: {0}", message);
}
}

Resources