Search code examples
c#autofacmasstransit

Masstransit Filter with IoC injection/Database


I have to validate incoming message before passing it to my consumer. To do it, I need to request some data from the database.

Following the tutorials, I created extension method to apply my specification + filter to the consumer pipe. Something like this:

public static void UseArticlesImportValidation(this IConsumePipeConfigurator configurator){}

public class ArticlesImportValidationSpecification : IPipeSpecification<ConsumeContext<ArticlesImportNotification>>

and the Filter

public class ArticlesImportValidationFilter : IFilter<ConsumeContext<ArticlesImportNotification>>

Everything looks good, but I want to injection some business services in my Filter to reuse some functionality + DAL services. This works completely fine for my Consumer using Autofac extension method builder.RegisterConsumers(Assembly.GetExecutingAssembly());.

Should I use middleware for this at all? Any suggestions?


Solution

  • You need to have the built container in your specification (it is easy to pass as a parameter when you call AddPipeSpecification and then in the specification:

    public void Apply(IPipeBuilder<T> builder)
    {
        builder.AddFilter(new ArticlesImportValidationFilter(container.Resolve<IDataAccessSomethingUseful>()));
    }
    

    But I would validate in the consumer or, if you want to keep them separate, have one consumer to validate and send the next message to do the actual job.