Home > other >  MassTransit is publishing to the wrong endpoint
MassTransit is publishing to the wrong endpoint

Time:11-24

I have a C# application that reads from one Azure service bus (external), and publishes to a different Azure service bus (internal).

In my publish class, the constructor accepts IPublishEndpoint, which is used when publishing the message:

IPublishEndpoint endpoint;
...
await endpoint.Publish(message, cancellationToken);

The type of endpoint is: massTransit.Context.ConsumeContextScope<ExternalServiceBus.Messages.IDeviceChanged>

Which is the context that the message was received on, not the one it should be published on. I believe this is causing the message to be sent back to the external bus, but it then fails to publish because there isn't an endpoint for that type of message.

I can't replace IPublishEndpoint with IBusControl because IBusControl creates a new DI scope to publish from, and I need data from the external consumer filters to pass to the internal publish filters.

How can I get the correct (internal) publish context?

CodePudding user response:

If you are using MultiBus, IPublishEndpoint will always default to the normal IBus instance. I'm not entirely sure if the current scope will be used (it should, but I haven't verified it for your scenario), but you can get the specific IPublishEndpoint for another bus instance using that bus interface type as key and depending upon:

public class SomeConsumer :
    IConsumer<SomeMessage>
{
    public SomeConsumer(Bind<IInternalBus, IPublishEndpoint> publishEndpoint)
    {
        _publishEndpoint = publishEndpoint.Value;
    }
}

You can replace IInternalBus with any bus interface type (including IBus for the default bus).

  • Related