PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label masstransit. Show all posts
Showing posts with label masstransit. Show all posts

Monday, September 19, 2022

[FIXED] How to pass IBus to masstransit consumer constructor

 September 19, 2022     c#, consumer, masstransit     No comments   

Issue

Good day

I'm registering masstransit consumer this way:

var provider = services.BuildServiceProvider();

cfg.ReceiveEndpoint(RabbitMqOptions.StatusChangeQueueName, e =>
{
e.Consumer(() => new ConsumerProcessingStatusChange(provider));
});

and want my consumer to Publish to another exchange is there a way to get IBus from service provider in consumer constructor? or should I pass other parameter (which one?)

public class ConsumerProcessingStatusChange : IConsumer<***>
    {
        private readonly IBus _bus;
        private readonly IMapper _mapper;

        public ConsumerProcessingStatusChange(IServiceProvider provider)
        {
            _bus = provider.GetService<IBus>();
            _mapper = new MapperConfiguration(cfg => cfg.AddProfile<MappingProfile>()).CreateMapper();
        }

        public async Task Consume(ConsumeContext<***> context)
        {
            await _bus.Publish(_mapper.Map<***>(context.Message));
        }

and is it correct way to create mapper here or it could be passed as well?


Solution

You don't need to inject IBus, and it's not the recommended approach for chaining messages. You should use context.Publish instead, which not only will publish your message, but also establish a link between those messages with a proper causation and conversation ids.

In addition, injecting the service provider is a service locator antipattern. I could suggest reading at least the DI docs from Microsoft to understand how it should be done.

If you configure your application following the documentation, you will get your dependencies injected when the consumer is instantiated. In particular, this example shows how it's done:

 services.AddMassTransit(x =>
 {
     x.AddConsumer<EventConsumer>();
     x.UsingRabbitMq((context, cfg) =>
     {
         cfg.ReceiveEndpoint("event-listener", e =>
         {
             e.ConfigureConsumer<EventConsumer>(context);
         });
     });
 });

When configured like this, the EventConsumer instance will get all its dependencies properly injected, if it has any, and these dependencies are properly registered in the services collection.

But still, to publish a message from inside the consumer, you better use context.Publish.



Answered By - Alexey Zimarev
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Monday, July 11, 2022

[FIXED] How are message contracts versioned for service buses?

 July 11, 2022     esb, masstransit, message, messagecontract     No comments   

Issue

Let's say we use message contracts based on interfaces, as recommended for MassTransit for example.

First how are those interfaces shared across all the applications? Let's say we provide them in a nuget package. (Is that the way to go?)

So second, how do we now make sure all the applications use the same version?

Should we use new interfaces every time (e.g. messageV1, messageV2) to be backwards compatible? That would require us to send multiple messages at once instead of 1...

Or should we have an upgrade window, where all applications are updated at the same time?


Please check out both the answers and the comments, if you are looking in the same.
Really got some quality feedback here :D


Solution

MassTransit doesn't explicitly support any kind of versioning, so you're left with the freedom to choose to do what you think is best. The assumptions you've made in your question are more or less exactly the way I do things:

  • Contracts are shared as a nuget package across subsystems
  • New interfaces are created when changes need to be made, interfaces are only ever extended with nullable / backwards compatible changes
  • If necessary, multiple messages are published/sent to preserve backwards compatibility
  • When no longer needed, older versions can be obsoleted/removed

It can seem like a lot of work, but if you design things to work that way from the start it's not so bad, and it really pays off.



Answered By - nizmow
Answer Checked By - Robin (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing