Code Monkey home page Code Monkey logo

sample-scopedfilters's Introduction

Scoped Filters

MassTransit supports scoped filters, allowing access to the same container scope as consumers as well as message producers, like those within ASP.NET controllers, etc.

This sample shows how to manage a scoped class, Token, and use it to pass values between an ASP.NET controller action and MassTransit send/publish middleware filters.

Simply open it up and run it, and add a header to the request (using Postman, CURL, whatever) called "Token" with any string value to get a response.

sample-scopedfilters's People

Contributors

phatboyg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

sample-scopedfilters's Issues

ConsumeFilter.Send is executed after IConsumer is instantiated

I have issue with Consume Filter - it seems it's getting executed after IConsumer instance is created.
In my case I want to take message header ("User-Guid" in the example below) and inject it into scoped service (IAuthContextContainer with UserContext property) and then inject IAuthContextContainer into consumer class.
The problem is that I have also some more complex DI related to AuthContextContainer which internally uses IServiceProvider.GetRequiredService<>() (in consumer constructor scope, not in any method). In this case it throws error because ConsumeFilter haven't been executed yet (see output below).

Is is by design? Is there any workaround?

Consumer filter:

public class AuthContextConsumeFilter<T> :
        IFilter<ConsumeContext<T>>
        where T : class
    {
        readonly IAuthContextContainer _authContextContainer;

        public AuthContextConsumeFilter(IAuthContextContainer authContextContainer)
        {
            _authContextContainer = authContextContainer;
        }

        public Task Send(ConsumeContext<T> context, IPipe<ConsumeContext<T>> next)
        {
            var userGuidStr = context.Headers.Get<string>("User-Guid");

            if (!string.IsNullOrWhiteSpace(userGuidStr))
            {
                _authContextContainer.UserContext = new UserContext(Guid.Parse(userGuidStr));
            }
            
            Console.WriteLine("AuthContextConsumerFilter executed");

            return next.Send(context);
        }

        public void Probe(ProbeContext context)
        {
        }
    }

Consumer:

public class CommandService
        : IConsumer<MyPayload>
    {
        readonly IAuthContextContainer _authContextContainer;
        
        protected CommandService(IAuthContextContainer authContextContainer;)
        {
            _authContextContainer = authContextContainer;
           Console.WriteLine($"AuthContextConsumer::Ctor {_authContextContainer.UserContext}");
        }
        
        public async Task Consume(ConsumeContext<MyPayload> consumeContext)
        {
            Console.WriteLine($"AuthContextConsumer::Consume {_authContextContainer.UserContext}");
        }

Output:

AuthContextConsumer::Ctor // <= no UserContext here
AuthContextConsumerFilter executed // <= executed after consumer constructor
AuthContextConsumer::Consume Airstrike.Backend.Common.Application.Authentication.UserContext // user context is injected here

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.